<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
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/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd"> <description>rabbitmq 连接服务配置</description>
<!-- 不适用【发布确认】连接配置 -->
<rabbit:connection-factory id="rabbitConnectionFactory"
host="172.18.112.102" username="woms" password="woms" port="5672"
virtual-host="lingyi" channel-cache-size="25" cache-mode="CHANNEL" publisher-confirms="true" publisher-returns="true" connection-timeout="200"/> <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="200" />
<property name="maxInterval" value="30000" />
</bean>
</property>
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="5"/>
</bean>
</property>
</bean> <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 如果使用多exchange必须配置declared-by="connectAdmin" -->
<rabbit:admin id="connectAdmin" connection-factory="connectionFactory"/> <rabbit:template id="ampqTemplate" connection-factory="connectionFactory"
exchange="test-mq-exchange" return-callback="sendReturnCallback"
message-converter="jsonMessageConverter" routing-key="test_queue_key"
mandatory="true" confirm-callback="confirmCallback" retry-template="retryTemplate"/> <bean id="confirmCallback" class="ly.net.rabbitmq.MsgSendConfirmCallBack" />
<bean id="sendReturnCallback" class="ly.net.rabbitmq.MsgSendReturnCallback" />
<!-- 消息对象json转换类 -->
<bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> <!-- queue配置 -->
<!-- durable:是否持久化 -->
<!-- exclusive: 仅创建者可以使用的私有队列,断开后自动删除 -->
<!-- auto_delete: 当所有消费客户端连接断开后,是否自动删除队列 -->
<rabbit:queue id="test_queue_key" name="test_queue_key" durable="true" auto-delete="false" exclusive="false" declared-by="rabbitAdmin" /> <!-- exchange配置 -->
<!-- rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。 -->
<!-- rabbit:binding:设置消息queue匹配的key -->
<rabbit:direct-exchange name="test-mq-exchange"
durable="true" auto-delete="false" id="test-mq-exchange" declared-by="rabbitAdmin">
<rabbit:bindings>
<rabbit:binding queue="test_queue_key" key="test_queue_key" />
</rabbit:bindings>
</rabbit:direct-exchange> <!-- <rabbit:topic-exchange name="${mq.queue}_exchange" durable="true" auto-delete="false"> -->
<!-- <rabbit:bindings> -->
<!-- 设置消息Queue匹配的pattern (direct模式为key) -->
<!-- <rabbit:binding queue="test_queue" pattern="${mq.queue}_patt"/> -->
<!-- </rabbit:bindings> -->
<!-- </rabbit:topic-exchange> --> <bean id="mqConsumer" class="ly.net.rabbitmq.MQConsumer" />
<bean id="mqConsumer1" class="ly.net.rabbitmq.MQConsumerManual" /> <!-- listener配置 消费者 自动确认 -->
<!-- queues:监听的队列,多个的话用逗号(,)分隔 ref:监听器 -->
<rabbit:listener-container
connection-factory="connectionFactory" acknowledge="auto"
message-converter="jsonMessageConverter">
<rabbit:listener queues="test_queue_key" ref="mqConsumer" />
</rabbit:listener-container>
<!-- 消费者 手动确认 -->
<rabbit:listener-container
connection-factory="connectionFactory" acknowledge="manual">
<rabbit:listener queues="test_queue_key" ref="mqConsumer1" />
</rabbit:listener-container> </beans>
 package ly.net.rabbitmq;

 import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
public class MsgSendConfirmCallBack implements RabbitTemplate.ConfirmCallback { @Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
// TODO Auto-generated method stub
if (ack) {
System.out.println("消息确认成功");
} else {
//处理丢失的消息
System.out.println("消息确认失败,"+cause);
}
} }
package ly.net.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.beans.factory.annotation.Autowired; public class MsgSendReturnCallback implements RabbitTemplate.ReturnCallback{
@Autowired
private RabbitTemplate errorTemplate; @Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
String msgJson = new String(message.getBody());
System.out.println("Returned Message:"+msgJson); //重新发布
// RepublishMessageRecoverer recoverer = new RepublishMessageRecoverer(errorTemplate,"errorExchange", "errorRoutingKey");
// Throwable cause = new Exception(new Exception("route_fail_and_republish"));
// recoverer.recover(message,cause);
// System.out.println("Returned Message:"+replyText);
//
} }
 package ly.net.rabbitmq;

 import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener; import com.rabbitmq.client.Channel; public class MQConsumerManual implements ChannelAwareMessageListener { @Override
public void onMessage(Message message, Channel channel) throws Exception {
// TODO Auto-generated method stub
//手动确认
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
} }
@Service
public class MQProducerImpl implements MQProducer {
@Autowired
private AmqpTemplate amqpTemplate; private final static Logger LOGGER = Logger.getLogger(MQProducerImpl.class);
/*
* convertAndSend:将Java对象转换为消息发送到匹配Key的交换机中Exchange,由于配置了JSON转换,这里是将Java对象转换成JSON字符串的形式。
* 原文:Convert a Java object to an Amqp Message and send it to a default exchange with a specific routing key.
*/
@Override
public void sendDataToQueue(String queueKey, Object object) {
try {
amqpTemplate.convertAndSend(object); } catch (Exception e) {
LOGGER.error(e);
} }
}
public interface MQProducer {
/**
* 发送消息到指定队列
* @param queueKey
* @param object
*/
public void sendDataToQueue(String queueKey, Object object);
}

RabbitMQ整合spring的更多相关文章

  1. 消息中间件——RabbitMQ(九)RabbitMQ整合Spring AMQP实战!(全)

    前言 1. AMQP 核心组件 RabbitAdmin SpringAMQP声明 RabbitTemplate SimpleMessageListenerContainer MessageListen ...

  2. RabbitMQ学习笔记(5)----RabbitMQ整合Spring

    在Spring AMQP项目中Spring也提供了对RabbitMQ的支持,这里在之前学习SpringBoot的时候也整合过,但是今天这里使用的Spring的xml配置来整个rabbit. Sprin ...

  3. Rabbitmq 整合Spring,SpringBoot与Docker

    SpringBootLearning是对Springboot与其他框架学习与研究项目,是根据实际项目的形式对进行配置与处理,欢迎star与fork. [oschina 地址] http://git.o ...

  4. RabbitMQ整合Spring Booot【消费者补偿幂等问题】

    如果消费者 运行时候 报错了 package com.toov5.msg.SMS; import org.springframework.amqp.rabbit.annotation.RabbitHa ...

  5. RabbitMQ整合Spring Booot【消费者应答模式】

    生产者代码不变,消费者: package com.toov5.Consumer; import java.io.IOException; import java.util.concurrent.Tim ...

  6. RabbitMQ整合Spring Booot【点对点模式】

    pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

  7. RabbitMQ整合Spring Booot【Exchange-Fanout模式】

    pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  8. 关于RabbitMQ以及RabbitMQ和Spring的整合

    转自:https://www.cnblogs.com/s648667069/p/6401463.html 基本概念 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是 ...

  9. 消息中间件——RabbitMQ(十)RabbitMQ整合SpringBoot实战!(全)

    前言 1. SpringBoot整合配置详解 publisher-confirms,实现一个监听器用于监听Broker端给我们返回的确认请求:RabbitTemplate.ConfirmCallbac ...

随机推荐

  1. [VS 2015] VS2015 完整ISO镜像包

    区别 :https://www.visualstudio.com/zh-cn/products/compare-visual-studio-2015-products-vs 完整ISO镜像:下载 VS ...

  2. Python3基础 bool类型变量赋值

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. shell编程学习笔记之特殊变量($0、$1、$2、 $?、 $# 、$@、 $*)

    特殊变量($0.$1.$2. $?. $# .$@. $*) shell编程中有一些特殊的变量可以使用.这些变量在脚本中可以作为全局变量来使用. 名称 说明 $0 脚本名称 $1-9 脚本执行时的参数 ...

  4. SPOJ - POLYNOM Polynomial(数论乱搞)题解

    题意 :给你n个数,问你是否存在一个多项式(最多三次方)满足f(i)= xi. 思路:讲一个神奇的思路: x3 - (x - 1)3 = 3x2 - 3x + 1 x2 - (x - 1)2 = 2x ...

  5. 51nod 1020 逆序排列 递推DP

    1020 逆序排列  基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么 ...

  6. v-pre原样输出&&v-once只加载一次

    html <div id="app"> <div v-pre>{{message1}}</div><!--原样输出--> <b ...

  7. [Pytorch]深度模型的显存计算以及优化

    原文链接:https://oldpan.me/archives/how-to-calculate-gpu-memory 前言 亲,显存炸了,你的显卡快冒烟了! torch.FatalError: cu ...

  8. 【jdk源码分析】jdk8的ArrayList初始化长度为0

    先看结果 用的是反射获取elementData底层数组的长度 查看源码 无参构造函数没有了this.size = 10; 图1 图2 图3 图4 java的基本数据类型默认值 所以无参构造时长度为0 ...

  9. testNG 学习笔记 Day 1 使用功能详解

    TestSuite处理测试用例有6个规约(否则会被拒绝执行测试) A 测试用例必须是公有类(Public) B 测试用例必须继承与TestCase类 C 测试用例的测试方法必须是公有的( Public ...

  10. Android手机摄像头编程入门

    本讲内容:Android手机摄像头编程入门智能手机中的摄像头和普通手机中的摄像头最大的区别在于,智能机上的摄像头可以由程序员写程序控制, 做一些有趣的应用譬如,画中画,做一些有用的应用譬如二维码识别, ...