first, the project must rely on the import, the basic spring package is needless to say, in addition to these need to import a few 2, for the integration
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.0</version>
</dependency>
after the import is complete, first write the XML file Spring-rabbitmq.xml file contents of Spring Consolidated RABBITMQ
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmln
S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit= "Http://www.springframework.org/schema/rabbit" xmlns:context= "Http://www.springframework.org/schema/context" 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.spri
Ngframework.org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema/rabbit Http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd "> <!--define a connection factory for creating connections and so on &L T;rabbit:connection-factory id= "ConnectionFactory" Username= "Guest" password= "Guest" host= "localhost" port= "5672"/ > <!--defining Exchange,queue in Admin,producer automatically takes advantage of theAdmin automatically generated in spring--<rabbit:admin id= "connadmin" connection-factory= "ConnectionFactory"/> <!-- Define the RABBITMQ template for receiving and sending messages--<rabbit:template id= "amqptemplate" connection-factory= "ConnectionFactory" Exchange= "Hjexchange"/> <rabbit:template id= "amqptemplates" connection-factory= "ConnectionFactory" exchange= "Directexchange"/> <!--use admin to define the queue, Spring automatically creates queue name persistence based on the following definition whether or not to delete declared-by when the exclusive queue is not in use. .. (not yet quite clear, hope big God pointing)--<rabbit:queue name= "Test1" auto-delete= "false" declared-by= "Connadmin" durable= "true" Exclusive= "false" auto-declare= "true"/> <rabbit:queue name= "Test2" auto-delete= "false" declared-by= "Connadmin "Durable=" true "Exclusive=" false "auto-declare=" true "/> <rabbit:queue name=" Test3 "auto-delete=" false "declare D-by= "Connadmin" durable= "true" Exclusive= "false" auto-declare= "true"/> <!--define Exchange and bind the queue to Exchange,
Set routing Key--<!--name to Echange name-- <rabbit:topic-exchange name= "Hjexchange" durable= "true" declared-by= "Connadmin" auto-delete= "false" > < Rabbit:bindings> <!--pattern is routing Key, queue is a bound queue-and <rabbit:binding pattern= "topickey.#" Queu E= "Test1" ></rabbit:binding> <rabbit:binding pattern= "topickeys.#" queue= "Test3" ></rabbit: Binding> </rabbit:bindings> </rabbit:topic-exchange> <!--ditto--<rabbit:direct-exchange NA Me= "Directexchange" durable= "true" declared-by= "Connadmin" auto-delete= "false" > <rabbit:bindings> < Rabbit:binding key= "Directkeys" queue= "test2" ></rabbit:binding> </rabbit:bindings> </rabbit: Direct-exchange> <!--define consumers, consume messages--<bean id= "Directconsumer" class= " Com.demo.rabbitmq.consumer.DirectConsumer "></bean> <bean id=" Topicconsumer "class=" Com.demo.rabbitmq.consumer.TopicConsumer "></bean> <bean id=" Topicsconsumer "class=" Com.demo.rabbitmq.consumeR.topicsconsumer "></bean> <!--turn on listening, it can also be understood as: to bind the consumer and the queue, so that when the queue has a message, it will be the binding of consumers to spend, can be regarded as a designated consumer to listen to the specified Queue. When a message arrives, inform the consumer to spend--<rabbit:listener-container connection-factory= "ConnectionFactory" > <!-- Inject bean, specify queue--<rabbit:listener ref= "Topicconsumer" queues= "test1"/> <rabbit:listener ref= " Directconsumer "queues=" test2 "/> <rabbit:listener ref=" Topicsconsumer "queues=" Test3 "/> </rabbit:
Listener-container> </beans>
This file needs to be noted in a place
XML Header declaration, you need to join the namespace and path: Http://www.springframework.org/schema/rabbit
xmlns:rabbit= "Http://www.springframework.org/schema/rabbit"
The RABBITMQ template that is declared for sending messages and receiving data can be bound to many of the same types of exchange when you bind Exchange to a queue. However, you cannot bind a template to an exchange of a type. If you want to bind an exchange of an RABBITMQ type, you need a non-pass template.
And then the Spring-mvc.xml.
<?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:cont ext= "Http://www.springframework.org/schema/context" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/beans HT Tp://www.springframework.org/schema/beans/spring-beans-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/context HTTP://WWW.S Pringframework.org/schema/context/spring-context-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SP Ringframework.org/schema/aop/spring-aop-4.0.xsd Http://www.springframework.org/schema/tx Http://www.springframew ork.org/schema/tx/spring-tx-4.0.xsd "> <import resource=" classpath:spring-rabbitmq.xml "/> <!--annotation maps, configuring some Messageconverter- <mvc:annotation-driven> <mvc:message-converters register-defaults= "true" > <bean class= "ORG.SPRINGF
Ramework.http.converter.StringHttpMessageConverter "> <constructor-arg value=" UTF-8 "/> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--annotations automatically scan-<context:component- Scan base-package= "Com.demo.rabbitmq.consumer"/> <bean class= "Org.springframework.web.servlet.view.Interna Lresourceviewresolver "> <!--here the configuration I understand is to automatically add a prefix and suffix to the string of the method return of the back action, which becomes an available URL address--< Property name= "prefix" value= "/web-inf/jsp/"/> <property name= "suffix" value= ". jsp"/> <pro Perty name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/> </bean> </beans>
Then create a message producer
MessageProducer
Package Com.demo.rabbitmq.consumer;
Import Javax.annotation.Resource;
Import Org.apache.log4j.Logger;
Import org.springframework.amqp.core.AmqpTemplate;
Import Org.springframework.stereotype.Service;
@Service public
class MessageProducer {
private Logger log = Logger.getlogger (Messageproducer.class);
@Resource (name= "Amqptemplate")
private amqptemplate amqptemplate;
@Resource (name= "amqptemplates")
private amqptemplate amqptemplates;
public void Send (Object message) {
log.info ("Send message:" + message);
Amqptemplate.convertandsend ("Topickey.name", message);
Amqptemplate.convertandsend ("Topickeys.name", message);
Amqptemplates.convertandsend ("Directkeys", message);
}
}
Define Consumer
Topic type of consumer 2, one of the direct types
1,topicconsumer
Package Com.demo.rabbitmq.consumer;
Import Org.apache.log4j.Logger;
Import Org.springframework.amqp.core.Message;
Import Org.springframework.amqp.core.MessageListener;
public class Topicconsumer implements messagelistener{
private Logger log = Logger.getlogger (Topicconsumer.class);
@Override public
void onMessage (Message message) {
log.info ("------> Topicconsumer received messages as:" + message );
}
}
2,topicsconsumer
Package Com.demo.rabbitmq.consumer;
Import Org.apache.log4j.Logger;
Import Org.springframework.amqp.core.Message;
Import Org.springframework.amqp.core.MessageListener;
public class Topicsconsumer implements messagelistener{
private Logger log = Logger.getlogger ( Topicsconsumer.class);
@Override public
void onMessage (Message message) {
log.info ("------> Topicsconsumer received messages as:" + message);
}
}
3,directconsumer
Package Com.demo.rabbitmq.consumer;
Import Org.apache.log4j.Logger;
Import Org.springframework.amqp.core.Message;
Import Org.springframework.amqp.core.MessageListener;
public class Directconsumer implements messagelistener{
private Logger log = Logger.getlogger (directconsumer.class );
@Override public
void onMessage (Message message) {
log.info ("------> Directconsumer received messages as:" + message); c8/>}
}
The consumer must implement the MessageListener interface, overriding the OnMessage method, because we configured the listener in the Spring-rabbitmq.xml file, so when a message exists in the queue that the class listens to, The method is called. You can then do further work.
Then write a test class
Package springtest;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import Com.demo.rabbitmq.consumer.MessageProducer;
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath:spring-mvc.xml"})
public class Castspringrabbitmq {
@Autowired
private messageproducer messageproducer;
@Test public
void Test () throws interruptedexception {
String message = ' My name is Sun Wu Kong ';
int a = +;
while (a--> 0) {
messageproducer.send (message);
Thread.Sleep (+);}}}
To run the project, the console output is as follows:
The above is a simple example of spring integration RABBITMQ. If the error, please leave a message to point out, thank you very much.