Brief Introduction
Actual combat one, the actual combat two introduced the basic concept and the configuration way of ACTIVEMQ.
This article introduces a way to use spring to send, consume topic, queue type messages. Do not understand the topic and queue of Google.
As shown in the illustration, topic and queue represent a topic and a queue message channel respectively. Topicmessageproducer sends messages to topic, Topicconsumera and Topicconsumerb consume messages from the topic. Queuemessageproducer sends a message to the queue, Queueconsumer consumes messages from the queue Spring consolidates JMS
Like the support of ORM, Web, spring also supports JMS, offering a number of convenient ways to consolidate JMS to existing projects. This article is mainly about combat, is so first from the configuration, spring configuration JMS basically need 8 parts. ConnectionFactory. The connection to the JMS server can be 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. The message converter. MessageProducer. Message producer. Messageconsumer. Message consumers. MessageListener. Message Listener Messagelistenercontainer. Message Listening container
The above 8 sections are described in the following example. 1. ConnectionFactory XML code <amq:connectionfactory id= "Jmsconnectionfactory" brokerurl= "Vm://localhost" />
brokerurl is the address of the ACTIVEMQ server that you want to connect to, and ACTIVEMQ provides a variety of brokerurl that you can see collectively. We use nested ACTIVEMQ server. Configuration is as follows, this configuration uses the storage mechanism of the message, the server restarts also does not lose the message. XML code <!-- embedded ActiveMQ Broker --> <amq:broker usejmx= "false" persistent= "true" > <amq:persistenceAdapter> <amq:amqpersistenceadapter directory= "D:/AMQ"/> </amq:persistenceAdapter> <amq:transportConnectors> <amq:transportconnector uri= "tcp://localhost:61616" /> &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; <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--> <!--use topic way--> <amq:topic "top IC "physicalname=" Jms-test-topic "/> <!--using the queue method--> <amq:queue name=" Queue physicalname= "Jms-test-que UE "/>3. Jmstemplate XML 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 --> <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 conversion function of the message. The implementation of Defaultmessageconverter is shown in the annex. The XML code <bean id= "Defaultmessageconverter" class= "Com.andyao.activemq.DefaultMessageConverter"/> 5. MessageProducer
The
instance has two message producers, the message producer is Pojo, see attachment. XML code <!-- POJO which send Message uses Spring jmstemplate --> <bean id= "TopicMessageProducer" class= "Com.andyao.activemq.TopicMessageProducer" > < Property name= "template" ref= "jmstemplate" /> <property name= "Destination" ref= "TOPIC" /> </bean>