springboot activemq之消费者

###springboot 中 activemq之消费者监听

package com.ciwei.psm.util.activemq;

import com.ciwei.psm.thriftApi.EmailAllTask;
import com.ciwei.psm.thriftApi.WeChatAllTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
* @author fuhang
* @description: TODO
* @date 2019/7/2621:31
*/
@Component
public class ActiveMQConsumer {
   private final Logger logger = LoggerFactory.getLogger(ActiveMQConsumer.class);
   private static final String destination = ActiveMqName.EMAIL_ALL_TASK;

   @JmsListener(destination = destination,containerFactory = "queueListenerFactory")
   public void receiveEmailQueue(EmailAllTask emailAllTask) {
       logger.info("consumer1收到queue_email信息:{}",emailAllTask.toString());
   }
   @JmsListener(destination = ActiveMqName.PUSH_ALL_TASK,containerFactory = "queueListenerFactory")
   public void receiveAppQueue(Object object) {
       logger.info("consumer1收到queue_app信息:{}",object.toString());
   }
   @JmsListener(destination = ActiveMqName.SMS_ALL_TASK,containerFactory = "queueListenerFactory")
   public void receivePhoneQueue(Object object) {
       logger.info("consumer1收到queue_phone信息:{}",object.toString());
   }
   @JmsListener(destination = ActiveMqName.WEIXIN_ALL_TASK,containerFactory = "queueListenerFactory")
   public void receiveWeChatQueue(Object object) {
       WeChatAllTask weChatAllTask = (WeChatAllTask) object;
       logger.info("consumer1收到queue_WeChat信息:{}",weChatAllTask.toString());
   }

   @JmsListener(destination = "queue_string_2way_test",containerFactory = "queueListenerFactory")
   @SendTo("queue_string_return_test")
   public String receive2WayQueue(String text) {
       logger.info("consumer1收到queue_string_2way信息:{}",text);
       return "queue_string_2way_test已收到消息:" + text + ",请进行下一步操作";
   }

   @JmsListener(destination = "queue_string_ack_test",containerFactory = "queueListenerACKFactory")
   public void receiveQueue(TextMessage message, Session session) throws JMSException {
       logger.info("consumer2收到queue_string信息:{}",message.getText());
       try {
           int a = 1 / 0;
           //业务处理结束,手动ack通知队列收到消息,可以把消息从队列移除
           message.acknowledge();
       } catch (Exception e) {
           logger.error(e.getMessage());
           /**
            * 通知队列重发,默认每秒重发1次,一共重发6次,
            * 若想自定义重试次数,重试间隔时间可以设置
            * ActiveMQConnectionFactory的RedeliveryPolicyMap的RedeliveryPolicy
            */
           session.recover();
       }
   }
}

你可能感兴趣的:(springboot activemq之消费者)