Spring整合MQ配置是什么
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,Spring整合MQ配置是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。import org.apache.activemq.c
千家信息网最后更新 2025年12月03日Spring整合MQ配置是什么
Spring整合MQ配置是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
import org.apache.activemq.command.ActiveMQQueue;import org.apache.activemq.command.ActiveMQTopic;import org.apache.activemq.pool.PooledConnectionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.listener.*;import org.springframework.jms.support.converter.SimpleMessageConverter;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.EnableScheduling;import javax.jms.*;@Configuration@EnableAsync@EnableSchedulingpublic class MQConfig { /** * 配置队列消息模板 * @param jmsFactory * @return */ @Bean public JmsTemplate queueTemplate(@Autowired ConnectionFactory jmsFactory, @Autowired ActiveMQQueue activeMQQueue) { var template = new JmsTemplate(jmsFactory); template.setDefaultDestination(activeMQQueue); template.setMessageConverter(new SimpleMessageConverter()); return template; } /** * 配置主题消息模板 * @param jmsFactory * @param activeMQTopic * @return */ @Bean public JmsTemplate topicTemplate(@Autowired ConnectionFactory jmsFactory, @Autowired ActiveMQTopic activeMQTopic) { var template = new JmsTemplate(jmsFactory); template.setDefaultDestination(activeMQTopic); template.setMessageConverter(new SimpleMessageConverter()); return template; } /** * 配置消息生产者 * @return */ @Bean public PooledConnectionFactory JmsFactory() { return new PooledConnectionFactory(); } /** * 配置队列消息 * @return */ @Bean public ActiveMQQueue activeMQQueue() { return new ActiveMQQueue(); } /** * 配置主题消息 * @return */ @Bean public ActiveMQTopic activeMQTopic() { return new ActiveMQTopic(); } /** * 配置监听器 * @return */ @Bean public MessageListener messageListener() { MessageListener messageListener = new MessageListener() { @Override public void onMessage(Message message) { if (null != message && message instanceof TextMessage) { try { ((TextMessage) message).getText(); } catch (JMSException e) { e.printStackTrace(); } } } }; return messageListener; } /** * 配置监听容器 * @param jmsFactory * @param activeMQQueue * @param messageListener * @return */ @Bean public DefaultMessageListenerContainer jmsContainer(@Autowired ConnectionFactory jmsFactory, @Autowired ActiveMQQueue activeMQQueue, @Autowired MessageListener messageListener) { DefaultMessageListenerContainer container = new DefaultMessageListenerContainer(); container.setConnectionFactory(jmsFactory); container.setDestination(activeMQQueue); container.setMessageListener(messageListener); return container; }}import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.jms.core.JmsTemplate;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;import javax.jms.TextMessage;@Servicepublic class MQ { @Autowired@Qualifier("queueTemplate") private JmsTemplate queueTemplate; @Autowired@Qualifier("topicTemplate") private JmsTemplate topicTemplate; @Async public void queueProducer(){ queueTemplate.send(session -> { TextMessage textMessage = session.createTextMessage("*****"); return textMessage; }); } @Async public void queueConsumer(){ queueTemplate.receiveAndConvert(); } @Async public void topicProducer(){ topicTemplate.send(session -> { TextMessage textMessage = session.createTextMessage("*****"); return textMessage; }); } @Async public void topicConsumer(){ topicTemplate.receiveAndConvert(); }}关于Spring整合MQ配置是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
配置
消息
问题
整合
主题
更多
模板
队列
帮助
监听
解答
易行
简单易行
内容
容器
小伙
小伙伴
方法
生产者
监听器
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
杭州图景网络技术公司
清华大学网络安全机试
哪里有云服务器代理加盟平台
鸣汇广盛互联网科技
梦间集选择哪个服务器
网站的服务器端技术
国家网络安全培训中心
票房 艺恩数据库
麦芒网络技术广州
数据库 姓氏
网络安全基础书籍推荐
软件开发人数分布
服务器网络安全机制
数字认证网络安全产业
老牌软件开发培训班
网络技术授课总结
河南互联网养老软件开发专业制作
北京智慧团建软件开发专业制作
全国特殊设备从业人员数据库
戴尔服务器转速很高
哪里有云服务器代理加盟平台
nginx 共享数据库
南京网络安全中心
金山区库存网络技术维保
网吧服务器和其他客机连接
邵阳网络安全攻防
长沙哪里学软件开发比较专业
数据库创建的微课视频
网络安全自查排查
7000计算机网络技术