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.
- Topicmessageproducer sends messages to topic, Topicconsumera and Topicconsumerb consume messages from topic.
- 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.
- ConnectionFactory. The connection to the JMS server can be an external JMS server, or you can use embedded ActiveMQ Broker.
- Destination. There are two ways of topic and queue.
- Jmstemplate. The JMS template provided by spring.
- Messageconverter. Message converters.
- MessageProducer. Message producers.
- Messageconsumer. Message consumers.
- MessageListener. Message Listener
- Messagelistenercontainer. Message Listening container
The above 8 sections are described in the following example.
1. Connectionfactoryxml Code
- <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
- <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:
- You can have one or more message producers send messages to the same destination.
- The queue type can have only one message consumer.
- The topic type can have multiple message consumers.
- Each consumer corresponds to a messagelistener and a messagelistenercontainer.
ActiveMQ5.0 Three: Use spring to send, consume topic and queue messages