Wednesday, April 2, 2008

Spring JMS Template

It provides a template mechanism to hide the details of a typical JMS implementation so developers can concentrate on the actual task of processing messages instead of worrying about how to create, access, or clean up JMS resources.

Synchronous Messaging Service Vs. Asynchronous Messaging Service

Synchronous Messaging Service
1. Has to wait for service completion before starting another service or processing further.
2. Sometime its time consuming when message consumption takes much time.
Asynchronous Messaging Service
1. Does not have to wait for service to be completed. Another service can come which than can be sending in queue rather than waiting for first request to complete.
2. Can’t use in all application.

Two types of messaging:
* Point to Point
* Subscribe & Publish.

JMS
1. Similar to writing JDBC or JCA code
2. Boilerplate code to create or retrieve JMS resource objects that make the task more with more repetitive code.

Typical Process of using JMS:
1. Create JNDI initial context.
2. Get a queue connection factory from the JNDI context.
3. Get a Queue from the queue connection factory.
4. Create a Session object.
5. Create a sender or receiver object.
6. Send or receive a message using the sender or receiver object created in Step 5.
7. Close all of the JMS resources after processing the messages.

Terminology:

A JMS provider is a messaging system that implements the JMS interfaces and provides administrative and control features. An implementation of the J2EE 1.3 platform includes a JMS provider. Bea Weblogic supports JMS as one of its j2ee service.

JMS clients are the programs or components written in the JavaTM programming language that produce and consume messages. Thus both producer and consumer of the Queue or Topic are known as JMS clients

Messages are the objects that communicate information between JMS clients.

Administered objects are preconfigured JMS objects created by an administrator of the J2EE Application Server (e.g. Bea Weblogic or IBM’s Websphere) for the use of clients. There are two kinds of administered objects, destinations (Queue or Topic) and connection factories.

JMS producer sends messages to the Destinations to be consumed by the consumer. Destination can be either Queue or Topic.

SPRING JMS
1. Templates to hide detail of java API.
2. Spring provides the JMSTemplate class so developers don't have to write the boilerplate code for a JMS implementation.
Advantages:
• Provides JMS abstraction API to simplify use of destination (Queue/Topics) and publish message to specified destination.
• No need to deal with Exception as Spring provides unchecked exception for any JMS exception that is re-thrown in JMS code.
• Simple for using asynchronous messaging
Java Classes to use with JMS application.

1. JmsException
This is the base (abstract) class for any exceptions thrown by Spring framework whenever there is a JMS exception.
2. JmsTemplate & JmsTemplate102
These are helper classes used to simplify the use of JMS by handling the creation and release of JMS resources like connection factories, destinations, and sender/receiver objects. JmsTemplate102 is a subclass of JmsTemplate that uses the JMS 1.0.2 specification.

• To get started using JmsTemplate, need to know which JMS specification is supported by the JMS provider. E.g. JBoss AS 4.0.2 supports the JMS 1.0.2 specification.
• JMS 1.1 unifies the programming interfaces for PTP and (Pub/Sub) domains. With this developer can create transacted session, and receive a message from QUEUE (PTP) and send message to TOPIC (Pub/Sub) within the same JMS transaction.

Various methods to send & receive messages:

Send: Send a message to default or specified destination.

Receive: Receive a message from default or specified destination, but only wait for a specified time of delivery.

ConvertAndSend: delegates the conversation process to an instance of MessageConverter interface and then sends it to destination.

ReceiveAndConvert: Receives a message from default or specified destination and converts it to JavaObject.

3. MessageCreator
This is a callback interface used by the JmsTemplate class. It creates a JMS message for a specified session.
4. MessageConverter
This interface acts as an abstraction to convert between Java objects and JMS messages.

MessageConverter
The MessageConverter interface defines a contract to convert Java objects into JMS messages.
Application code can focus on the business objects and not bother with the inner details of how it's represented as a JMS message.
SimpleMessageConverter (and SimpleMessageConverter102) are default implementations of MessageConverter. They are used to convert a String to a JMS TextMessage, a byte array (byte[]) to a JMS BytesMessage, a Map to a JMS MapMessage, and a Serializable object to a JMS ObjectMessage, respectively. You can also write your own custom implementations of MessageConverter to convert XML documents into a TextMessage object using an XML binding framework such as JAXB, Castor, Commons Digester, XMLBeans, or XStream.

5. DestinationResolver
This is an interface used by JmsTemplate for resolving destination names. DynamicDestinationResolver and JndiDestinationResolver are the default implementations of this interface.

Destinations
Destinations are stored and retrieved using a JNDI context. While configuring a Spring application context, we use JndiObjectFactoryBean class to get reference to JMS destinations.
The DestinationResolver interface is used to resolve a destination name to JMS destination, which is used when application has lots of destinations.
DynamicDestinationResolver is used to resolve dynamic destinations.

Process:

1. First thing we need message queue to send and receive message. Configure it in jbossmq-wsc-jboss-service.xml file.


name="jboss.mq.destination:service=Queue,name=CreditRequestSendQueue">

jboss.mq:service=DestinationManager



2. Create new destinations with queue/CreditRequestSendQueue and queue/CreditRequestReceiveQueue.

3. JMS configuration:

4. JNDI context to get resource.





org.jnp.interfaces.NamingContextFactory


localhost


org.jnp.interfaces:org.jboss.naming





5. JMS Queue connection factory configuration.

class="org.springframework.jndi.JndiObjectFactoryBean">




UIL2ConnectionFactory



JndiObjectFactoryBean:
FactoryBean that looks up a JNDI object. It exposes object found in JNDI when used as bean reference. E.g “dataSource”
Register this as singleton factory in an application context.

6. Configuration for send & receive Destination

class="org.springframework.jndi.JndiObjectFactoryBean">




queue/CreditRequestSendQueue



7. JMS Template configuration

class="org.springframework.jms.core.JmsTemplate102">







30000



8. JMS Sender & Receiver configuration















9. Testing:

public void send() {
try {
ClassPathXmlApplicationContext appContext = new
ClassPathXmlApplicationContext(new String[] {
"spring-jms.xml"});

System.out.println("Classpath loaded");

JMSSender jmsSender = (JMSSender)appContext.getBean("jmsSender");

jmsSender.sendMesage();

System.out.println("Message sent using Spring JMS.");
} catch(Exception e) {
e.printStackTrace();
}
}

No comments: