springboot整合多个activemq
来源:互联网 发布:ajax传递数组 编辑:程序博客网 时间:2024/06/05 15:42
最近项目中出现了要整合多个ActiveMQ的需求,自己写了个基础的整合配置类,记录一下
增加如下配置类
/** * Created by wangyahui on 2017/05/15. */@Configurationpublic 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
生产者
@Componentpublic 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); }}
消费者
@Componentpublic class Consumer{ @JmsListener(destination = Queues.LOCAL_Q, containerFactory = "firstFactory") public void receiveQueue(TextMessage message) { String json = ""; Map<String, String> 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<String, String> 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
阅读全文
1 0
- springboot整合多个activemq
- springboot、mybatis、activemq整合
- SpringBoot 整合 Apache ActiveMQ
- springboot整合activeMQ
- springboot整合activemq
- Springboot整合ActiveMQ
- SpringBoot 整合ActiveMQ 小Demo
- springboot整合多个mybatis数据源
- SpringBoot-ActiveMq
- springboot activemq
- ActiveMQ整合Spring(多消费者)
- SpringBoot+ActiveMQ多消息队列监听
- 整合ActiveMQ
- springboot quartz 多任务整合
- [ActiveMQ][SpringBoot]SpringBoot中集成ActiveMQ
- SpringBoot整合quartz(支持多个任务和job支持spring管理的对象)
- springboot整合activemq,应答模式,消息重发机制,消息持久化
- SpringBoot集成ActiveMQ
- Task+ConcurrentQueue+Parallel多线程编程
- git push -u orgin master报错
- java生成两种二维码
- uC/OS-II源码解析(os_q.c)
- SharePoint Framework 企业向导(三)
- springboot整合多个activemq
- SharePoint Framework 企业向导(四)
- Collections排序List<Map>
- Kafka 设计与原理详解
- squid
- SharePoint Framework 企业向导(五)
- 响应式布局css写法
- JVM性能优化, Part 3 垃圾回收
- 1031. 查验身份证(15) PAT