ActiveMQ5.0 Three: Use spring to send, consume topic and queue messages

Source: Internet
Author: User
Tags amq

Actual combat one, the actual combat two introduced the basic concept of ACTIVEMQ and configuration method.

This article will show you how to use spring to send, consume topic, queue type messages with an example. Google, who does not understand topic and queue.

Shown, topic and queue represent a topic and a queue message channel respectively.

    1. Topicmessageproducer sends messages to topic, Topicconsumera and Topicconsumerb consume messages from topic.
    2. Queuemessageproducer sends a message to the queue, Queueconsumer consumes messages from the queue
Spring consolidates JMS

Like support for ORM, the Web, spring also supports JMS, which provides many convenient ways to integrate JMS into existing projects. This article mainly tells the actual combat, is therefore starts from the configuration first, spring configures the JMS to basically need 8 parts.

    1. ConnectionFactory. The connection to the JMS server can be an external JMS server, or you can use embedded ActiveMQ Broker.
    2. Destination. There are two ways of topic and queue.
    3. Jmstemplate. The JMS template provided by spring.
    4. Messageconverter. Message converters.
    5. MessageProducer. Message producers.
    6. Messageconsumer. Message consumers.
    7. MessageListener. Message Listener
    8. Messagelistenercontainer. Message Listening container

The above 8 sections are described in the following example.

1. Connectionfactoryxml Code
    1. <amq:connectionfactory id="jmsconnectionfactory" brokerurl="vm://localhost" />

Brokerurl refers to the address of the ACTIVEMQ server to be connected, ACTIVEMQ provides a variety of brokerurl, collectively see the documentation. Generally we use nested ACTIVEMQ servers. Configured as follows, this configuration uses the storage mechanism of the message, and the server restarts without losing the message.

XML code
<!--  embedded ActiveMQ Broker-to-      <amq:broker usejmx= "false" persistent= "true" >          <amq: persistenceadapter>              <amq:amqpersistenceadapter directory= "d:/amq"/>          </amq: persistenceadapter>          <amq:transportConnectors>              <amq:transportconnector uri= "tcp://localhost : 61616 "/>                         <amq:transportconnector uri=" vm://localhost:0 "/>          </amq:transportConnectors>      </amq:broker>

2. Destination

In the example we used two kinds of destination

XML code
<!--  ActiveMQ destinations  -  <!--  using topic mode-  <amq:topic name= "topic" Physicalname= "Jms-test-topic"/>  <!--  using queue mode--  <amq:queue name= "queue" physicalname= " Jms-test-queue "/>  

3. Jmstemplatexml Code
<!--  Spring jmstemplate config---      <bean id= "jmstemplate" class= " Org.springframework.jms.core.JmsTemplate ">          <property name=" ConnectionFactory ">              <!--  Lets wrap in a pool to avoid creating a connection per send              -to <bean class= "org.springframework.jms.connection. Singleconnectionfactory ">                  <property name=" targetconnectionfactory "ref=" Jmsconnectionfactory "/>              </bean>          </property>          <!--custom Messageconverter--          <property name= " Messageconverter "ref=" Defaultmessageconverter "/>      </bean>  

4. Messageconverter

Messageconverter implements the Org.springframework.jms.support.converter.MessageConverter interface, which provides the ability to convert messages. The implementation of Defaultmessageconverter is shown in the annex.

XML code
    1. <Bean id= "defaultmessageconverter" class="Com.andyao.activemq.DefaultMessageConverter" />
5. MessageProducer

The instance has two message producers, the message producers are Pojo, and the implementation is shown in the annex.

XML code
<!--POJO which send Message uses  Spring jmstemplate-to      <bean id= "Topicmessageproducer" class= " Com.andyao.activemq.TopicMessageProducer ">          <property name=" template "ref=" Jmstemplate "/>          < Property name= "Destination" ref= "TOPIC"/>      </bean>      <bean id= "Queuemessageproducer" class= " Com.andyao.activemq.QueuMessageProducer ">          <property name=" template "ref=" Jmstemplate "/>          < Property name= "Destination" ref= "QUEUE"/>      

6. Messageconsumer

Topic channel has two messages consumer, queue has a message consumer

XML code
<!--  Message driven POJO (MDP)-      <!--consumer1 for topic A--      <bean id= "Topicconsumera" cl ass= "Com.andyao.activemq.TopicConsumerA"/>      <!--consumer2 for topic A---      <bean id= " Topicconsumerb "class=" Com.andyao.activemq.TopicConsumerB "/>      <!--consumer for queue--and      <bean Id= "Queueconsumer" class= "Com.andyao.activemq.QueueConsumer"/>  

7. MessageListener

Each message consumer corresponds to a MessageListener

XML code
<bean id= "Topiclistenera" class= "Org.springframework.jms.listener.adapter.MessageListenerAdapter" > <cons Tructor-arg ref= "Topicconsumera"/> <!--May is other method--and <property name= "Defaultlis Tenermethod "value=" Receive "/> <!--custom Messageconverter define--<property name=" mess Ageconverter "ref=" Defaultmessageconverter "/> </bean> <bean id=" Topiclistenerb "class=" org.spring          Framework.jms.listener.adapter.MessageListenerAdapter "> <constructor-arg ref=" Topicconsumerb "/> <!--May is other method-<property name= "Defaultlistenermethod" value= "Receive"/> &L t;!       --Custom Messageconverter define--<property name= "Messageconverter" ref= "Defaultmessageconverter"/> </bean> <bean id= "Queuelistener" class= "Org.springframework.jms.listener.adapter.MessageListenerAdap ter "> < Constructor-arg ref= "Queueconsumer"/> <!--May is other method--and <property name= "Defaul Tlistenermethod "value=" Receive "/> <!--custom Messageconverter define--<property name="   Messageconverter "ref=" Defaultmessageconverter "/> </bean>

8. Messagelistenercontainer

Several MessageListener have several messagelistenercontainer

XML code
<bean id= "Topiclistenercontainera" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" > <property name= "connectionfactory" ref= "jmsconnectionfactory"/> <property name= "Destinati On "ref=" TOPIC "/> <property name=" MessageListener "ref=" Topiclistenera "/> </bean> &l          T;bean id= "Topiclistenercontainerb" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" > <property name= "ConnectionFactory" ref= "jmsconnectionfactory"/> <property name= "destination" ref= "TOPI C "/> <property name=" MessageListener "ref=" Topiclistenerb "/> </bean> <bean ID = "Queuelistenercontainer" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" > <prope          Rty name= "ConnectionFactory" ref= "jmsconnectionfactory"/> <property name= "destination" ref= "QUEUE"/> <property name= "MessagelistEner "ref=" Queuelistener "/> </bean>   

Summary

To write the spring configuration file, MessageProducer, Messageconsumer,messagelistener,messagelistenercontainer a few places to figure out:

    1. You can have one or more message producers send messages to the same destination.
    2. The queue type can have only one message consumer.
    3. The topic type can have multiple message consumers.
    4. Each consumer corresponds to a messagelistener and a messagelistenercontainer.

ActiveMQ5.0 Three: Use spring to send, consume topic and queue messages

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.