spring的rabbitmq配置

来源:互联网 发布:手机多开软件 编辑:程序博客网 时间:2024/05/21 11:04

来源:http://conkeyn.iteye.com/blog/2197486


1、applicationContext-base.xml

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  7.     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"  
  9.     xmlns:task="http://www.springframework.org/schema/task"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  11.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  12.     http://www.springframework.org/schema/mvc  
  13.     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.     http://www.springframework.org/schema/aop  
  15.     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  16.     http://www.springframework.org/schema/context  
  17.     http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  18.     http://www.springframework.org/schema/cache  
  19.     http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  20.     http://www.springframework.org/schema/tx  
  21.     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  22.     http://www.springframework.org/schema/websocket  
  23.     http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd  
  24.     http://www.springframework.org/schema/data/jpa  
  25.     http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
  26.     http://www.springframework.org/schema/jdbc  
  27.     http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd  
  28.     http://www.springframework.org/schema/rabbit  
  29.     http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd  
  30.     http://www.springframework.org/schema/task  
  31.     http://www.springframework.org/schema/task/spring-task-4.1.xsd">  
  32.   
  33.     <!-- 自动扫描包,可以写多个 -->  
  34.     <context:component-scan base-package="com.test.**">  
  35.         <context:exclude-filter type="annotation"  
  36.             expression="org.springframework.stereotype.Controller" />  
  37.     </context:component-scan>  
  38.   
  39.     <!-- 开启注解事务只对当前配置文件有效 -->  
  40.     <tx:annotation-driven transaction-manager="transactionManager"  
  41.         proxy-target-class="true" />  
  42.   
  43.     <jpa:repositories base-package="com.test.  
  44.         repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory"  
  45.         transaction-manager-ref="transactionManager" />  
  46.   
  47.     <bean id="entityManagerFactory"  
  48.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  49.         <property name="dataSource" ref="dataSource" />  
  50.         <property name="packagesToScan" value="com.test. />  
  51.         <property name="persistenceProvider">  
  52.             <bean class="org.hibernate.ejb.HibernatePersistence" />  
  53.         </property>  
  54.         <property name="jpaVendorAdapter">  
  55.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
  56.                 <property name="generateDdl" value="true" />  
  57.                 <property name="databasePlatform" value="${hibernate.dialect}" />  
  58.                 <property name="showSql" value="${hibernate.show_sql}" />  
  59.             </bean>  
  60.         </property>  
  61.         <property name="jpaDialect">  
  62.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />  
  63.         </property>  
  64.         <property name="jpaPropertyMap">  
  65.             <map>  
  66.                 <entry key="hibernate.query.substitutions" value="true 1, false 0" />  
  67.                 <entry key="hibernate.default_batch_fetch_size" value="16" />  
  68.                 <entry key="hibernate.max_fetch_depth" value="2" />  
  69.                 <entry key="hibernate.generate_statistics" value="true" />  
  70.                 <entry key="hibernate.bytecode.use_reflection_optimizer"  
  71.                     value="true" />  
  72.                 <entry key="hibernate.cache.use_second_level_cache" value="${hibernate.cache.use_second_level_cache}" />  
  73.                 <entry key="hibernate.cache.use_query_cache" value="${hibernate.cache.use_query_cache}" />  
  74.                 <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />  
  75.             </map>  
  76.         </property>  
  77.     </bean>  
  78.   
  79.     <!--事务管理器配置 -->  
  80.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
  81.         <property name="entityManagerFactory" ref="entityManagerFactory" />  
  82.     </bean>  
  83.   
  84.     <!-- 数据源 -->  
  85.     <bean name="dataSource"  
  86.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  87.         <property name="driverClassName" value="${hibernate.connection.driver_class}" />  
  88.         <property name="url" value="${hibernate.connection.url}" />  
  89.         <property name="username" value="${hibernate.connection.username}" />  
  90.         <property name="password" value="${hibernate.connection.password}" />  
  91.     </bean>  
  92.   
  93.     <bean id="objectMapper" class="com.test.core.utils.JsonObjectMapper" />  
  94.   
  95.     <!-- 初始化数据库记录 -->  
  96.     <jdbc:initialize-database data-source="dataSource"  
  97.         ignore-failures="ALL">  
  98.         <jdbc:script location="classpath:*.sql" encoding="UTF-8" />  
  99.     </jdbc:initialize-database>  
  100.   
  101.     <!-- 异步的线程池,线程池的最在数不能设定太小,不然<rabbit:listener/>/@RabbitListener太多的话,会出现发无法正常消费问题 -->  
  102.     <task:executor id="taskExecutor" pool-size="4-256" queue-capacity="128" />  
  103.   
  104.     <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->  
  105.     <rabbit:annotation-driven />  
  106.   
  107.     <bean id="rabbitListenerContainerFactory" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">  
  108.         <property name="connectionFactory" ref="rabbitConnFactory" />  
  109.         <property name="transactionManager" ref="transactionManager" />  
  110.         <property name="concurrentConsumers" value="1" />  
  111.         <property name="maxConcurrentConsumers" value="10" />  
  112.         <property name="messageConverter" ref="jsonMessageConverter" />  
  113.         <property name="taskExecutor" ref="taskExecutor" />  
  114.         <property name="channelTransacted" value="true" />  
  115.         <property name="adviceChain">  
  116.             <array>  
  117.                 <ref bean="retryInterceptor" />  
  118.             </array>  
  119.         </property>  
  120.     </bean>  
  121.     <!-- rabbit:admin用于管理(创建和删除) exchanges, queues and bindings等 -->  
  122.   
  123.     <bean id="rabbitConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">  
  124.         <property name="host" value="${rabbitmq.host}" />  
  125.         <property name="port" value="${rabbitmq.port}" />  
  126.         <property name="username" value="${rabbitmq.username}" />  
  127.         <property name="password" value="${rabbitmq.password}" />  
  128.         <property name="virtualHost" value="${rabbitmq.vhost}" />  
  129.         <property name="connectionTimeout" value="${rabbitmq.connection.timeout}" />  
  130.     </bean>  
  131.   
  132.     <bean id="rabbitConnFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">  
  133.         <constructor-arg ref="rabbitConnectionFactory" />  
  134.         <property name="channelCacheSize" value="25" />  
  135.         <property name="executor" ref="taskExecutor" />  
  136.     </bean>  
  137.   
  138.     <rabbit:admin connection-factory="rabbitConnFactory" id="rabbitAdmin" />  
  139.   
  140.     <!-- 180秒 -->  
  141.     <rabbit:template id="amqpTemplate" reply-timeout="1000" connection-factory="rabbitConnFactory" message-converter="jsonMessageConverter" />  
  142.   
  143.   
  144.     <!-- 定义接收异常消息的exchange和queue -->  
  145.     <util:map id="dlxNaming" key-type="java.lang.String" value-type="java.lang.String">  
  146.         <entry key="zkcloud.subsystem.dlx.queue" value="#{'$dlx_queue_'+(T(com.zkteco.timecube.zkcloud.core.utils.PropertiesUtil).getValue('zkcloud.subsystem.code'))}" />  
  147.         <entry key="zkcloud.subsystem.dlx.exchange" value="#{'$dlx_ex_'+(T(com.zkteco.timecube.zkcloud.core.utils.PropertiesUtil).getValue('zkcloud.subsystem.code'))}" />  
  148.     </util:map>  
  149.   
  150.     <rabbit:queue id="zkcloud.subsystem.dlx.queue" name="#{dlxNaming['zkcloud.subsystem.dlx.queue']}">  
  151.         <rabbit:queue-arguments>  
  152.             <entry key="x-message-ttl">  
  153.                 <value type="java.lang.Long">86400000</value>  
  154.             </entry>  
  155.             <entry key="x-max-length">  
  156.                 <value type="java.lang.Long">100</value>  
  157.             </entry>  
  158.         </rabbit:queue-arguments>  
  159.     </rabbit:queue>  
  160.   
  161.     <rabbit:fanout-exchange id="zkcloud.subsystem.dlx.exchange" name="#{dlxNaming['zkcloud.subsystem.dlx.exchange']}">  
  162.         <rabbit:bindings>  
  163.             <rabbit:binding queue="zkcloud.subsystem.dlx.queue" />  
  164.         </rabbit:bindings>  
  165.     </rabbit:fanout-exchange>  
  166.   
  167.     <bean id="retryInterceptor" class="org.springframework.amqp.rabbit.config.StatelessRetryOperationsInterceptorFactoryBean">  
  168.         <property name="messageRecoverer" ref="messageRecoverer" />  
  169.         <property name="retryOperations" ref="retryTemplate" />  
  170.     </bean>  
  171.   
  172.     <!-- <bean id="messageRecoverer" class="org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer" /> -->  
  173.     <!-- 拒绝请求消息,并回复该请求者的请求被服务端拒绝-->  
  174.     <bean id="messageRecoverer" class="com.test.retry.RejectAndRplyToRequeueRecoverer">  
  175.         <property name="replyToTemplate" ref="amqpTemplate"/>  
  176.     </bean>  
  177.   
  178.     <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">  
  179.         <property name="backOffPolicy">  
  180.             <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">  
  181.                 <property name="initialInterval" value="1000" />  
  182.                 <property name="maxInterval" value="10000" />  
  183.             </bean>  
  184.         </property>  
  185.         <property name="retryPolicy">  
  186.             <bean class="org.springframework.retry.policy.SimpleRetryPolicy">  
  187.                 <property name="maxAttempts" value="1" />  
  188.             </bean>  
  189.         </property>  
  190.     </bean>  
  191.   
  192.     <bean id="jsonMessageConverter"  
  193.         class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"></bean>  
  194.   
  195.   
  196.     <!-- quartz配置 -->  
  197.     <bean class="com.zkteco.timecube.quartz.QuartJobSchedulingListener" />  
  198.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  199.         <property name="jobFactory">  
  200.             <bean class="com.zkteco.timecube.quartz.SpringQuartzJobFactory"></bean>  
  201.         </property>  
  202.         <property name="dataSource" ref="dataSource" />  
  203.         <!-- 要记得要指定配置文件的位置 -->  
  204.         <property name="configLocation" value="classpath:config/quartz.properties" />  
  205.     </bean>  
  206.     <!-- quartz配置 -->  
  207.   
  208.     <beans profile="develop">  
  209.         <bean id="propertyConfigurer" class="com.test.core.utils.PropertiesUtil"  
  210.             lazy-init="false">  
  211.             <property name="locations">  
  212.                 <list>  
  213.                     <value>classpath*:config/*.properties</value>  
  214.                 </list>  
  215.             </property>  
  216.             <property name="fileEncoding" value="utf-8" />  
  217.         </bean>  
  218.         <!-- 连接rabbitmq -->  
  219.         <rabbit:connection-factory id="rabbitConnFactory"  
  220.             host="localhost" username="guest" password="guest" port="5672"  
  221.             virtual-host="/" connection-timeout="30000" executor="taskExecutor" />  
  222.     </beans>  
  223.   
  224.     <beans profile="test">  
  225.         <bean id="propertyConfigurer" class="com.test.core.utils.PropertiesUtil"  
  226.             lazy-init="false">  
  227.             <property name="locations">  
  228.                 <list>  
  229.                     <value>classpath*:config/*.properties</value>  
  230.                     <value>classpath*:config/test/*.properties</value>  
  231.                 </list>  
  232.             </property>  
  233.             <property name="fileEncoding" value="utf-8" />  
  234.         </bean>  
  235.         <!-- 连接rabbitmq -->  
  236.         <rabbit:connection-factory id="rabbitConnFactory"  
  237.             host="192.168.0.179" username="guest" password="timeucbe" port="5672"  
  238.             virtual-host="/" connection-timeout="30000" executor="taskExecutor" />  
  239.     </beans>  
  240.   
  241.     <beans profile="production">  
  242.         <bean id="propertyConfigurer" class="com.test.core.utils.PropertiesUtil"  
  243.             lazy-init="false">  
  244.             <property name="locations">  
  245.                 <list>  
  246.                     <value>classpath*:config/*.properties</value>  
  247.                     <value>classpath*:config/production/*.properties</value>  
  248.                 </list>  
  249.             </property>  
  250.             <property name="fileEncoding" value="utf-8" />  
  251.             <!-- 连接rabbitmq -->  
  252.             <rabbit:connection-factory id="rabbitConnFactory"  
  253.                 host="114.215.82.3" username="guest" password="timecube" port="5672"  
  254.                 virtual-host="/" connection-timeout="30000" executor="taskExecutor" />  
  255.         </bean>  
  256.     </beans>  
  257.   
  258.   
  259. </beans>  

 2、Exchanges、routing keys、binding keys的配置

 

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  7.     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"  
  9.     xmlns:task="http://www.springframework.org/schema/task"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  11.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  12.     http://www.springframework.org/schema/mvc  
  13.     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.     http://www.springframework.org/schema/aop  
  15.     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  16.     http://www.springframework.org/schema/context  
  17.     http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  18.     http://www.springframework.org/schema/cache  
  19.     http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  20.     http://www.springframework.org/schema/tx  
  21.     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  22.     http://www.springframework.org/schema/websocket  
  23.     http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd  
  24.     http://www.springframework.org/schema/data/jpa  
  25.     http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
  26.     http://www.springframework.org/schema/jdbc  
  27.     http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd  
  28.     http://www.springframework.org/schema/rabbit  
  29.     http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd  
  30.     http://www.springframework.org/schema/task  
  31.     http://www.springframework.org/schema/task/spring-task-4.1.xsd">  
  32.   
  33.     <rabbit:queue id="queue_one" durable="true" auto-delete="false"  
  34.         name="queue_one">  
  35.         <!-- <rabbit:queue-arguments>  
  36.             <entry key="x-message-ttl">  
  37.                 <value type="java.lang.Long">100</value>  
  38.             </entry>  
  39.             <entry key="x-ha-policy" value="all" />  
  40.         </rabbit:queue-arguments> -->  
  41.     </rabbit:queue>  
  42.     <rabbit:direct-exchange name="my-mq-exchange"  
  43.         durable="true" auto-delete="false" id="my-mq-exchange">  
  44.         <rabbit:bindings>  
  45.             <rabbit:binding queue="queue_one" key="queue_one_key" />  
  46.         </rabbit:bindings>  
  47.     </rabbit:direct-exchange>  
  48.   
  49.   
  50.     <rabbit:queue id="queue_two" durable="true" auto-delete="false"  
  51.         exclusive="false" name="queue_two" />  
  52.     <rabbit:direct-exchange name="my-mq-exchange1"  
  53.         durable="true" auto-delete="false" id="my-mq-exchange1">  
  54.         <rabbit:bindings>  
  55.             <rabbit:binding queue="queue_two" key="queue_two_key" />  
  56.         </rabbit:bindings>  
  57.     </rabbit:direct-exchange>  
  58. </beans>  

 

Java代码  收藏代码
  1. import javax.annotation.Resource;  
  2.   
  3. import org.springframework.amqp.core.AmqpTemplate;  
  4. import org.springframework.stereotype.Service;  
  5. import org.springframework.transaction.annotation.Transactional;  
  6.   
  7. /** 
  8.  * 查 
  9.  * @version 0.0.0.1 
  10.  * @since 2015年3月30日 下午3:22:49 
  11.  */  
  12. @Service("producerMq")  
  13. @Transactional  
  14. public class ProducerMq  
  15. {  
  16.   
  17.     @Resource  
  18.     private AmqpTemplate amqpTemplate;  
  19.   
  20.     //同步示例  
  21.     public void sendDataToCrQueue(Object obj)  
  22.     {  
  23.         amqpTemplate.convertAndSend("my-mq-exchange""queue_one_key", obj);  
  24.     }  
  25.       
  26. }  

 

Java代码  收藏代码
  1. import javax.annotation.Resource;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5.   
  6. /** 
  7.  * 大 
  8.  * @version 0.0.0.1 
  9.  * @since 2015年3月30日 下午3:23:12 
  10.  */  
  11. @Controller  
  12. public class MessageController  
  13. {  
  14.     @Resource  
  15.     private ProducerMq producer;  
  16.   
  17.     @RequestMapping("/producer")  
  18.     public void producer() throws Exception  
  19.     {  
  20.         for (int i = 0; i < 100; i++)  
  21.         {  
  22.             producer.sendDataToCrQueue("data" + i);  
  23.         }  
  24.     }  
  25.   
  26. }  

 

 

 

Java代码  收藏代码
  1. import org.springframework.amqp.core.Message;  
  2. import org.springframework.amqp.rabbit.annotation.RabbitListener;  
  3. import org.springframework.messaging.handler.annotation.SendTo;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. /** 
  7.  * 队列监听器 
  8.  *  
  9.  * @author <a href="mailto:zhongqing.lin@zkteco.com">zhongqing.lin</a> 
  10.  * @version 0.0.0.1 
  11.  * @since 2015年3月30日 下午7:02:59 
  12.  */  
  13. @Component  
  14. public class QueueOneLitener  
  15. {  
  16.   
  17.     @RabbitListener(queues = "queue_one", exclusive = false,containerFactory="rabbitListenerContainerFactory",admin="rabbitAdmin")  
  18.     //参数中使用@Header获取mesage  
  19.     @SendTo("my-mq-exchange1/queue_two_key")  
  20.     public org.springframework.messaging.Message<String> data1(Message message)  
  21.     {  
  22.         System.out.println("headers:" + message.getMessageProperties().toString());  
  23.         String data = new String(message.getBody());  
  24.         System.out.println("queue_one data:" + data);  
  25.           
  26.         return org.springframework.messaging.support.MessageBuilder.withPayload(data).build();  
  27.     }  
  28. }  

 注意:

@SendTo的value填入的值应该是“exchange/routingKey”格式。

  • foo/bar - the replyTo exchange and routingKey.
  • foo/ - the replyTo exchange and default (empty) routingKey.
  • bar or /bar - the replyTo routingKey and default (empty) exchange.
  • / or empty - the replyTo default exchange and default routingKey.

   参考地址:http://docs.spring.io/spring-amqp/reference/htmlsingle/#async-annotation-driven-reply

 

Java代码  收藏代码
  1. import org.springframework.amqp.core.Message;  
  2. import org.springframework.amqp.rabbit.annotation.RabbitListener;  
  3. import org.springframework.stereotype.Component;  
  4.   
  5. /** 
  6.  * 队列监听器 
  7.  *  
  8.  * @author <a href="mailto:zhongqing.lin@zkteco.com">zhongqing.lin</a> 
  9.  * @version 0.0.0.1 
  10.  * @since 2015年3月30日 下午7:02:59 
  11.  */  
  12. @Component  
  13. public class QueueTwoLitener  
  14. {  
  15.   
  16.     @RabbitListener(queues = "queue_two", exclusive = false)  
  17.     //参数中使用@Header获取mesage  
  18.     public void onMessage(Message message)  
  19.     {  
  20.         System.out.println("queue_two data:" + new String(message.getBody()));  
  21.     }  
  22. }  

 

Java代码  收藏代码
  1. package com.test.rabbit.retry;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.apache.commons.lang3.StringUtils;  
  6. import org.springframework.amqp.core.Address;  
  7. import org.springframework.amqp.core.Message;  
  8. import org.springframework.amqp.core.MessageProperties;  
  9. import org.springframework.amqp.rabbit.core.RabbitTemplate;  
  10. import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;  
  11.   
  12. import com.test.utils.MessageUtil;  
  13. import com.test.utils.PropKeys;  
  14.   
  15. /** 
  16.  * 拒绝消息,并回复 
  17.  *  
  18.  * @version 0.0.0.1 
  19.  * @since 2015年4月21日 下午5:05:35 
  20.  */  
  21. public class ZkRejectAndRplyToRequeueRecoverer extends RejectAndDontRequeueRecoverer  
  22. {  
  23.     /** 用于发送拒绝消息状态给请求者 */  
  24.     RabbitTemplate replyToTemplate;  
  25.   
  26.     @Override  
  27.     public void recover(Message message, Throwable cause)  
  28.     {  
  29.         MessageProperties mp = message.getMessageProperties();  
  30.         if (mp != null && StringUtils.isNotBlank(mp.getReplyTo()) && replyToTemplate != null)  
  31.         {  
  32.             Map<String, Object> headers = mp.getHeaders();  
  33.             System.err.println(headers.toString());  
  34.             Object vLang = headers.get(PropKeys.LANG);  
  35.             String lang = "en";  
  36.             if (vLang != null)  
  37.             {  
  38.                 lang = (String) vLang;  
  39.             }  
  40.             com.test.utils.Message rejectRespMsg = new com.test.utils.Message(false);  
  41.             rejectRespMsg.setPayload(null);  
  42.             MessageUtil.changeResult(rejectRespMsg, "test.rabbit.replyto.interceptor.illegal.request", lang);  
  43.             Address address = new Address(mp.getReplyTo());  
  44.             replyToTemplate.convertAndSend(address.getExchangeName(), address.getRoutingKey(), rejectRespMsg);  
  45.         }  
  46.         super.recover(message, cause);  
  47.     }  
  48.   
  49.     public void setReplyToTemplate(RabbitTemplate replyToTemplate)  
  50.     {  
  51.         this.replyToTemplate = replyToTemplate;  
  52.     }  
  53.   
  54. }  

 



0 0
原创粉丝点击