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

advertisement

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();
        env.put("java.naming.factory.initial",
            "org.jnp.interfaces.NamingContextFactory");
        env.put("java.naming.provider.url","localhost");
        env.put("java.naming.factory.url.pkgs",
            "org.jnp.interfaces:org.jboss.naming");

        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)
        jndiContext.lookup("UIL2ConnectionFactory");

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

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

    } catch (JMSException e) {
        System.out.println("Exception occurred: " + e.toString());
    } finally {
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } 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[] {
                "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();
    }
}

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.

Conclusions

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.

Resources

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


Return to ONJava.com.