Send messages from the JMS of spring

Source: Internet
Author: User

Jmstemplate contains many methods for sending messages. There are some sending methods that specify the destination, use a Javax.jms.Destination object, and those that use a string as a parameter and need a JNDI query. The sending method with no specified destination uses the default purpose. Here is an example of sending a message to a queue.


[Java]  View Plain  copy   import javax.jms.connectionfactory;   import  javax.jms.jmsexception;   import javax.jms.message;   import javax.jms.Queue;    import javax.jms.session;      import  org.springframework.jms.core.messagecreator;   import org.springframework.jms.core.jmstemplate;       public class jmsqueuesender {           private JmsTemplate jmsTemplate;       private Queue  queue;          public void setconnectionfactory ( CONNECTIONFACTORY CF)  {           this.jmstemplate  = new jmstemplate (CF);       }           public void setqueue (queue qUeue)  {           this.queue = queue;        }          public void  Simplesend ()  {           this.jmstemplate.send ( This.queue, new messagecreator ()  {                public message createmessage (session session)  throws  jmsexception {                    return session.createtextmessage ("Hello queue world");                }            });       }  }  


This example uses the Messagecreator callback to create a text message from a supported session object. Jmstemplate is built by passing connectionfactory references. Optionally, a default constructor and ConnectionFactory are provided and can be used to build instances of the JavaBean type (using a beanfactory or Pojo). Also, consider the base class provided by spring's Jmsgatewaysupport, which provides pre-built bean properties for the JMS configuration.


The Send (String destinationname, Messagecreator Creator) method lets you send a message using the name of the destination. If these names are registered in Jndi, you need to set the Destinationresolver property of this template to the instance of Jndidestinationresolver.


If you created jmstemplate and specified the default purpose, send (Messagecreator c) sends the message to the destination.


23.3.1 using a message converter

To facilitate the sending of domain model objects, Jmstemplate has different sending methods for the data content of a message, with a Java object as the parameter. The overloaded methods in Jmstemplate convertandsend () and Receiveandconvert () delegate processing to an instance of the Messageconverter interface. The interface defines a simple protocol for converting between a Java object and a JMS message. The default implementation Simplemessageconverter supports conversions between string,textmessage,byte[] and Bytesmesssage,java.util.map and Mapmessage. By using this converter, you and your application code can concentrate on the business objects being sent or the business objects that are accepted with JMS, without having to focus on the details of how a JMS message is represented.


Contains the Mapmessageconverter sandbox, using reflection to convert between JavaBean and Mapmessage. Other things you can do for yourself are converters that use a package of existing XML, such as Jaxb,castor,xmlbeans, or XStream, to create a textmessage that represents an object.


To adjust the settings of the message properties, the message header and the body cannot be encapsulated inside a translator class, Messagepostprocessor, allowing you to access the message before it is converted and sent. The following example shows how to modify a message header and a property before Java.util.Map is converted to a message.


[Java]   View plain  copy   public void sendwithconversion ()  {        map map = new hashmap ();        map.put ("Name",  "Mark");       map.put ("Age",  new integer (47 ));       jmstemplate.convertandsend ("Testqueue", map, new  Messagepostprocessor ()  {           public

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.