1.RabbitMQ Introduction
RABBITMQ is a popular open source Message Queuing system, developed in Erlang language. RABBITMQ is the standard implementation of the AMQP (Advanced Message Queuing protocol).
Official website: http://www.rabbitmq.com/
2.Spring Integrated RabbitMQ2.1 maven Configuration
//pom.xml<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.4.5.RELEASE</version> </dependency>
2.2 RABBMITMQ configuration file
//rabbitmq-config.propertiesmq.host=127.0.0.1mq.username=testmq.password=123456mq.port=5672mq.vhost=testmq
2.3 Spring Configuration
Application-mq.xml<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/ 2001/xmlschema-instance "xmlns:rabbit=" Http://www.springframework.org/schema/rabbit "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www . springframework.org/schema/rabbit Http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd "> < DESCRIPTION>RABBITMQ Connection Service Configuration </description> <!--connection configuration-<rabbit:connection-factory id= "connectionf Actory "host=" ${mq.host} "Username=" ${mq.username} "password=" ${mq.password} "port=" ${mq.port} "virtual-host=" ${ Mq.vhost} "/> <rabbit:admin connection-factory=" ConnectionFactory "/> <!--spring Template declaration--< Rabbit:template exchange= "Amqpexchange" id= "Amqptemplate" connection-factory= "ConnectionFactory" Message-converter = "Jsonmessageconverter"/> <!--Message object JSON conversion class--&Gt <bean id= "Jsonmessageconverter" class= " Org.springframework.amqp.support.converter.Jackson2JsonMessageConverter "/> </beans>
3. Use RabbitMQ3.1 in spring to declare a Message Queuing queue
//application-mq.xml<rabbit:queue id="test_queue_key" name="test_queue_key" durable="true" auto-delete="false" exclusive="false" />
Description
Durable: whether to persist
Exclusive: Only private queues that the creator can use, automatically deleted after disconnection
Auto_delete: Automatically delete queues when all consumer client connections are disconnected
3.2 Switch Definition
//application-mq.xml<rabbit:direct-exchange name="test-mq-exchange" durable="true" auto-delete="false" id="test-mq-exchange"> <rabbit:bindings> <rabbit:binding queue="test_queue_key" key="test_queue_key"/> </rabbit:bindings></rabbit:direct-exchange>
Description
Rabbit:direct-exchange: Defines the Exchange Mode as direct, meaning that the message is not forwarded until it exactly matches a specific routing key .
Rabbit:binding: Set Message queue matching key
3.3 Sending a message producer
//MQProducer.javapublic interface MQProducer { /** * 发送消息到指定队列 * @param queueKey * @param object */ public void sendDataToQueue(String queueKey, Object object);}
@Servicepublic class MQProducerImpl implements MQProducer { @Autowired private AmqpTemplate amqpTemplate; private final static Logger LOGGER = Logger.getLogger(MQProducerImpl.class); /* (non-Javadoc) * @see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object) */ @Override public void sendDataToQueue(String queueKey, Object object) { try { amqpTemplate.convertAndSend(queueKey, object); } catch (Exception e) { LOGGER.error(e); } }}
Description
Convertandsend: Converting a Java object to a message sent to a switch that matches key exchange, where JSON conversion is configured, this is the form of converting a Java object into a JSON string. Original: Convert a Java object to an AMQP Message and send it to a default exchange with a specific routing key.
3.4 Receiving Messages asynchronously consumer
Defining listeners
//QueueListenter.java@Componentpublic class QueueListenter implements MessageListener { @Override public void onMessage(Message msg) { try{ System.out.print(msg.toString()); }catch(Exception e){ e.printStackTrace(); } }}
Monitoring Configuration
//application-mq.xml<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"> <rabbit:listener queues="test_queue" ref="queueListenter"/></rabbit:listener-container>
Description
Queues: Listener queue, multiple words separated by commas (,)
Ref: Listener
3.5 JUnit Test
//TestQueue.java@RunWith(value = SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:/ApplicationContext/ApplicationContext-mq.xml"})public class TestQueue{ @Autowired MQProducer mqProducer; final String queue_key = "test_queue"; @Test public void send(){ Map<String,Object> msg = new HashMap()<>; msg.put("data","hello,rabbmitmq!"); mqProducer.sendDataToQueue(query_key,msg); }}
Running the test program, run with JUnit, sends a message to Test_queue, which, after the listener hears the message, prints the message.
At this point, the spring and RABBMITMQ integration, configuration, and use have been completed.
Message Queuing RABBITMQ and spring