1.RabbitMQ简介
RabbitMQ是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协议)的标准实现。
官网:http://www.rabbitmq.com/
2.Spring集成RabbitMQ
2.1 maven配置
1//pom.xml 2<dependency> 3 <groupId>com.rabbitmq</groupId> 4 <artifactId>amqp-client</artifactId> 5 <version>3.5.1</version> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework.amqp</groupId> 9 <artifactId>spring-rabbit</artifactId> 10 <version>1.4.5.RELEASE</version> 11 </dependency>
2.2 rabbmitmq配置文件
1//rabbitmq-config.properties 2mq.host=127.0.0.1 3mq.username=test 4mq.password=123456 5mq.port=5672 6mq.vhost=testmq
2.3 Spring配置
1//application-mq.xml 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:rabbit="http://www.springframework.org/schema/rabbit" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/rabbit 8 http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" > 9 10 <description>rabbitmq 连接服务配置</description> 11 <!-- 连接配置 --> 12 <rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}" virtual-host="${mq.vhost}"/> 13 <rabbit:admin connection-factory="connectionFactory"/> 14 15 <!-- spring template声明--> 16 <rabbit:template exchange="amqpExchange" id="amqpTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter" /> 17 18 <!-- 消息对象json转换类 --> 19 <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> 20</beans>
3. 在Spring中使用RabbitMQ
3.1 申明一个消息队列Queue
1//application-mq.xml 2<rabbit:queue id="test_queue_key" name="test_queue_key" durable="true" auto-delete="false" exclusive="false" />
说明:
durable:是否持久化
exclusive: 仅创建者可以使用的私有队列,断开后自动删除
auto_delete: 当所有消费客户端连接断开后,是否自动删除队列
3.2 交换机定义
1//application-mq.xml 2<rabbit:direct-exchange name="test-mq-exchange" durable="true" auto-delete="false" id="test-mq-exchange"> 3 <rabbit:bindings> 4 <rabbit:binding queue="test_queue_key" key="test_queue_key"/> 5 </rabbit:bindings> 6</rabbit:direct-exchange>
说明:
rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。
rabbit:binding:设置消息queue匹配的key
3.3 发送消息Producer
1//MQProducer.java 2public interface MQProducer { 3 /** 4 * 发送消息到指定队列 5 * @param queueKey 6 * @param object 7 */ 8 public void sendDataToQueue(String queueKey, Object object); 9} 10 11 12 13 14@Service 15public class MQProducerImpl implements MQProducer { 16 @Autowired 17 private AmqpTemplate amqpTemplate; 18 19 private final static Logger LOGGER = Logger.getLogger(MQProducerImpl.class); 20 /* (non-Javadoc) 21 * @see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object) 22 */ 23 @Override 24 public void sendDataToQueue(String queueKey, Object object) { 25 try { 26 amqpTemplate.convertAndSend(queueKey, object); 27 } catch (Exception e) { 28 LOGGER.error(e); 29 } 30 31 } 32}
说明:
convertAndSend:将Java对象转换为消息发送到匹配Key的交换机中Exchange,由于配置了JSON转换,这里是将Java对象转换成JSON字符串的形式。原文:Convert a Java object to an Amqp Message and send it to a default exchange with a specific routing key.
3.4 异步接收消息Consumer
定义监听器
1//QueueListenter.java 2@Component 3public class QueueListenter implements MessageListener { 4 5 @Override 6 public void onMessage(Message msg) { 7 try{ 8 System.out.print(msg.toString()); 9 }catch(Exception e){ 10 e.printStackTrace(); 11 } 12 } 13 14}
监听配置
1//application-mq.xml 2<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"> 3 <rabbit:listener queues="test_queue" ref="queueListenter"/> 4</rabbit:listener-container>
说明:
queues:监听的队列,多个的话用逗号(,)分隔
ref:监听器
3.5 JUnit测试
1//TestQueue.java 2@RunWith(value = SpringJUnit4ClassRunner.class) 3@ContextConfiguration(locations = { 4 "classpath:/ApplicationContext/ApplicationContext-mq.xml"}) 5 6public class TestQueue{ 7 @Autowired 8 MQProducer mqProducer; 9 10 final String queue_key = "test_queue"; 11 12 @Test 13 public void send(){ 14 Map<String,Object> msg = new HashMap()<>; 15 msg.put("data","hello,rabbmitmq!"); 16 mqProducer.sendDataToQueue(query_key,msg); 17 } 18}
运行测试程序,Run with JUnit,会发送一条消息到test_queue,监听器监听到消息后,打印出消息。
至此,已经完成了Spring和RabbmitMQ集成,配置,和使用。