Spring RabbbitMq 整合


1、先介绍下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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">

<!-- scan service -->
<context:component-scan base-package="com.wang.rabbitmq.service">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<context:property-placeholder location="classpath:application.properties" />

<!-- 连接配置 必须先搭建这个服务才可以进行这一步骤-->
<rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}" virtual-host="${mq.vhost}" />

<rabbit:admin connection-factory="connectionFactory" />

------------------------------------------------------------------------------ 下面这两行只是为了 做消息转换的。这个交换机下面的都按照这个进行消息转换,也可以精确到路由key
<!-- spring template声明 -->
<rabbit:template exchange="test_exchange" id="amqpTemplate"
connection-factory="connectionFactory" message-converter="jsonMessageConverter" />

<!-- 消息对象json转换类 -->
<bean id="jsonMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
------------------------------------------------------------------------------

<!-- 申明一个消息队列Queue -->
<rabbit:queue id="test_queue" name="test_queue"
durable="true" auto-delete="false" exclusive="false" />

<!-- 交换机定义 -->
<rabbit:direct-exchange name="test_exchange"
durable="true" auto-delete="false" id="test_exchange">
<rabbit:bindings>
<rabbit:binding queue="test_queue" key="test_routing_key" /> <!-- 把这个消息队列绑定在这个交换机上 key="test_routing_key" 表示一组路由 -->
</rabbit:bindings>
</rabbit:direct-exchange>

<!-- 客户端监听的队列 -->
<rabbit:listener-container
connection-factory="connectionFactory" acknowledge="auto">
<rabbit:listener queues="test_queue" ref="queueListenter" />
</rabbit:listener-container>
</beans>

参数配置文件:
mq.host=localhost
mq.username=guest
mq.password=guest
mq.port=5672 //默认端口
mq.vhost=test-host //这个是要在localhost15672管理平台进行配置的,还要把这个归属到哪个用户上面。


创建rabbitmq:virtual host、exchange、queue
参照博客http://blog.csdn.net/zheng911209/article/details/49949303

java代码介绍
【生产者】
public interface MQProducer {

/**
* 发送消息到指定队列
* @param queueKey
* @param object
*/
public void sendDataToQueue(String queueKey, Object object);
}


@Service
public class MQProducerImpl implements MQProducer{

@Autowired
private AmqpTemplate amqpTemplate;

public void sendDataToQueue(String queueKey, Object object) {
// TODO Auto-generated method stub
amqpTemplate.convertAndSend(queueKey, object);
}
}

【消费者】
@Component
public class QueueListenter implements MessageListener {

@Override
public void onMessage(Message msg) {
// TODO Auto-generated method stub
System.out.print("+++++++++++++++++++++++++" + msg.toString());
}

}

 

运行junit 测试类就能看出效果。

posted @ 2017-10-25 12:54  下一站你要去哪里  阅读(424)  评论(0编辑  收藏  举报