The code samples in this article are written in the spring integration environment and are tested.
The pom file needs to be joined by the spring integrated RABBITMQ dependency:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId> spring-rabbit</artifactid>
<version>1.6.3.RELEASE</version>
</dependency>
first, the RABBITMQ topic mode:
Graphic:
Usage scenario: The sending side does not just send a message by a fixed routing key, but instead sends it by the string "match", the same is true for the receiving end.
XML configuration for send-side spring:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:jee=" Http://www.springframework.org/schema/jee "xmlns:p="/HTTP/ www.springframework.org/schema/p "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:task=" Http://www.springframework.org/schema/task "xmlns:rabbit=" Http://www.springframework.org/schema/rabbit "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema/jee
Http://www.springframework.org/schema/jee/spring-jee-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.0.xsd/HTTP Www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd/HTTP
Www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd "> <!--RABBITMQ configuration--<bean id= "rabbitconnectionfactory" class= "Org.springframework.amqp.rabbit.connectio N.cachingconnectionfactory "> <constructor-arg value=" 192.168.1.11 "/> <property name=" username " value= "root"/> <property name= "password" value= "lee13233"/> <property name= "Channelcachesize" Value= "8"/> <property name= "port" value= "5672" ></PROPERTY> </bean> <rabbit:admin connection-factory= "Rabbitconnectionfactory"/> <!--autodelete: Automatically delete durabl E: Persistent---<rabbit:queue name= "Test123queue" durable= "true"/> <rabbit:queue name= "Test321queue" durabl E= "true"/> <!--topic Theme--<rabbit:topic-exchange name= "Leo.pay.topic.exchange" xmlns= "Http://www.sprin Gframework.org/schema/rabbit "durable=" true "> <bindings> <binding queue=" Test123queue "pattern=" *.*.test123 "/> <binding queue=" Test321queue "pattern=" test321.# "/> </bindings> </rab bit:topic-exchange> <!--creating rabbittemplate message Template Classes--<bean id= "rabbittemplate" class= "Org.springframew Ork.amqp.rabbit.core.RabbitTemplate "> <constructor-arg ref=" rabbitconnectionfactory "></ Constructor-arg> </bean> </beans>
Java code on the sending side:
@Test public
void Testrabbitmq () throws Exception {
rabbittemplate rabbittemplate = (rabbittemplate) Leocontext.getcontext (). Getapplication (). Getbean ("Rabbittemplate");
The second parameter is the value of the route key (Routingkey), and when the route can be test321.hello.test123, both consumption queues can receive messages, and when the values are TEST321.HELLO.AAA, only the test321.# is bound Queue to receive the message, the value is ta1.hello.test123, only the queue bound to *.*.test123 can receive the message for
(int i = 1; i <=; i++) {
String str = "Hello" + i;
Rabbittemplate.send ("Leo.pay.topic.exchange", "test321.hello.test123", New Message (Str.getbytes (), New Messageproperties ()));
}
}
The XML configuration of the receive side (the receive end I configured is not the same project as the sender) for spring:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:jee=" Http://www.springframework.org/schema/jee "xmlns:p="/HTTP/ www.springframework.org/schema/p "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:task=" Http://www.springframework.org/schema/task "xmlns:rabbit=" Http://www.springframework.org/schema/rabbit "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema/jee
Http://www.springframework.org/schema/jee/spring-jee-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.0.xsd/HTTP Www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd/HTTP
Www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd " Default-autowire= "ByName" > <!--rabbitmq configured-<bean id= "rabbitconnectionfactory" class= "org.sp
Ringframework.amqp.rabbit.connection.CachingConnectionFactory "> <constructor-arg value=" 192.168.1.11 "/> <property name= "username" value= "Leo"/> <property name= "password" value= "lee31211"/> & Lt;property name= "Channelcachesize" value= "8"/> <property name= "Port" vaLue= "5672" ></property> </bean> <rabbit:admin connection-factory= "Rabbitconnectionfactory"/> <rabbit:queue name= "Test123queue" durable= "true"/> <rabbit:queue name= "Test321queue" durable= "true"/>
; <!-- This refers to the route leo.pay.topic.exchange with two queues in a piece, can also be manually bound on the console of RABBITMQ, manual binding, the code can be omitted, in fact, the sender has been bound, there is no need to bind, so the code can be omitted-- > <!--<rabbit:topic-exchange name= "Leo.pay.topic.exchange" xmlns= "http://www.springframework.org/schema/ Rabbit "durable=" true "> <!--<bindings>-<!--<binding queue=" Test123queue "Pat tern= "test123.*"/>-<!--<binding queue= "Test321queue" pattern= "test321.*"/>--<!-- </bindings> <!--</rabbit:topic-exchange>-<!--start two queues for listening (consumer)--<b Ean id= "Detailqueueconsumer" class= "Com.leo.website.cousumer.DetailQueueConsumer" ></bean> <bean id= " Testqueueconsumer "class=" Com.leO.website.cousumer.testqueueconsumer ></bean> <!--add two queues to the listening container, and each queue listens to one listener--<rabbit:lis Tener-container connection-factory= "Rabbitconnectionfactory" concurrency= "8" > <rabbit:listener queues= "Test 123queue "ref=" Detailqueueconsumer "method=" OnMessage "/> <rabbit:listener queues=" Test321queue "ref=" TestQue Ueconsumer "method=" OnMessage "/> </rabbit:listener-container> </beans>
Receive-side Java code (only one listener is listed, the other is similar):
public class Detailqueueconsumer implements MessageListener {
@Override public
void onMessage (Message message) {
System.out.println ("Detailqueueconsumer:" + New String (Message.getbody ()));
}
}
Second, RABBITMQ's fanout mode (publisher subscriber mode):
Graphic:
Usage Scenario: Publish, Subscribe mode, send side send broadcast message, multiple receive end receive.
XML configuration for send-side spring:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:jee=" Http://www.springframework.org/schema/jee "xmlns:p="/HTTP/ www.springframework.org/schema/p "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:task=" Http://www.springframework.org/schema/task "xmlns:rabbit=" Http://www.springframework.org/schema/rabbit "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema/jee
Http://www.springframework.org/schema/jee/spring-jee-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.0.xsd/HTTP Www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd/HTTP
Www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd "> <!--RABBITMQ configuration--<bean id= "rabbitconnectionfactory" class= "Org.springframework.amqp.rabbit.connectio N.cachingconnectionfactory "> <constructor-arg value=" 192.168.1.12 "/> <property name=" username " value= "root"/> <property name= "password" value= "lee323"/> <property name= "Channelcachesize" V Alue= "8"/> <property name= "port" value= "5672" ></property>
</bean> <rabbit:admin connection-factory= "rabbitconnectionfactory"/> <rabbit:queue name= "test 123queue "durable=" true "/> <rabbit:queue name=" Test321queue "durable=" true "/> <rabbit:fanout-exchange Name= "Leo.pay.fanout.exchange" xmlns= "Http://www.springframework.org/schema/rabbit" durable= "true" > </ rabbit:fanout-exchange> <!--creating rabbittemplate message Template Classes--<bean id= "rabbittemplate" class= "ORG.SPRINGFR Amework.amqp.rabbit.core.RabbitTemplate "> <constructor-arg ref=" rabbitconnectionfactory "></ Constructor-arg> </bean> </beans>
Send-side Java code:
@Test public
void Testrabbitmq () throws Exception {
rabbittemplate rabbittemplate = (rabbittemplate) Leocontext.getcontext (). Getapplication (). Getbean ("Rabbittemplate");
Send data to the route named Leo.pay.fanout.exchange, as long as the client is bound together with the queue will receive related messages, this is similar to full-frequency broadcast, the sender regardless of the queue is who, all by the client to bind themselves, who need data who to bind their own processing queue 。 for
(int i = 1; i <=; i++) {
String str = "Hello" + i;
Rabbittemplate.send ("Leo.pay.fanout.exchange", "", New Message (Str.getbytes (), New Messageproperties ())
}
}
The XML configuration of the client spring (I am not configuring the client with the sending side under the same project):
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:jee=" Http://www.springframework.org/schema/jee "xmlns:p="/HTTP/ www.springframework.org/schema/p "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:task=" Http://www.springframework.org/schema/task "xmlns:rabbit=" Http://www.springframework.org/schema/rabbit "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema/jee
Http://www.springframework.org/schema/jee/spring-jee-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.0.xsd/HTTP Www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd/HTTP
Www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd " Default-autowire= "ByName" > <!--rabbitmq configured-<bean id= "rabbitconnectionfactory" class= "org.sp
Ringframework.amqp.rabbit.connection.CachingConnectionFactory "> <constructor-arg value=" 192.168.1.13 "/> <property name= "username" value= "root"/> <property name= "password" value= "lee2342"/> & Lt;property name= "Channelcachesize" value= "8"/> <property name= "Port" vaLue= "5672" ></property> </bean> <rabbit:admin connection-factory= "Rabbitconnectionfactory"/> <rabbit:queue name= "Test123queue" durable= "true"/> <rabbit:queue name= "test