Spring整合RabbitMQ生产者和消费者

一.所需jar包

序号jar
1spring-rabbit-1.7.3.jar
2spring-ampq-1.7.3.jar
3ampq-client-4.1.1.jar
4junit-4.12.jar
5spring-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");
    }
}

运行生产者测试
在这里插入图片描述

在这里插入图片描述
队列被创建

运行消费者测试
在这里插入图片描述
成功接收到消息!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值