Mule is a lightweight open source ESB that is not bogged down by standards but yet enables to integrate applications that support various open standards. By bogged down I mean – its not a JBI compliant ESB. Yes and it supports JMS, Web Services, Java POJO.
For a general overview of ESB see my article from some time back – Enterprise Service Bus. An ESB is made up of a high-performance messaging layer (such as ActiveMQ) and the ESB software itself (Mule). The ESB software provides integration services to create/host/manage business services, mediation, content transformation, routing, security and so on.
To get started go to http://www.mulesoft.org and download the software. I downloaded the full version – 2.2.1. For the messaging layer go download ActiveMQ from http://activemq.apache.org/ (the version I used is 5.2.0 … latest as of today is 5.3.2). Finally I am using Eclipse 3.5 and also installed the Mule IDE Eclipse Plugin (Plugin).
Once you have the Mule Eclipse plug-in installed , create a new Mule project and then add the code below to make it work.
Our use case is:
- Purchase Orders come in XML .dat files to a folder.
- We want to scan the folder periodically and load up any incoming files.
- Now we want to check if the PurchaseOrder for item with part number as “WXJ-1”.
- If the part number is found then that purchase order is routed to the JMS Queue “ImportantPOQ”
- All other orders are routed to JMS Queue “AllOtherPOsQ”
To begin, configure the JMS connector in the mule-config.xml:
1 2 |
1: <mule-jms:activemq-connector name="jmsConnector" brokerURL="tcp://localhost:61616" /> |
Next Configure the file connector to poll for incoming files:
1 2 |
1: <mule-file:connector name="fileConnector" fileAge="5000" autoDelete="false" pollingFrequency="5000" /> |
The service itself is configured as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
1: <model name="myproject"> 2: <service name="FileReader"> 3: <inbound> 4: <mule-file:inbound-endpoint address="file:///Users/mathew/temp/in" moveToDirectory="/Users/mathew/temp/out" moveToPattern="#[DATE]-#[ORIGINALNAME]"> 5: <mule-file:filename-wildcard-filter pattern="*.xml" /> 6: </mule-file:inbound-endpoint> 7: </inbound> 8: <log-component></log-component> 9: <outbound> 10: <filtering-router> 11: <mule-jms:outbound-endpoint queue="ImportantPOQ" /> 12: <mule-xml:xpath-filter pattern="/PurchaseOrder/Items/Item/@PartNumber" expectedValue="WXJ-1" /> 13: </filtering-router> 14: <forwarding-catch-all-strategy> 15: <mule-jms:outbound-endpoint queue="AllOtherPOsQ" /> 16: </forwarding-catch-all-strategy> 17: </outbound> 18: </service> 19: </model> |
A Mule Service has a inbound router , a service component and an outbound router. The inbound router is used to configure how the service will consume messages and from where. In this case you can see the use of the file connector to read files from a predefined folder.
The Service component typically contains your business logic. For the purposes of this article I do not have a service component. Instead I use a predefined log component to print out the file contents to the console.
The outbound router is used to configure where the processed messages will go once the business component is finished with it. In this case I configure a filtering-router which sends messages to the queue ImportantPOQ if the part number is WXJ-1. The forwarding-catch-all-strategy router is used for all other unmatched messages.
I have a little utility class that spits out a bunch of test xml files. The project structure in Eclipse should look like this…
Ignore the java classes since my next task is to send the PurchaseOrder to a POJO service component. For the purposes of this article there is no java code used.
Start your ActiveMQ server and create the two queues noted in the mule-config.xml. Right click on the mule-config.xml -> RunAs -> Mule Server. If everything goes well the server should start and be polling for files in the predefined folder. Run the FileCreator.java to put some files into your folder and in a few seconds you should see that the files are processed, moved to the processed folder. Go to the ActiveMQ web admin page at localhost:8161/admin/queues.jsp. You should see messages in both queues. Most will end up in the AllOtherPOsQ. Any messages with partnumber as WXJ-1 will end up in the ImportantPOQ.
Download the project by clicking here Mule Sample.