springboot整合多个activemq

最近项目中出现了要整合多个ActiveMQ的需求,自己写了个基础的整合配置类,记录一下

增加如下配置类

/**
 * Created by wangyahui on 2017/05/15.
 */
@Configuration
public class ActiveMqConfig {


    @Bean(name = "firstConnectionFactory")
    @Primary
    public ConnectionFactory firstConnectionFactory(
            @Value("${spring.activemq.a.broker-url}") String url
    ) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        return connectionFactory;
    }

    @Bean(name = "secondConnectionFactory")
    public ConnectionFactory secondConnectionFactory(
            @Value("${spring.activemq.b.broker-url}") String url
    ) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        return connectionFactory;
    }

    @Bean(name = "firstTemplate")
    @Primary
    public JmsMessagingTemplate firstTemplate(
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
        JmsMessagingTemplate firstTemplate = new JmsMessagingTemplate(connectionFactory);
        return firstTemplate;
    }

    @Bean(name = "secondTemplate")
    public JmsMessagingTemplate secondRabbitTemplate(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
        JmsMessagingTemplate secondTemplate = new JmsMessagingTemplate(connectionFactory);
        return secondTemplate;
    }

    @Bean(name = "firstFactory")
    public JmsListenerContainerFactory jmsListenerContainerFactory(
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
        configurer.configure(containerFactory, connectionFactory);
        return containerFactory;
    }

    @Bean(name = "secondFactory")
    public JmsListenerContainerFactory jmsListenerContainerFactory2(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory containerFactory = new DefaultJmsListenerContainerFactory();
        configurer.configure(containerFactory, connectionFactory);
        return containerFactory;
    }
}

配置文件

spring:
  activemq:
    a:
      in-memory: true
      broker-url: tcp://192.168.15.9:62616
      user: admin
      password: admin
      pool:
         idle-timeout: 0
    b:
      in-memory: true
      broker-url: tcp://192.168.15.17:63616
      user: admin
      password: admin
      pool:
        idle-timeout: 0

生产者

@Component
public class Producer {

    @Autowired
    @Qualifier("firstTemplate")
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    @Qualifier("secondTemplate")
    private JmsMessagingTemplate jmsTemplate2;

    public void sendFirst(String queueName,String json, Map map) {
        this.jmsTemplate.convertAndSend(queueName,json, map);
    }

    public void sendSecond(String queueName,String json,Map map) {
        this.jmsTemplate2.convertAndSend(queueName, json,map);
    }
}

消费者

@Component
public class Consumer{

    @JmsListener(destination = Queues.LOCAL_Q, containerFactory = "firstFactory")
    public void receiveQueue(TextMessage message) {
        String json = "";
        Map propertyMap = new HashMap();
        try {
            json=message.getText();
            Enumeration et =  message.getPropertyNames();
            while(et.hasMoreElements()){
                String propertyName = et.nextElement()+"";
                propertyMap.put(propertyName,  message.getStringProperty(propertyName));
            }
            System.out.println("监听到第一个MQ的localQ的【信息】"+json);
            System.out.println("监听到第一个MQ的localQ的【属性】"+propertyMap);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    @JmsListener(destination = Queues.LOCAL_Q, containerFactory = "secondFactory")
    public void receiveQueue2(TextMessage message) {
        String json = "";
        Map propertyMap = new HashMap();
        try {
            json=message.getText();
            Enumeration et =  message.getPropertyNames();
            while(et.hasMoreElements()){
                String propertyName = et.nextElement()+"";
                propertyMap.put(propertyName,  message.getStringProperty(propertyName));
            }
            System.out.println("监听到第二个MQ的localQ的【信息】"+json);
            System.out.println("监听到第二个MQ的localQ的【属性】"+propertyMap);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

测试发送类

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestSend {
    @Autowired
    private Producer producer;

    @Test
    public void test(){
        Map map = new HashMap<>();
        map.put("name", "wyh");
        producer.sendFirst(Queues.LOCAL_Q,"发送给第一个MQ",map);
    }

    @Test
    public void test2(){
        Map map = new HashMap<>();
        map.put("name", "wyh");
        producer.sendSecond(Queues.LOCAL_Q,"发送给第二个MQ",map);
    }


}

原文链接:http://blog.csdn.net/sbuiwqvb123/article/details/72414070

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