RabbitMQ之与Spring集成

Posted zengnansheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ之与Spring集成相关的知识,希望对你有一定的参考价值。

Maven配置

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.6.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>1.6.0.RELEASE</version>
</dependency>

 

 

增加rabbitmq.properties文件并引入到spring中

mq.host=127.0.0.1

mq.username=zns

mq.password=123456

mq.port=5672

 

用户名和密码可以访问http://localhost:15672登录后台设置,

注意:5672是连接端口,15672是管理界面的端口

 

增加spring-rabbitmq.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: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" >

    <!-- 连接 RabbitMQ 配置 -->
    <rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}"/>
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- 声明一个 RabbitMQ Template -->
    <rabbit:template id="amqpTemplate" exchange="exchange1" connection-factory="connectionFactory"  message-converter="jsonMessageConverter" />
    <!-- 消息对象json转换类 -->
    <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />   
    <!-- 声明消息队列 -->
    <rabbit:queue id="queue1" name="queue1" durable="true" auto-delete="false" exclusive="false" />   
    <!-- 声明一个直连类型direct交换器 -->
    <rabbit:direct-exchange id="exchange1" name="exchange1" durable="true" auto-delete="false" >
        <rabbit:bindings>
            <rabbit:binding queue="queue1" key="key1"/>
        </rabbit:bindings>
    </rabbit:direct-exchange>      
    <!-- 配置消费者监听  queues:监听的队列,多个用逗号分隔  ref:监听器 -->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
        <rabbit:listener queues="queue1" ref="queueListenter1"/>
    </rabbit:listener-container>
    <bean name="queueListenter1" class="com.zns.listenter.QueueListenter1"></bean>        
</beans>


<import resource="classpath:spring/spring-rabbitmq.xml"/>

 

 

新增消费者监听器QueueListenter1

package com.zns.listenter;

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

public class QueueListenter1 implements MessageListener {
    public void onMessage(Message msg) {
        System.out.println("接收到的消息为:"+msg.toString());
    }
}

发送消息到Rabbitmq

@Autowired
private AmqpTemplate amqpTemplate;

@RequestMapping("/Send")
public @ResponseBody Object Send() throws Exception {
    Map<String,Object> msg = new HashMap<String, Object>();
    msg.put("data","hello,rabbmitmq!");
    amqpTemplate.convertAndSend("key1",msg);
    return msg;
}

 

运行访问可以看到消费者端监听打印到了消息。

以上是关于RabbitMQ之与Spring集成的主要内容,如果未能解决你的问题,请参考以下文章

ActiveMQ之与Spring集成

Spring与RabbitMQ集成问题

Spring与RabbitMQ集成问题

rabbitMQ第五篇:Spring集成RabbitMQ

RabbitMQ第四篇:Spring集成RabbitMQ

消息队列RabbitMQ与Spring集成