一.所需jar包
序号 | jar |
---|---|
1 | spring-rabbit-1.7.3.jar |
2 | spring-ampq-1.7.3.jar |
3 | ampq-client-4.1.1.jar |
4 | junit-4.12.jar |
5 | spring-test-4.3.8.jar |
二.配置Spring配置文件
配置spring-consumer.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: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
http://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"/>
<!--引入Spring-Producer配置文件-->
<import resource="Spring-Producer.xml" />
<context:component-scan base-package="cn"/>
<!--配置队列信息-->
<rabbit:queue id="MyQueue" durable="true" auto-delete="true" exclusive="false"
name="sjw.queue"/>
<!--配置交换器,直连模式-->
<rabbit:direct-exchange id="directExchange" name="${rabbit.exchange}" durable="true"
auto-delete="true">
<rabbit:bindings>
<rabbit:binding queue="MyQueue" key="sjw-key-1"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<!--bean消费者-->
<bean id="Consumer" class="cn.sheng.rabbit.Consumer"/>
<!--启动消费者监听程序-->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="Consumer" queues="MyQueue"/>
</rabbit:listener-container>
</beans>
配置spring-consumer.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: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
http://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"/>
<!--配置连接工厂-->
<rabbit:connection-factory id="connectionFactory" host="${rabbit.host}"
port="${rabbit.port}" virtual-host="${rabbit.vhost}"
username="${rabbit.usename}" password="${rabbit.password}"/>
<!--所有共产连接都要交给连接管理进行代管-->
<rabbit:admin id="rabbitAdmin" connection-factory="connectionFactory"/>
<!--配置队列信息-->
<rabbit:queue id="MyQueue" durable="true" auto-delete="true" exclusive="false"
name="sjw.queue"/>
<!--配置交换器,直连模式-->
<rabbit:direct-exchange id="directExchange" name="${rabbit.exchange}" durable="true"
auto-delete="true">
<rabbit:bindings>
<rabbit:binding queue="MyQueue" key="sjw-key-1"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<!--定义模板-->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"
exchange="${rabbit.exchange}"/>
</beans>
生产者代码
public class IMessageImpl implements IMessageService {
//注入文件内定义的模板
@Resource(name = "rabbitTemplate")
private AmqpTemplate rabbitTemplate;
@Override
public void send(String Message) {
//第一个是rountingKey,第二个是参数
rabbitTemplate.convertAndSend("sjw-key-1",Message);
}
}
三.编写与运行测试类
消费者测试类
@ContextConfiguration(locations = {"classpath:Spring-Consumer.xml"})
//可以在测试开始的时候自动创建Spring应用上下文
@RunWith(SpringJUnit4ClassRunner.class)
public class ConsumerTest {
@Test
public void TestConsumer(){
try {
//使消费者线程睡眠,一直维持接收状态,等待队列新增信息
Thread.sleep(Long.MAX_VALUE);
}catch (Exception e){
e.printStackTrace();
}
}
}
生产者测试类
ContextConfiguration(locations = {"classpath:Spring-Consumer.xml"})
//可以在测试开始的时候自动创建Spring应用上下文
@RunWith(SpringJUnit4ClassRunner.class)
public class ProducerTest {
@Resource(name = "IMessageImpl")
private IMessageService messageService;
@Test
public void TestProducer(){
messageService.send("sjw");
}
}
运行生产者测试
队列被创建
运行消费者测试
成功接收到消息!!!