SpringBoot 整合 JMSTemplate的示例代碼
1.1 添加依賴
可以手動在 SpringBoot 項目添加依賴,也可以在項目創(chuàng)建時選擇使用 ActiveMQ 5 自動添加依賴。高版本 SpringBoot (2.0 以上) 在添加 activemq 連接池依賴啟動時會報 Error creating bean with name 'xxx': Unsatisfied dependency expressed through field 'jmsTemplate'; 可以將 activemq 連接池換成 jms 連接池解決。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- activemq 連接池 --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency> <!-- jms 連接池 --> <dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency>

1.2 添加配置
spring: activemq: broker-url: tcp://127.0.0.1:61616 # 是否是內(nèi)存模式 in-memory: false pool: # 是否用 PooledConnectionFactory 代替普通的 ConnectionFactory enabled: true # 最大連接數(shù) max-connections: 10 # 連接空閑超時 idle-timeout: 30000
1.3 測試類
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/8/5
* @description MQ 測試
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class MyMQTest {
@Autowired
private JmsTemplate jmsTemplate;
@Test
public void jms() {
jmsTemplate.convertAndSend(new ActiveMQQueue("myTest"), "測試消息");
}
}
1.4 運行結(jié)果



到此這篇關(guān)于SpringBoot 整合 JMSTemplate的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot 整合 JMSTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot隨機數(shù)設(shè)置及參數(shù)間引用的操作步驟
在Spring Boot配置文件中設(shè)置屬性時,除了可以像前面示例中顯示的配置屬性值外,還可以使用隨機值和參數(shù)間引用對屬性值進行設(shè)置。下面給大家介紹SpringBoot參數(shù)間引用隨機數(shù)設(shè)置的操作步驟,感興趣的朋友一起看看吧2021-06-06
Java實現(xiàn)SMS短信通發(fā)送手機驗證碼案例講解
這篇文章主要介紹了Java實現(xiàn)SMS短信通發(fā)送手機驗證碼案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
java與微信小程序?qū)崿F(xiàn)websocket長連接
這篇文章主要為大家詳細介紹了java與微信小程序?qū)崿F(xiàn)websocket長連接,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05

