Spring 集成 RabbitMQ

java

浏览数:349

2019-1-8

片段 1片段 2片段 3片段 4片段 5片段 6片段 7


pom.xml

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


spring-rabbitmq-parent.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

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

    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- queue 队列声明 -->
    <rabbit:queue id="queue" name="${rabbit.queue.name}"/>

    <!-- exchange queue binging key 绑定 -->
    <rabbit:direct-exchange id="directExchange" name="${rabbit.direct.exchange.name}">
        <rabbit:bindings>
            <rabbit:binding queue="queue" key="${rabbit.queue.key}"/>
        </rabbit:bindings>
    </rabbit:direct-exchange>
</beans>


spring-rabbitmq-producer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!-- 导入生产者和消费者的公共配置 -->
    <import resource="spring-rabbitmq-parent.xml"/>

    <!-- spring template声明 -->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" exchange="${rabbit.direct.exchange.name}" queue="${rabbit.queue.name}" routing-key="${rabbit.queue.key}"/>
</beans>


spring-rabbitmq-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!-- 导入生产者和消费者的公共配置 -->
    <import resource="spring-rabbitmq-parent.xml"/>

    <!-- queue litener  观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener queues="queue" ref="${rabbit.queue.listener}"/>
    </rabbit:listener-container>
</beans>


RabbitMQUtil.java

package com.app.core.util;

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@Log4j2
public class RabbitMQUtil {
    /**
     * RabbitMQ消息发送和接收模板
     */
    private static RabbitTemplate template;

    @Autowired
    public void setTemplate(RabbitTemplate template) {
        RabbitMQUtil.template = template;
    }

    /**
     * 发送文本消息
     *
     * @param msg 消息内容
     */
    public static void send(String msg) {
        template.convertAndSend(msg);

        if (log.isInfoEnabled())
            log.info("RabbitMQ消息发送成功,消息内容:{}", msg);
    }
}


QueueListener.java

package com.app.server.listener;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.CharEncoding;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Log4j2
@Component
public class QueueListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        try {
            // 将 byte 数组转换为字符串
            String msgContent = new String(message.getBody(), CharEncoding.UTF_8);

            if (log.isInfoEnabled())
                log.info("RabbitMQ消息接收成功,消息内容:{}", msgContent);
        } catch (UnsupportedEncodingException e) {
            log.error("RabbitMQ编码类型不支持", e);
        }
    }
}


config.properties

rabbit.host=127.0.0.1
rabbit.port=8080
rabbit.username=
rabbit.password=
rabbit.direct.exchange.name=exchange.demo.name
rabbit.queue.key=queue.demo.key
rabbit.queue.name=queue.demo.name
rabbit.queue.listener=queueListener