文章目录

1.pom
<!--spring整合rabbitmq-->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
2. spring-rabbitmq-producer.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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!--生产者者配置如下:-->
<context:property-placeholder location="/WEB-INF/config/rabbitmq.properties"/>

<!-- 定义RabbitMQ的连接工厂

-->
<rabbit:connection-factory id="connectionFactory"
addresses="${rabbitmq.addresses}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.vhost}"
connection-timeout="${rabbitmq.conTimeout}"
publisher-confirms="${rabbitmq.publisher-confirms}"
publisher-returns="${rabbitmq.publisher-returns}"
channel-cache-size="${rabbitmq.channel.cache-size}"/>
<!-- 管理消息队列 -->
<rabbit:admin connection-factory="connectionFactory"/>

<!--此处为配置文件方式 管控台配置模式需要注释 默认模式管控台 Start-->
<!-- 定义一个队列或者多个队列 自动声明-->
<rabbit:queue name="LY-TRACE-OBJ-QUEUE" auto-declare="true" durable="true"/>
<rabbit:queue name="LY-TRACE-OBJ-QUEUE2" auto-declare="true" durable="true"/>

<rabbit:topic-exchange name="LY-TRACE-WS-EXCHANGE">
<rabbit:bindings>
<!-- 可绑定多个队列,发送的时候指定key进行发送 -->
<rabbit:binding queue="LY-TRACE-OBJ-QUEUE" pattern="LY-trace-obj.h"/>
<rabbit:binding queue="LY-TRACE-OBJ-QUEUE" pattern="LY-trace-obj.h"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:topic-exchange name="LY-TRACE-HTTP-EXCHANGE">
<rabbit:bindings>
<!-- 可绑定多个队列,发送的时候指定key进行发送 -->
<rabbit:binding queue="LY-TRACE-OBJ-QUEUE" pattern="LY-trace-obj.*"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<!--此处为配置文件方式 管控台配置模式需要注释 默认模式管控台 End-->

<!-- 定义交换机 自动声明-->
<rabbit:topic-exchange name="LY-TRACE-WS-EXCHANGE"
auto-declare="true" durable="true"/>
<rabbit:topic-exchange name="LY-TRACE-HTTP-EXCHANGE"
auto-declare="true" durable="true"/>

<!-- 定义MQ消息模板
1. id 定义消息模板ID
2.connection-factory 把定义的连接工厂放到消息模板中
3.confirm-callback confirm确认机制
4.return-callback return确认机制
5.mandatory #有2种状态
设置为 true 消费者在消息没有被路由到合适队列情况下会被return监听,而不会自动删除;
设置为 false 消费者在消息没有被路由到合适队列情况下会自动删除
-->
<rabbit:template id="rabbitTemplate"
connection-factory="connectionFactory"
confirm-callback="confirmCallBackListener"
return-callback="returnCallBackListener"
mandatory="${rabbitmq.mandatory}"/>

<!--RabbitMQ2种消息确认机制 xml形式 如果使用注解请注释-->
<bean id="confirmCallBackListener" class="com.gblfy.LY.confirm.ConfirmCallBackListener"/>
<bean id="returnCallBackListener" class="com.gblfy.LY.re.ReturnCallBackListener"/>

</beans>
3. spring-rabbitmq-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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!--消费者配置如下:-->
<context:property-placeholder location="/WEB-INF/config/rabbitmq.properties"/>

<!-- 定义RabbitMQ的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
addresses="${rabbitmq.addresses}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.vhost}"
connection-timeout="${rabbitmq.conTimeout}"/>
<!-- 管理消息队列 -->
<rabbit:admin connection-factory="connectionFactory"/>

<!-- 定义一个队列或者多个队列 自动声明-->
<!-- <rabbit:queue name="LY-TRACE-OBJ-QUEUE" auto-declare="true" durable="true"/>-->

<!-- 声明多个消费者对象 -->
<bean id="mQAsynStorageLog" class="com.gblfy.mqhandler.MQAsynStorageLog"/>

<!-- 监听队列
1. connectionFactory 连接工厂
2. acknowledge 签收方式:手动签收
3. concurrency 消费者多线程监听队列数量
max-concurrency 消费者多线程监听队列最大数量
4. ref="" 消费者监听
-->
<rabbit:listener-container connection-factory="connectionFactory"
acknowledge="manual"
concurrency="${rabbitmq.concurrency}"
max-concurrency="${rabbitmq.max-concurrency}">
<rabbit:listener ref="mQAsynStorageLog" method="onMessage" queue-names="LY-TRACE-STR-QUEUE,LY-TRACE-OBJ-QUEUE"/>
</rabbit:listener-container>
</beans>
4. rabbitmq.properties
#RabbitMQ 连接信息
#IP地址
rabbitmq.addresses=ip1:5672,ip2:5672,ip3:5672
#端口
#rabbitmq.port=5672
#用户名
rabbitmq.username=guest
#密码
rabbitmq.password=guest
#虚拟主机
rabbitmq.vhost=/
#连接超时时间
rabbitmq.conTimeout=15000
#发送确认 对应RabbitTemplate.ConfirmCallback接口
#消息发送成功 有2个重要参数
# ack 状态为true correlationId 全局唯一ID用于标识每一支队列
#设置发送消息失败重试
rabbitmq.publisher-confirms=true
rabbitmq.mandatory=true
#发送失败回退,对应RabbitTemplate.ReturnCallback接口
rabbitmq.publisher-returns=true
#解决多线程发送消息
rabbitmq.channel.cache-size=500

#消费者多线程消费
rabbitmq.concurrency=10
rabbitmq.max-concurrency=20