spring-web 集成 rabbitmq

Posted 护花使者

tags:

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

 

引入java包:

本项目中仅引入了四个java包:amqp-client-5.7.3.jar,spring-rabbit-2.2.2.RELEASE.jar,spring-retry-1.2.4.RELEASE.jar,spring-amqp-2.2.2.RELEASE.jar

 

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">

    <!--配置connection-factory,指定连接rabbit server参数 -->
    <rabbit:connection-factory id="connectionFactory"
                               username="guest" password="guest" host="localhost" port="5672" />

    <!--定义rabbit template用于数据的发送 -->
    <rabbit:template id="amqpTemplate"  connection-factory="connectionFactory"
                     exchange="exchangeTest" />

    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
    <rabbit:admin connection-factory="connectionFactory" />

    <!--定义queue -->
    <rabbit:queue name="queueTest" durable="true" auto-delete="false" exclusive="false" />

    <!-- 定义direct exchange,绑定queueTest -->
    <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!-- 消息接收者 -->
    <bean id="messageReceiver" class="club.codeapes.web.core.rabbitmq.RabbitMqMessageConsumer"></bean>

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

</beans>

 spring中需要引入这个xml,  主要在总spring.xml。或者web.xml中需要引入下。

 

RabbitMqMessageConsumer

package club.codeapes.web.core.rabbitmq;

import club.codeapes.common.date.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class RabbitMqMessageConsumer implements MessageListener {

    @Override
    public void onMessage(Message message) {
        System.out.println("消费信息," + DateUtil.getNow("yyyy-MM-dd HH:mm:ss") + "---->" + message);
    }

}

 

RabbitMqMessageProducer:

package club.codeapes.web.core.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class RabbitMqMessageProducer{

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessage(Object message) {
        System.out.println("to send message:" + message);
        amqpTemplate.convertAndSend("queueTestKey", message);
    }

}

 

 



 

 

 

 

以上是关于spring-web 集成 rabbitmq的主要内容,如果未能解决你的问题,请参考以下文章

Spring与RabbitMQ集成问题

Spring与RabbitMQ集成问题

SpringBoot集成RabbitMQ之死信队列限流队列延迟队列(第四节)

分布式WebSocket - 4SpringBoot集成STOMP协议,RabbitMQ为消息代理

springboot集成RabbitMQ,Eclipse开发集成RabbitMq,IDEA集成RabbitMQ报错 socket close

RabbitMQ:第三章:Springboot集成RabbitMQ(直连模式,工作队列模式,发布订阅模式,路由模式,通配符模式)