Use Spring JMS to simplify Asynchronous Message Processing

Source: Internet
Author: User
Tags jboss

Http://www.onjava.com/lpt/a/6490

This article describes how Spring simplifies asynchronous message calls. It uses a loan example to explain how Spring reduces

The amount in development.

The following is the amount of code required by traditional development.

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) {}        }    }}
 
Then the Spring code
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();    }}
On the surface, Spring wins, and the code is much less, but let's take a look at the XML configuration of Spring.
  
   
    
     
                      org.jnp.interfaces.NamingContextFactory            
     
     
                      localhost            
     
     
                      org.jnp.interfaces:org.jboss.naming            
     
    
   
  
  
   
    
   
   
    
     UIL2ConnectionFactory
    
   
  
   
    
     
    
    
     
      queue/CreditRequestSendQueue
     
    
   
   
    
     
    
    
     
      queue/CreditReqeustReceiveQueue
     
    
   
   
    
     
    
    
     
    
    
     
      30000
     
    
   
   
    
     
    
   
   
    
     
    
   
 
We began to recognize the true face of Spring. The configuration with no headers is a nightmare of XML. From chaos to chaos.
 
 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 607922

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.