RabbitMQ java客户端集成 Spring 开发环境

1、在pom.xml文件中引入rabbitmq坐标

    <!-- rabbitmq -->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

2、spring配置文件编写 (笔者独立出一个配置文件 spring-rabbit.xml 装载RabbitMQ配置)

<?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: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/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <bean id="fooMessageListerer"
        class="com.hao.web.listener.FooMessageListerer" />

    <!-- 配置连接 -->
    <rabbit:connection-factory
        id="connectionFactory" host="127.0.0.1" port="5672" username="guest"
        password="guest" virtual-host="/" requested-heartbeat="60" />

    <!-- 配置RabbitTemplate -->
    <rabbit:template id="amqpTemplate"
        connection-factory="connectionFactory" exchange="myExchange"
        routing-key="foo.bar" />
    
    <!-- 配置 RabbitAdmin -->
    <rabbit:admin connection-factory="connectionFactory" />

    <!-- 配置队列名称 -->
    <rabbit:queue name="myQueue" />

    <!-- 配置 topic 类型交换器 -->
    <rabbit:topic-exchange name="myExchange">
        <rabbit:bindings>
            <rabbit:binding queue="myQueue" pattern="foo.*"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>
    
    <!-- 配置监听器 -->
    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="fooMessageListerer"
            queue-names="myQueue" />
    </rabbit:listener-container>

</beans>

3、编写监听类

package com.hao.web.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

/**
 * @desc rabbitMQ监听器
 * @author zhanh247
 */
public class FooMessageListerer implements MessageListener {

    @Override
    public void onMessage(Message message) {
        String msg = new String(message.getBody());
        System.out.println("spring rabbitmq receive message: " + msg);
    }

}

4、编写发送消息的测试类

package com.hao;

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({ "classpath:spring/spring-rabbit.xml" })
public class RabbitMqTest {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void sendMessage() {
        rabbitTemplate.convertAndSend("hello spring-rabbit...");
    }

}

5、启动spring项目,运行测试类,发送测试消息

看到控制台有如下日志打印出,则说明集成已完成。

posted on 2019-12-15 23:59  牛鼻子老赵  阅读(394)  评论(0编辑  收藏  举报