springboot activemq

1.Factory配置Bean,包含Queue和Topic两种Factory。主要是设置PubSubDomain字段的设置(bean.setPubSubDomain(true))

JmsConfiguration.java

package com.thinvent.service;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;

import javax.jms.ConnectionFactory;

/**
 * Created by GongWenHua on 2017/10/20.
 */
@Configuration
@EnableJms
public class JmsConfiguration {
    /**
     * topic模式的ListenerContainer
     * @param activeMQConnectionFactory
     * @return
     */
    @Bean
    public JmsListenerContainerFactory jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        /**
         * 使用消息转换器
         */
        bean.setMessageConverter(jacksonJmsMessageConverter());
        return bean;
    }
    /**
     * queue模式的ListenerContainer
     * @param activeMQConnectionFactory
     * @return
     */
    @Bean
    public JmsListenerContainerFactory jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(activeMQConnectionFactory);
        /**
         * 使用消息转换器
         */
        bean.setMessageConverter(jacksonJmsMessageConverter());
        return bean;
    }
    /**
     * 消息转换器
     * @return
     */
    @Bean
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }
}

2.创建消息生产者,一个提交任意对象到Queue,一个提交任意对象到Topic,一般有这两种就够了。(个人之见)
MQProviderService.java

package com.thinvent.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Created by GongWenHua on 2017/10/20.
 */
@Component
public class MQProviderService {
    @Autowired
    private JmsTemplate jmsTemplate;
    public void send2Queue(String domainName,final Object  message){
        if(jmsTemplate.isPubSubDomain()){
            jmsTemplate.setPubSubDomain(false);
        }
        if(StringUtils.isEmpty(domainName)){
            jmsTemplate.convertAndSend(message);
        }else {
            jmsTemplate.convertAndSend(domainName,message);
        }
    }

    public void send2Topic(String domainName,final Object  message){
        if(!jmsTemplate.isPubSubDomain()){
            jmsTemplate.setPubSubDomain(true);
        }
        if(StringUtils.isEmpty(domainName)){
            jmsTemplate.convertAndSend(message);
        }else {
            jmsTemplate.convertAndSend(domainName,message);
        }
    }
}

3.创建要发送的两个自定义消息类
Message.java

package com.thinvent.pojo;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by GongWenHua on 2017/10/20.
 */
@Data
public class Message implements Serializable {
        // 序列化
        private static final long serialVersionUID = 1L;
        // 消息内容
        private Map alarm_event_mes;
        // 消息标题
        private String alarm_event_title;
        // 消息类型ID
        private Integer alarm_type_id;
        // 动作ID
        private String alarm_action_code;
        // 消息备注
        private String remark;

        public void add(String key,Object value){
            alarm_event_mes.put(key,value);
        }
        public Object get(String key){
            return alarm_event_mes.get(key);
        }

        public Message(){
            create_time = new Date();
            alarm_event_mes = new HashMap<>();
        }
}

ThirdMessage.java

package com.thinvent.pojo;

import lombok.Data;

import java.io.Serializable;

/**
 * Created by GongWenHua on 2017/10/20.
 */
@Data
public class ThirdMessage implements Serializable {
    private static final long serialVersionUID = 1L;
    String id;
    String type;
    Integer level;
}

4.针对两种消息分别创建队列消息消费者和主题消息消费者.
MQConsumerService.java

package com.thinvent.service;

import com.thinvent.pojo.Message;
import com.thinvent.pojo.ThirdMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

/**
 * Created by GongWenHua on 2017/10/20.
 */
@Service
public class MQConsumerService {

    @JmsListener(destination = "portal.admin.topic",containerFactory = "jmsListenerContainerTopic") // 监听指定消息主题
    public void receiveTopic(Message message) {
        System.out.println("portal.admin.topic");
        System.out.println(message);
    }

    @JmsListener(destination = "portal.admin.topic2",containerFactory = "jmsListenerContainerTopic") // 监听指定消息主题
    public void receiveTopic(ThirdMessage message) {
        System.out.println("portal.admin.topic2");
        System.out.println(message);
    }


    @JmsListener(destination = "portal.admin.queue",containerFactory = "jmsListenerContainerQueue") // 监听指定消息队列
    public void receiveQueue(Message message) {
        System.out.println("portal.admin.queue");
        System.out.println(message);
    }

    @JmsListener(destination = "portal.admin.queue2",containerFactory = "jmsListenerContainerQueue") // 监听指定消息队列
    public void receiveQueue(ThirdMessage message) {
        System.out.println("portal.admin.queue2");
        System.out.println(message);
    }
}

5.编写controller进行测试

MQController.java

package com.thinvent.controller;

import com.thinvent.pojo.Message;
import com.thinvent.pojo.ThirdMessage;
import com.thinvent.service.MQProviderService;
import com.thinvent.util.ResultVOUtil;
import com.thinvent.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * Created by GongWenHua on 2017/10/19.
 */
@RestController
public class MQController {

    @Autowired
    private MQProviderService mqProviderService;

    @GetMapping("/send/to/topic")
    public ResultVO sendToTopic() {
        Message message = new Message();
        message.setEquip_ip(UUID.randomUUID().toString());
        mqProviderService.send2Topic("portal.admin.topic",message);
        return ResultVOUtil.success("send ok");
    }
    @GetMapping("/send/to/queue")
    public ResultVO sendToQueue() {
        Message message = new Message();
        message.setEquip_ip(UUID.randomUUID().toString());
    mqProviderService.send2Queue("portal.admin.queue",message);
        return ResultVOUtil.success("send ok");
    }

    @GetMapping("/send/to/topic2")
    public ResultVO sendToTopic2() {
        ThirdMessage message = new ThirdMessage();
        message.setId(UUID.randomUUID().toString());
        mqProviderService.send2Topic("portal.admin.topic2",message);
        return ResultVOUtil.success("send ok");
    }

    @GetMapping("/send/to/queue2")
    public ResultVO sendToQueue2() {
        ThirdMessage message = new ThirdMessage();
        message.setId(UUID.randomUUID().toString());
        mqProviderService.send2Queue("portal.admin.queue2",message);
        return ResultVOUtil.success("send ok");
    }
}

6.测试结果
依次执行Controller中的方法。结果如下
Application.log

portal.admin.queue
Message(alarm_event_mes={}, alarm_event_title=null, alarm_type_id=null, alarm_action_code=null, alarm_action_name=null, create_time=Fri Oct 20 21:59:45 CST 2017, alarm_level_id=null, equip_ip=443c2f5b-2325-41e0-877c-87a09dc33c9b, remark=null)

portal.admin.queue2
ThirdMessage(id=e69cc7bb-b99e-44d2-9352-2170096c0236, type=null, level=null)

portal.admin.topic
Message(alarm_event_mes={}, alarm_event_title=null, alarm_type_id=null, alarm_action_code=null, alarm_action_name=null, create_time=Fri Oct 20 21:59:55 CST 2017, alarm_level_id=null, equip_ip=83d81e99-b7a6-4a11-90a1-973fa3493d29, remark=null)

portal.admin.topic2
ThirdMessage(id=d22a6fb7-0292-4f5a-9abc-c02be8d7306a, type=null, level=null)

你可能感兴趣的:(activemq,activemq-spring)