热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解

这篇文章主要介绍了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配置代码

<&#63;xml version="1.0" encoding="UTF-8"&#63;>

 
 
 
  
   
    
     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<&#63;> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
  DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
  bean.setPubSubDomain(true);
  bean.setConnectionFactory(activeMQConnectionFactory);
  return bean;
 }
 // queue模式的ListenerContainer
 @Bean
 public JmsListenerContainerFactory<&#63;> 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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


推荐阅读
  • 标题: ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • Java如何导入和导出Excel文件的方法和步骤详解
    本文详细介绍了在SpringBoot中使用Java导入和导出Excel文件的方法和步骤,包括添加操作Excel的依赖、自定义注解等。文章还提供了示例代码,并将代码上传至GitHub供访问。 ... [详细]
  • 本文介绍了在Win10上安装WinPythonHadoop的详细步骤,包括安装Python环境、安装JDK8、安装pyspark、安装Hadoop和Spark、设置环境变量、下载winutils.exe等。同时提醒注意Hadoop版本与pyspark版本的一致性,并建议重启电脑以确保安装成功。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • 本文介绍了Java的集合及其实现类,包括数据结构、抽象类和具体实现类的关系,详细介绍了List接口及其实现类ArrayList的基本操作和特点。文章通过提供相关参考文档和链接,帮助读者更好地理解和使用Java的集合类。 ... [详细]
  • 本文介绍了电流源并联合并的方法,以及谐振电路的原理。谐振电路具有很强的选频能力,通过将电感和电容连接在一起,电流和电压会产生震荡。谐振频率的大小取决于电感和电容的大小,而电路中的电阻会逐渐降低震荡的幅度。电阻和电容组成的电路中,当电容放完电后,电阻两端的电压为0,电流不再流过电容。然而,电感是一种特殊的器件,当有电流流过时,线圈会产生感应磁场,阻止电流的流动,从而使电流不会减小。 ... [详细]
  • 单点登录原理及实现方案详解
    本文详细介绍了单点登录的原理及实现方案,其中包括共享Session的方式,以及基于Redis的Session共享方案。同时,还分享了作者在应用环境中所遇到的问题和经验,希望对读者有所帮助。 ... [详细]
  • 本文介绍了在Docker容器技术中限制容器对CPU的使用的方法,包括使用-c参数设置容器的内存限额,以及通过设置工作线程数量来充分利用CPU资源。同时,还介绍了容器权重分配的情况,以及如何通过top命令查看容器在CPU资源紧张情况下的使用情况。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
author-avatar
耿睿---疯子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有