Why to use JMS

January 19, 2009

Java Messaging Service (JMS) is part of J2EE, a Message Oriented Middleware (MOM) API for sending/receiving message. It supports two distinct message service. One of them is Point-to-Point and Other one is Publish and subscribe model. In point-to-point mode, a producer publish a message to a queue and consumer reads from the queue. Both producer and consumer can run independently. A successful process is acknowledged by consumer in case of synchronize message. A message is publish on a topic in publish/subscribe model. If a particular subscriber register for the topic, it will receive the message without knowing about the publisher. One topic can be registered by many subscribers. A subscriber must be active to receive a message. JMS is loosely coupled communications for message precessing where the sender doesn’t worry about whether the message consumed by the consumer. All it do is to send the message into the message queue in the server.

In Message Queue (MQ) server a message is nothing more than stream of bytes (XML document, serialized Java object, text string). Interpretation is totally concern of the application domain. It has no duty with the structure of the entire message.

Continue….

References:
1. http://en.wikipedia.org/wiki/Java_Message_Service
2. http://www.ibm.com/developerworks/java/library/j-jtp0205.html
3. http://today.java.net/pub/a/today/2005/06/03/loose.html

By: Md. Shahjalal


JMS with Spring Framework

January 18, 2009

Uses of Java Messaging Service (JMS) are populated across the web applications. Spring framework provides JMS integration framework to simplify the use of this useful message publisher (message production) and consumer (synchronous/asynchronous message reception) technology. Class JMSException (org.springframework.jms.support) can be used for exception handling. MessageConverter (org.springframework.jms.support.converter) can be used abstract for the conversion between Java Object and JMS Message. The org.springframework.jms.support.destination package is useful for managing various strategy message destination such as router settings. JmsAccessor (org.springframework.jms.support) class used for connection factory and session acknowledgment mode. For handling connection management spring provides package org.springframework.jms.connection

JmsTemplate class is used for sending a message while MessageCreator is used for creating a message. Both of these classes are part of org.springframework.jms.core package of Spring framework. A simple message can be created by using Message (javax.jms.Message) class for publishing. Following code can be used to create a sample message by a publisher class:


import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.Message;
import javax.jms.Session;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;

public class simpleJmsPublisher {
private JmsTemplate jmsTemplate;
public void publish(final MessageClass messageToPublish) {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
ObjectMessage message = session.createObjectMessage();
message.setObject((Serializable)messageToPublish);
return message;
}
});
}
}

Implementation of onMessage() method of MessageListener (javax.jms.MessageListener) interface in the consumer class used to receive the message that was published from publisher class.


import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class simpleJmsConsumer implements MessageListener {
public void onMessage(Message receivedMessage) {
if (receivedMessage instanceof MessageClass) {
try {
//Do the specific task to be done by receiving message
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
} else {
throw new IllegalArgumentException(“Message must be of type MessageClass”);
}
}
}

Source : http://static.springframework.org/spring/docs/2.5.x/reference/jms.html

by: Md. Shahjalal


Follow

Get every new post delivered to your Inbox.