全java代码实现Spring与ActiveMQ整合
来源:互联网 发布:pubmed数据库主题词 编辑:程序博客网 时间:2024/04/27 19:51
关于其他的配置就不做介绍了,这里主要介绍Spring与ActiveMQ整合部分的配置
在ApplicationConfig.java中添加如下代码:
//配置ConnectionFactory @Bean public ConnectionFactory connectionFactory() { ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(); mqConnectionFactory.setBrokerURL("tcp://localhost:6166");//mq的服务器地址 mqConnectionFactory.setUserName("admin");//用户名 mqConnectionFactory.setPassword("admin");//密码 return mqConnectionFactory; }//配置消息队列Queue @Bean public Destination queueDestination() { ActiveMQQueue activeMQQueue = new ActiveMQQueue(); activeMQQueue.setPhysicalName("queue1"); return activeMQQueue; }//发送消息配置//配置JmsTemplate @Bean public JmsTemplate jmsTemplate() { JmsTemplate jmsTemplate = new JmsTemplate(); jmsTemplate.setConnectionFactory(connectionFactory()); jmsTemplate.setDefaultDestination(queueDestination()); jmsTemplate.setReceiveTimeout(10000); return jmsTemplate; } @Bean public Destination queueDestination2() { ActiveMQQueue activeMQQueue = new ActiveMQQueue(); activeMQQueue.setPhysicalName("queue2"); return activeMQQueue; } @Bean public QueueMessageListener queueMessageListener() { QueueMessageListener queueMessageListener = new QueueMessageListener(); return queueMessageListener; } @Bean public DefaultMessageListenerContainer jmsContainer() { DefaultMessageListenerContainer listenerContainer = new DefaultMessageListenerContainer(); listenerContainer.setConnectionFactory(connectionFactory()); listenerContainer.setDestination(queueDestination()); listenerContainer.setMessageListener(queueMessageListener()); return listenerContainer; } @Bean public ActiveMQTopic topicDestination() { ActiveMQTopic activeMQTopic = new ActiveMQTopic(); activeMQTopic.setPhysicalName("publics_topic"); return activeMQTopic; } @Bean public ActiveMQTopic dpt_topic() { ActiveMQTopic activeMQTopic = new ActiveMQTopic(); activeMQTopic.setPhysicalName("PUBLICS.department_topic"); return activeMQTopic; } @Bean public ActiveMQTopic job_topic() { ActiveMQTopic activeMQTopic = new ActiveMQTopic(); activeMQTopic.setPhysicalName("PUBLICS.job_topic"); return activeMQTopic; } @Bean public ActiveMQTopic pes_topic() { ActiveMQTopic activeMQTopic = new ActiveMQTopic(); activeMQTopic.setPhysicalName("PUBLICS.person_topic"); return activeMQTopic; } @Bean public JmsTemplate topicJmsTemplate() { JmsTemplate jmsTemplate = new JmsTemplate(); jmsTemplate.setConnectionFactory(connectionFactory()); jmsTemplate.setDefaultDestination(topicDestination()); jmsTemplate.setPubSubDomain(true); jmsTemplate.setReceiveTimeout(10000); return jmsTemplate; } @Bean public CachingConnectionFactory cachingConnectionFactory() { CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); cachingConnectionFactory.setTargetConnectionFactory(connectionFactory()); cachingConnectionFactory.setSessionCacheSize(100); return cachingConnectionFactory; } @Bean public JmsTemplate persistentTopicjmsTemplate() { JmsTemplate jmsTemplate = new JmsTemplate(); jmsTemplate.setConnectionFactory(cachingConnectionFactory()); jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT); jmsTemplate.setPubSubDomain(true); return jmsTemplate; }
使用方法:
1.引入topic
@Autowired
private TopicProvider topicProvider;
2.发送信息
public void mqService(Object o){SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");ObjectMapper mapper = new ObjectMapper();try {String message = df.format(new Date())+ mapper.writeValueAsString(o);topicProvider.publish2(department_topic, message);} catch (JsonProcessingException e) {e.printStackTrace();}}
1 0
- 全java代码实现Spring与ActiveMQ整合
- Spring与ActiveMQ整合
- Spring与ActiveMQ整合
- ActiveMQ 与spring整合
- Spring与ActiveMQ整合
- activemq与spring整合
- ActiveMQ与Spring整合
- Spring与ActiveMQ的整合
- Spring与ActiveMQ的整合
- JMS-ActiveMq与Spring整合
- ActiveMQ 与spring整合使用
- ActiveMQ安装与Spring整合
- Spring Boot与ActiveMQ整合
- activemq与spring的整合
- ActiveMQ实战(五)--Spring整合ActiveMQ实现点对点与主题发布订阅通信
- spring整合JMS - 基于ActiveMQ实现
- spring整合JMS - 基于ActiveMQ实现
- spring整合JMS - 基于ActiveMQ实现
- 0/1背包问题
- Python的sys.stdout、sys.stdin重定向
- iOS APP多语言开发(国际化)
- c++ getline正确读取多行内容
- Makefile 教程
- 全java代码实现Spring与ActiveMQ整合
- linux常用解压压缩命令
- JAVA从一个.p12或.pfx文件中获取公钥和私钥
- (一)linux常用,但容易忘记的命令
- jenkins配置用户名和密码
- 浏览器 HTTP 协议缓存机制详解
- 利用python爬虫大量基金股票的净值写入到csv文件中方便做其他计算
- 游标
- python+beautifulsoup/xpath实现新浪微博某博主全部高清大图下载器