<?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);
}

最新文章

  1. Python中的高级特性
  2. Jquery ajax提交表单几种方法
  3. MFC创建文件和文件夹
  4. jquery.validate 基础
  5. bootstrap和bootstrap-select的outline设置
  6. oracle 安装注意
  7. 认识Runtime1
  8. [转]FFMPEG视音频编解码零基础学习方法
  9. 结构体key
  10. ubuntu tab命令补全失效
  11. 【配置文件节点】java世界配置文件节点
  12. mark jquery 链式调用的js原理
  13. c语言数据处理!
  14. [Apache系列]怎样在windows下配置apache vhost
  15. Qt开发初步,循序渐进,preRequest for 蓝图逆袭
  16. cookies和session的优缺点
  17. CSS3的background-size
  18. 大数据平台搭建-kafka集群的搭建
  19. 超详细的PS抠图方法
  20. mysql日期问题

热门文章

  1. MS08_067漏洞测试——20145301
  2. Android项目开发四
  3. Java继承相关知识总结
  4. tslib移植笔记(1)【转】
  5. 全面理解虚拟DOM,实现虚拟DOM
  6. CSS3 动画的一些属性
  7. Ubuntu 14.04 下 安装Protocol Buffers
  8. vim E437: terminal capability &quot;cm&quot; required
  9. MVC ---- T4模板的小练习
  10. LINK : fatal error LNK1104: 无法打开文件“libboost_serialization-vc90-mt-gd-1_62.lib”