Spring sends a receive ACTIVEMQ message using Mappingjackson2messageconverter

Source: Internet
Author: User

One, Spring uses jmstemplate to simplify access to JMS

In Java access to JMS queues, there will be a large number of check-type exceptions using the default JMS support. With spring support, you can convert all of the JMS check-type exceptions to run-time non-check exceptions. And in spring, specify the sending and receiving destinations by configuring the Defaultdestinationname of the jmsconnectionfactory.

The following is the ACTIVEMQ connection factory configuration:

1     @Bean 2      Public activemqconnectionfactory getamqfactory () {3         New activemqconnectionfactory (); 4         Mqconnectionfactory.setbrokerurl ("tcp://59.110.231.87:61616"); 5         Mqconnectionfactory.settrustedpackages (Arrays.aslist ("Com.edoctor.bean")); 6         return mqconnectionfactory; 7     }

Here is the configuration of the jmstemplate:

1     @Bean2      Publicjmstemplate getjmstemplate (activemqconnectionfactory CF, Messageconverter messageconverter) {3Jmstemplate jmstemplate =Newjmstemplate (CF);4Jmstemplate.setdefaultdestinationname ("Com.demo.testActiveMQ");5 Jmstemplate.setmessageconverter (messageconverter);6         //Pubsubdomain = True for queue mode, FALSE for subscription release mode7Jmstemplate.setpubsubdomain (false);8         returnjmstemplate;9}

I am a bean configured with pure Java annotations, which is similar to XML-based and can be searched by itself.

The above fields have the following meanings:

Setdefaultdestinationname: Sets the queue name of the ACTIVEMQ, of course, if the pubsubdomain below is true, the subject name

Setmessageconverter: Set the ACTIVEMQ message converter, the default is not to write the words is the use of spring simplemessageconverter

Setpubsubdomain: The value true represents the template for the queue, and false for the subject

Types of message converters for 2.Spring

Spring's own message converters can greatly simplify the reading and writing of messages, all of which are located in the Org.springframework.jms.support.converter package.

Message Converters Function
Mappingjacksonmessageconverter

Using the Jackson JSON library to convert between messages and JSON formats

Mappingjackson2messageconverter

Using the Jackson 2 JSON library to convert between messages and JSON formats

Marshallingmessageconverter

Using the JAXB library to convert between messages and XML formats

Simplemessageconverter

Implements the conversion between string and TextMessage, byte array and bytes
Conversion between message, map and Mapmessage
and the conversion between serializable object and ObjectMessage

By default, Jmstemplate uses Simplemessage Converter in the Convertandsend () method. However, we can override this behavior by declaring the message converter as a bean and injecting it into the Messageconverter property of Jmstemplate. For example, if you want to use a JSON message, you can declare a mappingjackson2messageconverter bean.

Third, configure the Mappingjackson2messageconverter bean

The above jmstemplate has been successfully injected into the activemqconnectionfactory, the following will be injected into our messageconverter.

Because using the default Simplemessageconverter object object, you must serialize the objects, and if the object contains wrapper classes such as integers, it will not be possible to serialize, so I intend to use JSON-based Mappingjackson2messageconverter to serialize the sending and receiving of objects, which is automatically serialized. However, in the online access to the relevant documents, found that there is almost no Chinese introduction configuration Mappingjackson2messageconverter, so I hope to write this configuration to help everyone.

Detailed configuration of Mappingjackson2messageconverter in Javadoc: https://docs.spring.io/spring/docs/current/javadoc-api/org/ Springframework/jms/support/converter/mappingjackson2messageconverter.html

For the documentation and introduction, the above links are described in detail, the main mention is:

This means that you need to configure this property if you need to accept the message in the format of object, and this property needs to refer to typeidmappings.

Let's take a look at the map description:

This map has a string value of Key,class, and here the key is the corresponding Typeid,value is the class you need to serialize. So just building such a map allows you to accept the class object-type message from MQ. Here is my Pojo ( note that the object to send the received in MQ must have a parameterless constructor )

1  Public classTESTJMS {2     PrivateString name;3     PrivateInteger age;4     PrivateString Email;5 6      Publictestjms () {7     }8 9      Publictestjms (string name, Integer age, string email) {Ten          This. Name =name; One          This. Age =Age ; A          This. email =email; -     } -  the      PublicString GetName () { -         returnname; -     } -  +      Public voidsetName (String name) { -          This. Name =name; +     } A  at      PublicInteger getage () { -         returnAge ; -     } -  -      Public voidsetage (Integer age) { -          This. Age =Age ; in     } -  to      PublicString Getemail () { +         returnemail; -     } the  *      Public voidsetemail (String email) { $          This. email =email;Panax Notoginseng     } -  the @Override +      PublicString toString () { A         return"Testjms{" + the"Name=" + name + ' \ ' + +", age=" + Age + -", email= '" + email + "\" + $‘}‘; $     } -}

And this is the bean definition injected into Jmstemplate's mappingjackson2messageconverter.

1 @Bean2      PublicMappingjackson2messageconverter Getjacksonmessageconverter () {3Mappingjackson2messageconverter converter =NewMappingjackson2messageconverter ();4 Converter.settargettype (messagetype.text);5         //defines a map of typeid to class6map<string, class<?>> typeidmap =NewHashmap<>();7Typeidmap.put ("Testjms", TESTJMS.class);8 converter.settypeidmappings (typeidmap);9         //set the name of the typeID sent to the queueTenConverter.settypeidpropertyname ("TESTJMS"); OneConverter.setencoding ("UTF-8"); A         returnConverter; -}

Through this injection, the class object format is implemented without the need to serialize the sending and receiving of objects

The complete activemq Java annotation-based configuration code is as follows:

1 ImportCom.test.bean.TestJMS;2 Importorg.apache.activemq.ActiveMQConnectionFactory;3 ImportOrg.springframework.context.annotation.Bean;4 Importorg.springframework.context.annotation.Configuration;5 Importorg.springframework.jms.core.JmsTemplate;6 ImportOrg.springframework.jms.support.converter.MappingJackson2MessageConverter;7 ImportOrg.springframework.jms.support.converter.MessageConverter;8 ImportOrg.springframework.jms.support.converter.MessageType;9 Ten Importjava.util.Arrays; One ImportJava.util.HashMap; A ImportJava.util.Map; -  - @Configuration the  Public classMqconfig { -  - @Bean -      Publicactivemqconnectionfactory getamqfactory () { +Activemqconnectionfactory mqconnectionfactory =Newactivemqconnectionfactory (); -Mqconnectionfactory.setbrokerurl ("tcp://59.110.231.87:61616"); +Mqconnectionfactory.settrustedpackages (Arrays.aslist ("Com.edoctor.bean")); A         returnmqconnectionfactory; at     } -  - @Bean -      Publicjmstemplate getjmstemplate (activemqconnectionfactory CF, Messageconverter messageconverter) { -Jmstemplate jmstemplate =Newjmstemplate (CF); -Jmstemplate.setdefaultdestinationname ("EDoctor.JMSTemplate.queue2"); in Jmstemplate.setmessageconverter (messageconverter); -         //Pubsubdomain = True for queue mode, FALSE for subscription release mode toJmstemplate.setpubsubdomain (false); +         returnjmstemplate; -     } the  * @Bean $      PublicMappingjackson2messageconverter Getjacksonmessageconverter () {Panax NotoginsengMappingjackson2messageconverter converter =NewMappingjackson2messageconverter (); - Converter.settargettype (messagetype.text); themap<string, class<?>> typeidmap =NewHashmap<>(); +Typeidmap.put ("Testjms", TESTJMS.class); A converter.settypeidmappings (typeidmap); theConverter.settypeidpropertyname ("TESTJMS"); +Converter.setencoding ("UTF-8"); -         returnConverter; $     } $  -}
Iv. Summary
Mappingjackson2messageconverter can effectively implement MQ's sending and receiving serialization without the need to manually serialize Pojo to implement the serializable interface. JSON-based parsing also adapts to mainstream technology. Because stepping on more pits, so specifically to stay this blog, if the wrong place, but also hope to point out a lot.
There are questions welcome message, Thank you!

Spring sends a receive ACTIVEMQ message using Mappingjackson2messageconverter

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.