java 配置jmstemplate_SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解...
1.导入jar包:
org.springframework.boot
spring-boot-starter-activemq
org.apache.activemq
activemq-pool
2.填写配置文件(application.properties)
#设置JMS(AMQ)
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=true
#spring.jms.pub-sub-domain=true
spring.activemq.pool.max-connections=50
spring.activemq.pool.expiry-timeout=10000
spring.activemq.pool.idle-timeout=30000
上面需要注意的是,如果开启订阅者和发布者模式下面的代码会使监听器失效。
3.编写控制器代码
@RestController
@RequestMapping("/Jms")
public class ProducerController {
@Autowired
private JmsProducerService jmsProducerService;
@RequestMapping("/send")
public void contextLoads() throws InterruptedException {
Destination destination = new ActiveMQQueue("mytest.queue");
for(int i=0; i<10; i++){
jmsProducerService.sendMessage(destination, "我是超人啊");
}
System.out.println("发送成功");
}
}
4.服务层代码:
package com.zzf.finals.service.impl;
import com.zzf.finals.service.JmsProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
@Service
public class JmsProducerServiceImpl implements JmsProducerService {
@Autowired
private JmsTemplate jmsTemplate;
@Override
public void sendMessage(Destination destination, String message) {
this.jmsTemplate.convertAndSend(destination,message);
}
}
5.最后加上监听器类
package com.zzf.finals.domain;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Message:"+text);
}
}
OK~
但是这样有另外一个问题:如果开启了订阅者和发布者模式则无法发送和接收queue消息。
这里我提供两种写法xml和java配置:
首先贴上我的xml配置代码
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
destroy-method="stop">
tcp://localhost:61616
JavaConfig配置为:
package com.zzf.finals.domain;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
public class JmsConfig {
public final static String TOPIC = "topic.test";
public final static String QUEUE = "queue.test";
@Bean
public Queue queue() {
return new ActiveMQQueue(QUEUE);
}
@Bean
public Topic topic() {
return new ActiveMQTopic(TOPIC);
}
// topic模式的ListenerContainer
@Bean
public JmsListenerContainerFactory> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
// queue模式的ListenerContainer
@Bean
public JmsListenerContainerFactory> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
}
控制台代码为:
package com.zzf.finals.controller;
import com.zzf.finals.service.JmsProducerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
@RestController
@RequestMapping("/Jms")
public class ProducerController {
@Autowired
private JmsProducerService jmsProducerService;
@Autowired
private Topic topic;
@Autowired
private Queue queue;
@Autowired
private Topic destinationTopic;
@Autowired
private Queue destinationQueue;
@RequestMapping("/send3")
public void testJms2() {
for (int i=0;i<10;i++) {
jmsProducerService.sendMessage(destinationQueue,"queue,world!" + i);
jmsProducerService.sendMessage(destinationTopic, "topic,world!" + i);
}
}
@RequestMapping("/send2")
public void testJms() {
for (int i=0;i<10;i++) {
jmsProducerService.sendMessage(queue,"queue,world!" + i);
jmsProducerService.sendMessage(topic, "topic,world!" + i);
}
}
@RequestMapping("/send")
public void contextLoads() throws InterruptedException {
Destination destination = new ActiveMQQueue("mytest.queue");
for(int i=0; i<10; i++){
jmsProducerService.sendMessage(destination, "我是超人啊");
}
System.out.println("发送成功");
}
}
最后的监听器类:
package com.zzf.finals.domain;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Message:"+text);
}
@JmsListener(destination = JmsConfig.TOPIC,containerFactory = "jmsListenerContainerTopic")
public void onTopicMessage(String msg) {
System.out.println("topic:"+msg);
}
@JmsListener(destination = JmsConfig.QUEUE,containerFactory = "jmsListenerContainerQueue")
public void onQueueMessage(String msg) {
System.out.println("queue:"+msg);
}
@JmsListener(destination = "spring-topic",containerFactory = "TopicContainers")
public void onTopicMessageXML(String msg) {
System.out.println("topic1:"+msg);
}
@JmsListener(destination = "spring-topic",containerFactory = "TopicContainers")
public void onTopicMessageXML2(String msg) {
System.out.println("topic2:"+msg);
}
@JmsListener(destination = "spring-queue",containerFactory = "QueueContainers")
public void onQueueMessageXML(String msg) {
System.out.println("queue:"+msg);
}
}
OK~JmsTemplate的使用和配置Demo就完成了 ,有兴趣的可以自己跑下试试
总结
到此这篇关于SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解的文章就介绍到这了,更多相关SpringBoot集成JmsTemplate内容请搜索聚米学院以前的文章或继续浏览下面的相关文章希望大家以后多多支持聚米学院!
java 配置jmstemplate_SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解...相关推荐
- 五、交换机 与 发布/订阅模式、路由模式、主题模式
RabbitMQ目录 文章目录 交换机 与 发布/订阅模式.路由模式.主题模式 1.Exchanges(交换机) 1.1. Exchanges 概念 1.2. Exchanges 的类型 1.3. 无 ...
- 【JAVA秘籍心法篇-Spring】Spring XML解析源码详解
[JAVA秘籍心法篇-Spring]Spring XML解析源码详解 所谓天下武功,无坚不摧,唯快不破.但有又太极拳法以快制慢,以柔克刚.武功外式有拳打脚踢,刀剑棍棒,又有内功易筋经九阳神功.所有外功 ...
- 丢掉xml使用JavaConfig配置Spring
Spring JavaConfig 最近撸了一遍Spring action 4,发现里面讲的都不再使用xml文件来配置spring,全都采用Java代码来配置. 用Java代码配置的话,感觉要比xml ...
- java 会话跟踪技术_JavaEE基础(04):会话跟踪技术,Session和Cookie详解
一.会话跟踪 1.场景描述 比如登录某个购物网站,身份识别成功后,在网站下单,支付 等操作,这些操作中当前登录用户信息必须是共享的,这样这些操作结果才能和登录用户做关联. 2.概念简介 可以把会话理解 ...
- 经验模式分解(EMD)的MATLAB工具箱安装详解
经验模式分解(EMD)的MATLAB工具箱安装详解 MATLAB工具箱安装 时频分析工具箱 安装EMD工具箱 代码检验是否安装成功 MATLAB工具箱安装 首先,本文参考CSDN这篇文章https:/ ...
- 数据库系统模式(schema)和模型(model)详解
数据库系统模式(schema)和模型(model)详解 数据(data)是描述事物的符号记录. 模型(Model)是现实世界的抽象. 数据模型(Data Model)是数据特征的抽象,是数据库管 ...
- SpringBoot集成JmsTemplate(队列模式和主题模式)(xml和JavaConfig配置实现)
1.导入jar包: <!--jmsTemplate--><dependency><groupId>org.springframework.boot</grou ...
- 【转】RabbitMQ六种队列模式-5.主题模式
前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列模式-主题 ...
- java.lang.OutOfMemoryError: PermGen space基于idea,MyEclipse或tomcat的详细解决方法以及详解JVM配置参数
文章目录 1. 引出问题 2. 分析问题 3. 解决问题 3.1 手动设置tomcat的MaxPermSize大小 3.2 基于MyEclipse的修改 3.3 基于idea的修改 4. jvm配置参 ...
最新文章
- Infortrend的EonStor DS与GS强势进军银行领域
- Delphi XE5 for Android (十一)
- 论文浅尝 | 面向时序知识图谱推理的循环事件网络
- POJ 1163 The Triangle
- 【Android】Listview返回顶部,快速返回顶部的功能实现,详解代码。
- VS2008内嵌水晶报表翻页BUG修正方法
- 算法设计与分析基础(三)
- Python爬取链家成都二手房源信息 asyncio + aiohttp 异步爬虫实战
- python导入鸢尾花数据集_python 鸢尾花数据集报表展示
- 什么是ipo表,ipo图,hipo图
- Alfa eBooks Manager(电子书管理软件)v8.1.7.3官方版
- 手机号码邮箱 验证规则(最新)
- 使用matlab在图片上画框,并保存
- php引用符号详解——————给变量起小名
- 迭代器 iter()函数与next()函数 for...in...循环的本质
- 游戏里的答题都有哪些类型
- 【笔记】 C++中 方向键的输入
- 18_一文总结Flask语法
- 和我一起学Python,一起采集妹子图
- Hadoop自动化安装脚本
热门文章
- 湖南工大11级C语言网上作业,湖南工大11级C语言网上作业之《最简单的程序设计》.docx...
- 博途变量类型_PLC数据类型(UDT)
- azure webjob java_使用 WebJobs 运行后台任务 | Azure Docs
- 学.net还是php,ASP.NET和php哪个更容易学
- linux驱动日志格式,( 转)嵌入式Linux驱动Makefile
- java数组更新_java刷新数组到jList
- python图像文件压缩_python实现图片压缩代码实例
- spring源码分析第五天------springAOP核心原理及源码分析
- 嵌入式全栈工程师_我花了半个月,整理出了这篇嵌入式开发学习指南(学习路线+知识点梳理)...
- 安卓与Linux共存,Android和Linux重新合并成一个操作系统