最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成JmsTemplate(隊(duì)列模式和主題模式)及xml和JavaConfig配置詳解

 更新時(shí)間:2020年08月06日 17:20:07   作者:蛇皮皮蛋  
這篇文章主要介紹了SpringBoot集成JmsTemplate(隊(duì)列模式和主題模式)及xml和JavaConfig配置詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.導(dǎo)入jar包:

 <!--jmsTemplate-->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-activemq</artifactId>
  </dependency>
  <dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-pool</artifactId>
  </dependency>

2.填寫配置文件(application.properties)

#設(shè)置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

上面需要注意的是,如果開啟訂閱者和發(fā)布者模式下面的代碼會使監(jiān)聽器失效。

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("發(fā)送成功");
 }
 
}

4.服務(wù)層代碼:

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.最后加上監(jiān)聽器類

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~

但是這樣有另外一個問題:如果開啟了訂閱者和發(fā)布者模式則無法發(fā)送和接收queue消息。

這里我提供兩種寫法xml和java配置:

首先貼上我的xml配置代碼

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  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">
 
 <!--連接池,內(nèi)部引入一個連接工廠-->
 <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
   destroy-method="stop">
  <property name="connectionFactory">
   <bean class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL">
     <value>tcp://localhost:61616</value>
    </property>
   </bean>
  </property>
  <property name="maxConnections" value="100"></property>
 </bean>
 
 
 <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
  <constructor-arg name="name" value="spring-queue"/>
 </bean>
 
 <!--測試Topic-->
 <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
  <constructor-arg name="name" value="spring-topic"/>
 </bean>
 
 <!--配置消息容器-->
 <bean id="TopicContainers" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
  <property name="pubSubDomain" value="true"/>
  <property name="connectionFactory" ref="jmsFactory"/>
 </bean>
 
 <!--配置隊(duì)列消息容器-->
 <bean id="QueueContainers" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
  <property name="connectionFactory" ref="jmsFactory"/>
 </bean>
 
</beans>

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("發(fā)送成功");
 }
}

最后的監(jiān)聽器類:

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就完成了 ,有興趣的可以自己跑下試試

總結(jié)

到此這篇關(guān)于SpringBoot集成JmsTemplate(隊(duì)列模式和主題模式)及xml和JavaConfig配置詳解的文章就介紹到這了,更多相關(guān)SpringBoot集成JmsTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 確保某個Bean類被最后執(zhí)行的幾種實(shí)現(xiàn)方式

    Java 確保某個Bean類被最后執(zhí)行的幾種實(shí)現(xiàn)方式

    這篇文章主要介紹了Java 確保某個BeanDefinitionRegistryPostProcessor Bean被最后執(zhí)行的幾種實(shí)現(xiàn)方式,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • Java如何通過SSE實(shí)現(xiàn)消息推送詳解

    Java如何通過SSE實(shí)現(xiàn)消息推送詳解

    這篇文章主要介紹了Java如何通過SSE實(shí)現(xiàn)消息推送的相關(guān)資料,SSE是一種服務(wù)器向客戶端推送數(shù)據(jù)的技術(shù),基于HTTP協(xié)議,利用長連接特性,它適用于單向數(shù)據(jù)流場景,如股票價(jià)格更新、新聞實(shí)時(shí)推送等,需要的朋友可以參考下
    2025-04-04
  • Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解

    Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解

    這篇文章主要介紹了Struts2實(shí)現(xiàn)自定義攔截器的三種方式詳解,一些與系統(tǒng)邏輯相關(guān)的通用功能如權(quán)限的控制和用戶登錄控制等,需要通過自定義攔截器實(shí)現(xiàn),本節(jié)將詳細(xì)講解如何自定義攔截器,需要的朋友可以參考下
    2023-07-07
  • Spring?Boot與Spring?MVC?Spring對比及核心概念

    Spring?Boot與Spring?MVC?Spring對比及核心概念

    這篇文章主要為大家介紹了Spring?Boot與Spring?MVC?Spring的對比以及你需要了解的核心概念,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-03-03
  • Java volatile 關(guān)鍵字介紹與使用示例詳解

    Java volatile 關(guān)鍵字介紹與使用示例詳解

    這篇文章詳細(xì)介紹了Java中的volatile關(guān)鍵字,包括它的核心特性、如何保證變量的可見性和有序性,以及它在解決多線程問題中的局限性,文章通過示例展示了如何在實(shí)際編程中使用volatile,并解釋了如何通過其他同步機(jī)制來彌補(bǔ)volatile的不足,感興趣的朋友一起看看吧
    2025-01-01
  • springboot的controller層的常用注解說明

    springboot的controller層的常用注解說明

    這篇文章主要介紹了springboot的controller層的常用注解說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Java多線程的原子性,可見性,有序性你都了解嗎

    Java多線程的原子性,可見性,有序性你都了解嗎

    這篇文章主要為大家詳細(xì)介紹了Java多線程的原子性,可見性,有序性,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Mybatis 動態(tài)sql if 判讀條件等于一個數(shù)字的案例

    Mybatis 動態(tài)sql if 判讀條件等于一個數(shù)字的案例

    這篇文章主要介紹了Mybatis 動態(tài)sql if 判讀條件等于一個數(shù)字的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • mybatis寫xml時(shí)數(shù)字類型千萬別用 !=‘‘(不為空串)進(jìn)行判斷的示例詳解

    mybatis寫xml時(shí)數(shù)字類型千萬別用 !=‘‘(不為空串)進(jìn)行判斷的示例詳解

    這篇文章主要介紹了mybatis寫xml時(shí)數(shù)字類型千萬別用 !=‘‘(不為空串)進(jìn)行判斷的示例詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 分析Netty直接內(nèi)存原理及應(yīng)用

    分析Netty直接內(nèi)存原理及應(yīng)用

    Netty作為一個流行的應(yīng)用框架,它的強(qiáng)悍之處是性能強(qiáng)悍,可以輕松承載數(shù)萬并發(fā); 其編程模型簡單,容易上手; 這就給大家打開了一扇通向高性能的大門。高效io模型略去不說,我們今天主要來看看內(nèi)存控制這塊的強(qiáng)大之處
    2021-06-06

最新評論

塘沽区| 卓资县| 扎鲁特旗| 广汉市| 怀集县| 铜梁县| 海淀区| 邯郸市| 湖口县| 津市市| 镇赉县| 阿拉善左旗| 广东省| 怀远县| 宣威市| 民丰县| 井冈山市| 腾冲县| 方正县| 宁南县| 乐平市| 工布江达县| 若羌县| 明光市| 罗平县| 阳西县| 武功县| 同心县| 特克斯县| 饶河县| 彭阳县| 菏泽市| 廉江市| 三门峡市| 万宁市| 常德市| 新绛县| 伊宁县| 吉水县| 庄浪县| 威远县|