您好,登錄后才能下訂單哦!
Java SpringBoot如何整合ActiveMQ,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
一、 如果要想在項(xiàng)目之中去使用 ActiveMQ 組件,則應(yīng)該為項(xiàng)目添加依賴支持庫,修改 pom.xml 配置文件:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
二、修改 application.yml 配置文件進(jìn)行 activemq 的配置;
server: port: 80 spring: messages: basename: i18n/Messages,i18n/Pages jms: pub-sub-domain: false # 配置消息的類型,如果是true則表示為topic消息,如果為false表示Queue消息 activemq: user: studyjava # 連接用戶名 password: hello # 連接密碼 broker-url: tcp://activemq-server:61616 # 消息組件的連接主機(jī)信息
三、 隨后定義一個消息的消費(fèi)者,消費(fèi)者主要是進(jìn)行一個監(jiān)聽控制,在 SpringBoot 里面可以直接利用注解@JmsListener進(jìn)行監(jiān)聽:
package cn.study.microboot.consumer; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Service; @Service public class MessageConsumerService { @JmsListener(destination="study.msg.queue") public void receiveMessage(String text) { // 進(jìn)行消息接收處理 System.err.println("【*** 接收消息 ***】" + text); } }
四、 隨后建立消息的發(fā)送者服務(wù),一般而言如果進(jìn)行消息的發(fā)送往往會準(zhǔn)備出一個業(yè)務(wù)接口來:
package cn.study.microboot.producer; public interface IMessageProducerService { public void sendMessage(String msg) ; }
五、隨后建立一個配置程序類,定義 ActiveMQ 的消息發(fā)送模版處理類:
package cn.study.microboot.config; import javax.jms.Queue; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.annotation.EnableJms; @Configuration @EnableJms public class ActiveMQConfig { @Bean public Queue queue() { return new ActiveMQQueue("study.msg.queue") ; } }
六、創(chuàng)建消息發(fā)送的子類實(shí)現(xiàn)消息發(fā)送處理:
package cn.study.microboot.producer.impl; import javax.annotation.Resource; import javax.jms.Queue; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; import cn.study.microboot.producer.IMessageProducerService; @Service public class MessageProducerServiceImpl implements IMessageProducerService { @Resource private JmsMessagingTemplate jmsMessagingTemplate; @Resource private Queue queue; @Override public void sendMessage(String msg) { this.jmsMessagingTemplate.convertAndSend(this.queue, msg); } }
七、編寫測試類來觀察消息的處理:
package cn.study.microboot.test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import cn.study.microboot.StartSpringBootMain; import cn.study.microboot.producer.IMessageProducerService; @SpringBootTest(classes = StartSpringBootMain.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestActiveMQ { @Resource private IMessageProducerService messageProducer; @Test public void testSend() throws Exception { for (int x = 0; x < 10; x++) { this.messageProducer.sendMessage("study - " + x); } } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。