Wednesday, January 07, 2009

Implementing JMS with Oracle Coherence

I have come across many applications that use JMS infrastructure to pass messages from one system to another. If you are already using Oracle Coherence or thinking of employing one then for many scenarios you do not need to have another messaging provider. ezMQ is an effort to provide a JMS implementation on top of Oracle Coherence. Do not confuse this project with Coherence's Incubator Messaging Pattern. The scope and target use cases are different even though it can be easily integrated. So if you are fed up with additional licenses for a JMS provider for straight forward usecases or looking to consolidate the infrastructure, the project ezMQ lets you keep your current application unchanged and gets it integrated with Coherence infrastructure by replacing and deploying just a few configuration files. The project is a stab to address this unique problem space and even though does not guarantee to work in all scenarios but the code is available for free download, make updates to and share it further. More details on ezMQ can be found at http://sites.google.com/site/miscellaneouscomponents/Home/ezmq
If you have an application similar to the following then you can use Oracle Coherence as the JMS provider by using ezMQ APIs. The only change set the jndi.properties or the Context.PROVIDER_URL.

public class Client implements MessageListener {
public void someMethod () {
Properties env = new Properties();
env.setProperty(Context.PROVIDER_URL, "...");
InitialContext ctx = new InitialContext(env);

TopicConnectionFactory factory =
(TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
TopicConnection connection = factory.createTopicConnection();
Topic topic = (Topic) ctx.lookup("Topic");

addSubscriber(connection, topic);
publishMessage(connection, topic);
}

private void publishMessage(TopicConnection connection, Topic topic)
throws JMSException {
TopicSession pubSession =
connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = pubSession.createPublisher(topic);
TextMessage message = pubSession.createTextMessage();
message.setText("Ashish");
publisher.publish(message);
}

private void addSubscriber(TopicConnection connection, Topic topic)
throws JMSException {
TopicSession subSession =
connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = subSession.createSubscriber(topic);
subscriber.setMessageListener(this);

}

public void onMessage(Message message) {
try {
TextMessage tMsg = (TextMessage) message;
String text = tMsg.getText();
System.out.println("On Message: " + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
This version of ezMQ is pretty basic and does not implement all the bells and whistles of other popular JMS providers but it works beautifully and provides a platform to expand.
How is it implememted? There are two aspects of creating a JMS implementation:
  1. Taking care of JNDI
    • Context (EzContext)
    • Context Factory (EzContextFactory)
  2. Taking care of JMS
    • Topic (EzTopic)
    • TopicConnection (EzTopicConnection)
    • TopicConnectionFactory (EzTopicConnectionFactory
    • TopicSession (EzTopicSession)
    • TopicPublisher (EzTopicPublisher)
    • TopicSubscriber (EzTopicSubscriber)
    • TextMessage (EzMessage)
So take a look and enjoy!

No comments: