Monday, December 10, 2007

JMS Helper

In many application there is a need to send asynchronise messages, The right solutions is usually to use JMS Api, however the JMS Api is not trivial to use, and in many cases the user may forget to close the session or the publisher or it does it but not in finanlly block.

A good solution is to build a light framework above the JMS API which would take care for all this issues.

Here is the code implentation for the JMS Helper which take care for all the JMS issues as opening and closing the sessoins, and it prove a simple easy to use API:

public class JMSHelper
{
private static final Logger logger = Logger.getInstance();

/**
* Publish notification to a specific topic (topicRef) with a message, the notification is sent using

* the publish/subscribe mechanism of the JMS.
*
* @param jmsFactory
* @param topicRef the topic jndi name to reference to
* @param message the message to deliver, if the message is null then empty message is publish
* @param isTransacted indicates whether the publish is transacted
* @param properties the properties on the message, the properties used by the selector, if this value is null then no properites are transferred
* @throws JmsException
*/
public static void publish(String jmsFactory, String topicRef, Serializable message, boolean isTransacted, HashMap properties) throws JmsException
{
TopicConnection topicConnection = null;
Topic topic = null;
TopicSession topicSession = null;
TopicPublisher topicPublisher = null;
TopicConnectionFactory topicConnectionFactory = null;

try
{
// get the topic connection factory from JNDI
topicConnectionFactory = (TopicConnectionFactory) ServiceLocator.getInstance().getService(jmsFactory);

// create topic connection
topicConnection = topicConnectionFactory.createTopicConnection();

// get the topic from JNDI
topic = (Topic) ServiceLocator.getInstance().getService(topicRef);

// create topic session
topicSession = topicConnection.createTopicSession(isTransacted, Session.AUTO_ACKNOWLEDGE);

// create sender
topicPublisher = topicSession.createPublisher(topic);

// publish
ObjectMessage objectMessage = topicSession.createObjectMessage();

// Set the message properties (selectors)
if (properties!=null)
{
Map.Entry element = null;
Iterator propertyIterator = properties.entrySet().iterator();

while (propertyIterator.hasNext())
{
element = (Map.Entry) propertyIterator.next();
objectMessage.setObjectProperty((String) element.getKey(), element.getValue());

}
}

// Set the message text
if (message!=null)
{
objectMessage.setObject(message);
}

// Publish
topicPublisher.publish(objectMessage);
}
catch (Exception exception)
{
logger.error(exception,"Failed to publish JMS message");
throw new JmsException(exception);
}
finally
{
try
{

// clos es the jms topic session
if (topicSession!=null)
{
topicSession.close();
}

// closes the topic publisher
if (topicPublisher!=null)
{
topicPublisher.close();
}

}
catch (Exception exception)
{
logger.error(exception,"Failed closing the jms connection");
throw new JmsException(exception);
}
}
}

}

No comments:

Links

 
RSS Feeds Submission Directory