ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Asynchronous Messaging Made Easy With Spring JMS
Pages: 1, 2, 3, 4, 5

Testing & Monitoring

I wrote a test class called LoanApplicationControllerTest to test the LoanProc application. We use this class to set the loan parameters and call the credit request service class.

Let's look at the message sender implementation using the traditional JMS development approach without the Spring JMS API. Listing 9 shows the sendMessage method in MessageSenderJMS class with all the steps required to process a message using the JMS API.

Listing 9. Traditional JMS implementation

public void sendMessage() {

    queueName = "queue/CreditRequestSendQueue";
    System.out.println("Queue name is " + queueName);

     * Create JNDI Initial Context
    try {
        Hashtable env = new Hashtable();

        jndiContext = new InitialContext(env);
    } catch (NamingException e) {
        System.out.println("Could not create JNDI API " +
            "context: " + e.toString());

     * Get queue connection factory and queue objects from JNDI context.
    try {
        queueConnectionFactory = (QueueConnectionFactory)

        queue = (Queue) jndiContext.lookup(queueName);
    } catch (NamingException e) {
        System.out.println("JNDI API lookup failed: " +

     * Create connection, session, sender objects.
     * Send the message.
     * Cleanup JMS connection.
    try {
        queueConnection =
        queueSession = queueConnection.createQueueSession(false,
        queueSender = queueSession.createSender(queue);
        message = queueSession.createTextMessage();
        message.setText("This is a sample JMS message.");
        System.out.println("Sending message: " + message.getText());

    } catch (JMSException e) {
        System.out.println("Exception occurred: " + e.toString());
    } finally {
        if (queueConnection != null) {
            try {
            } catch (JMSException e) {}

Now, let's look at the message sender implementation using Spring. Listing 10 shows the code for the send method in the MessageSenderSpringJMS class.

Listing 10. JMS implementation using Spring API

public void send() {
    try {
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {

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

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


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

As you can see, all the steps related to managing JMS resources are handled by the Spring container using the configuration file. We just need to get a reference to a JMSSender object and then call sendMessage on this object.


In this article, we looked at how the Spring framework simplifies working with asynchronous messaging applications using the JMS API. Spring takes away all of the boilerplate code required to process a message using JMS, such as getting a queue connection factory and creating queue and session objects from Java code and wiring them at runtime using a configuration file. We can swap the JMS resource objects dynamically without having to modify any Java code, thanks to the power of the Inversion of Control (IOC) principle.

Since async messaging is an integral part of a SOA framework, Spring fits nicely into the SOA toolset. Also, a JMS management tool such as Hermes makes it easy to create, manage, and administer the JMS resources, especially for system administrators.


Srini Penchikala is an information systems subject matter expert at Flagstar Bank.

Return to ONJava.com.