Thursday, August 02, 2007

Enterprise Service Bus and Mule ESB

Some of my friends mentioned that my blog entries these days aren't too technical. Admittedly, am cranking out lot of enterprise software :-). This purpose of this entry is to be as technical as possible and get head on with Mule ESB.

If you are a novice to ESB, I suggest to read this first. Essentially, an ESB is an integration backbone supporting the SOA. Being a strong advocate of open source software, Mule ESB has been picked up as the integration bus. There is some documentation on the Mule Wiki to start with and have the mule up and running. I'll discuss a simple example of reading a file from a folder, do some processing and publish the message on to a Weblogic JMS queue.

The first thing is to have the Mule server installed and have it running. I used Mule 1.3.3. The process is straight forward. The first time when the server is brought up, a couple of jars are downloaded. If you encounter any difficulties during this step, download the jars manually and place them at $MULE_HOME/lib/user.

Getting Mule to work for you requires no more than a configuration file and few java classes, if required. The most important one is the configuration file. The configuration starts with the "connector" element. For detailed information on the Mule architecture refer the Wiki. Since, my requirement was to read a file and publish the message on to a queue, I used the FileConnector and JmsConnector. The configuration for these elements looks like the following:



<connector name="myFileConnector" className="org.mule.providers.file.FileConnector"/>
<connector name="jmsConnector" className="org.mule.providers.jms.JmsConnector">
<properties>
<property name="jndiDestinations" value="true"/>
<property name="forceJndiDestinations" value="true"/>
<property name="specification" value="1.0.2b"/>
<property name="connectionFactoryJndiName" value="jms/MyJMSConnectionFactory"/>
<property name="jndiInitialFactory" value="weblogic.jndi.WLInitialContextFactory"/>
<property name="jndiProviderUrl" value="t3://localhost:55555"/>
</properties>
</connector>


Next, a very basic Transformer that reads the contents of the file and appends TEST at the end of each line. The following is the java class that does this. The compiled class should be packaged into a jar and placed in $MULE_HOME/lib/user.


public class MyTransformer extends AbstractTransformer{

public MyTransformer() {
registerSourceType(byte[].class);
setReturnClass(String.class);
}

public Object doTransform(Object src, String encoding) throws TransformerException {
String s = null;
try{
byte[] theBytes = (byte[])src;
String str = new String(theBytes);
BufferedReader br = new BufferedReader(new StringReader(str));
String line = null;
StringBuffer buff = new StringBuffer();
while ( (line = br.readLine()) != null)
{
System.out.println(">>>> "+line);
buff.append(line).append("TEST").append("\n");

}
s = buff.toString();
} catch(Exception e) {
throw new TransformerException(
Message.createStaticMessage("Unable to Transform Message."), e);
}
return s;
}
}


The transformer configuration in the mule config file:

<transformers>
<transformer name="myTransformer" className="com.test.MyTransformer"/>
</transformers>


Finally, configuring the endpoints. Place the weblogic jar in the $MULE_HOME/lib/user to access the JMS specific dependencies. The endpoint configuration:

<model name="READ_FILE">
<mule-descriptor name="read-file" implementation="org.mule.components.simple.BridgeComponent">
<inbound-router>
<endpoint address="file:///C:/mule-1.3.3/sandbox/in?connector=myFileConnector" transformers="myTransformer"/>
</inbound-router>
<outbound-router>
<router className="org.mule.routing.outbound.OutboundPassThroughRouter">
<endpoint address="jms://MyJMSQueue"/>
</router>
</outbound-router>
</mule-descriptor>
</model>


That is all it takes to have the stuff working. There is not much of coding here, but configuring elements play a key role in having Mule work as expected. The info I was looking for wasn't available at once place. This blog entry addresses the nitty gritty to a fair extent. The version of Mule I used had a problem with the JmsConnector that broke in weblogic, there was no cast to QueueSender on MessageProducer in the class Jms102bSupport. I corrected this and had to build the source to fix the issue. However, this seems to have been corrected in version 1.4.

Mule ESB is flexible, and easy to configure and use. I just started working with Mule and there is a lot to learn. Suggestions, case studies on mule will be of great help to me. Feel free to post a comment on this entry.