赞
踩
说明: spring 整合 rabbitmq 的主要之处是配置文件信息, 代码量减少
1. 创建生产者
rabbitmq.host=192.168.192.137
rabbitmq.port=5672
rabbitmq.username=qmh
rabbitmq.password=qmh
rabbitmq.virtual-host=/qmh
<?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:context="http://www.springframework.org/schema/context" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!--加载配置文件--> <context:property-placeholder location="classpath:rabbitmq.properties"/> <!-- 定义rabbitmq connectionFactory --> <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <!--定义管理交换机、队列--> <rabbit:admin connection-factory="connectionFactory"/> <!--定义持久化队列,不存在则自动创建;不绑定到交换机则绑定到默认交换机 默认交换机类型为direct,名字为:"",路由键为队列的名称 id: bean容器的名称 name: 队列queue的名称 auto-declare: 自动创建 --> <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!--定义广播交换机中的持久化队列,不存在则自动创建--> <rabbit:queue id="spring_fanout_queue_one" name="spring_fanout_queue_one" auto-declare="true"/> <!--定义广播交换机中的持久化队列,不存在则自动创建--> <rabbit:queue id="spring_fanout_queue_two" name="spring_fanout_queue_two" auto-declare="true"/> <!--定义广播类型交换机;并绑定上述两个队列--> <rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="spring_fanout_queue_one"/> <rabbit:binding queue="spring_fanout_queue_two"/> </rabbit:bindings> </rabbit:fanout-exchange> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~路由;指定routing key的队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!--定义广播交换机中的持久化队列,不存在则自动创建--> <rabbit:queue id="spring_direct_queue_one" name="spring_direct_queue_one" auto-declare="true"/> <!--定义广播交换机中的持久化队列,不存在则自动创建--> <rabbit:queue id="spring_direct_queue_two" name="spring_direct_queue_two" auto-declare="true"/> <rabbit:direct-exchange id="spring_direct_exchange" name="spring_direct_exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="spring_direct_queue_one" key="qmh"></rabbit:binding> <rabbit:binding queue="spring_direct_queue_one" key="lyq"></rabbit:binding> <rabbit:binding queue="spring_direct_queue_two" key="qmh love lyq"></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~通配符;*匹配一个单词,#匹配多个单词 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!--定义广播交换机中的持久化队列,不存在则自动创建--> <rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/> <!--定义广播交换机中的持久化队列,不存在则自动创建--> <rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/> <!--定义广播交换机中的持久化队列,不存在则自动创建--> <rabbit:queue id="spring_topic_queue_well_copy" name="spring_topic_queue_well_copy" auto-declare="true"/> <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding pattern="qmh.*" queue="spring_topic_queue_star"/> <rabbit:binding pattern="qmh.#" queue="spring_topic_queue_well"/> <rabbit:binding pattern="lyq.#" queue="spring_topic_queue_well_copy"/> </rabbit:bindings> </rabbit:topic-exchange> <!--定义rabbitTemplate对象操作可以在代码中方便发送消息--> <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/> </beans>
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml") public class SpringAndRabbitMQTest { @Autowired private RabbitTemplate rabbitTemplate; @Test public void helloWorldTest(){ // 发送消息 rabbitTemplate.convertAndSend("spring_queue","Hello,spring and rabbitmq!"); } @Test public void helloFanoutTest(){ // 发送消息 rabbitTemplate.convertAndSend("spring_fanout_exchange","","Hello,rabbitmq fanout test!"); } @Test public void helloDirectTest(){ // 发送消息 rabbitTemplate.convertAndSend("spring_direct_exchange","qmh love lyq","Hello,qmh love lyq!"); } @Test public void helloTopicTest(){ // 发送消息 rabbitTemplate.convertAndSend("spring_topic_exchange","qmh.lyq.love","Hello,rabbitmq topic test!"); } }
2. 创建消费者
rabbitmq.host=192.168.192.137
rabbitmq.port=5672
rabbitmq.username=qmh
rabbitmq.password=qmh
rabbitmq.virtual-host=/qmh
<?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:context="http://www.springframework.org/schema/context" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!--加载配置文件--> <context:property-placeholder location="classpath:rabbitmq.properties"/> <!-- 定义rabbitmq connectionFactory --> <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <!-- 定义对应的消费者实现类 SpringQueueListener,FanoutListenerOne.... --> <bean id="springQueueListener" class="com.itheima.rabbitmqListener.SpringQueueListener"/> <bean id="fanoutListenerOne" class="com.itheima.rabbitmqListener.FanoutListenerOne"/> <bean id="fanoutListenerTwo" class="com.itheima.rabbitmqListener.FanoutListenerTwo"/> <bean id="directListenerOne" class="com.itheima.rabbitmqListener.DirectListenerOne"/> <bean id="directListenerTwo" class="com.itheima.rabbitmqListener.DirectListenerTwo"/> <bean id="topicListenerStar" class="com.itheima.rabbitmqListener.TopicListenerStar"/> <bean id="topicListenerWell" class="com.itheima.rabbitmqListener.TopicListenerWell"/> <bean id="topicListenerWellCopy" class="com.itheima.rabbitmqListener.TopicListenerWellCopy"/> <!-- 为对应的消费者实现类添加监听器, 绑定对应的队列.... --> <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true"> <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/> <rabbit:listener ref="fanoutListenerOne" queue-names="spring_fanout_queue_one"/> <rabbit:listener ref="fanoutListenerTwo" queue-names="spring_fanout_queue_two"/> <rabbit:listener ref="directListenerOne" queue-names="spring_direct_queue_one"/> <rabbit:listener ref="directListenerTwo" queue-names="spring_direct_queue_two"/> <rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/> <rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/> <rabbit:listener ref="topicListenerWellCopy" queue-names="spring_topic_queue_well_copy"/> </rabbit:listener-container> </beans>
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* spring 整合 rabbitmq
*/
public class SpringQueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("spring_queue:" + new String(message.getBody()));
}
}
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml") public class ListenerTest { @Test public void test(){ Boolean flag = true; while (true){ } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。