springboot集成activemq的實(shí)例代碼
ActiveMQ
ActiveMQ 是Apache出品,最流行的,能力強(qiáng)勁的開源消息總線。ActiveMQ 是一個(gè)完全支持JMS1.1和J2EE 1.4規(guī)范的 JMS Provider實(shí)現(xiàn),盡管JMS規(guī)范出臺已經(jīng)是很久的事情了,但是JMS在當(dāng)今的J2EE應(yīng)用中間仍然扮演著特殊的地位。
特性
- 多種語言和協(xié)議編寫客戶端。語言: Java,C,C++,C#,Ruby,Perl,Python,PHP。應(yīng)用協(xié)議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
- 完全支持JMS1.1和J2EE 1.4規(guī)范 (持久化,XA消息,事務(wù))
- 對Spring的支持,ActiveMQ可以很容易內(nèi)嵌到使用Spring的系統(tǒng)里面去,而且也支持Spring2.0的特性
- 通過了常見J2EE服務(wù)器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何兼容J2EE 1.4 商業(yè)服務(wù)器上
- 支持多種傳送協(xié)議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
- 支持通過JDBC和journal提供高速的消息持久化
- 從設(shè)計(jì)上保證了高性能的集群,客戶端-服務(wù)器,點(diǎn)對點(diǎn)
- 支持Ajax
- 支持與Axis的整合
- 可以很容易的調(diào)用內(nèi)嵌JMS provider,進(jìn)行測試
更多關(guān)于 ActiveMQ 的內(nèi)容可以點(diǎn)擊這里。
Spring-Boot 集成 ActiveMQ
添加maven依賴
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> </dependency>
沒有直接使用注釋的依賴,是因?yàn)槠浜腥缦乱蕾?/p>
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency>
而它的作用是什么呢,會在程序中直接內(nèi)嵌 ActivityMQ,也就是說不需要安裝 ActiveMQ,但是這個(gè)如果服務(wù)宕機(jī)了,內(nèi)嵌的 ActiveMQ 也就沒了。關(guān)鍵,這個(gè)內(nèi)嵌的 ActiveMQ 而無法看到圖形化界面,所以這里沒有直接使用注釋里的依賴。
在application.properties中增加如下配置
# activemq spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.in-memory=true spring.activemq.pool.enabled=false
這里對 ActiveMQ 的端口進(jìn)行一個(gè)簡短說明,61616為消息接口 ,8161 為管理界面
JAVA代碼實(shí)現(xiàn)
定義QUEUE
package com.activemq.queue; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.jms.Queue; @Configuration public class QueueConfig { @Bean public Queue logQueue() { return new ActiveMQQueue(QueueName.LOG_QUEUE); } }
消息生產(chǎn)者
package com.activemq.producer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component; import javax.jms.Queue; @Component public class LogProducer implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(LogProducer.class); @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue logQueue; @Override public void run(String... strings) throws Exception { send("This is a log message."); LOGGER.info("Log Message was sent to the Queue named sample.log"); } public void send(String msg) { this.jmsMessagingTemplate.convertAndSend(this.logQueue, msg); } }
消息消費(fèi)者
package com.activemq.consumer; import com.activemq.queue.QueueName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class LogConsumer { private static final Logger LOGGER = LoggerFactory.getLogger(LogConsumer.class); @JmsListener(destination = QueueName.LOG_QUEUE) public void receivedQueue(String msg) { LOGGER.info("Has received from " + QueueName.LOG_QUEUE + ", msg: " + msg); } }
測試接口
@Autowired private LogProducer logProducer; @GetMapping("/activemq/send") public String activemq(HttpServletRequest request, String msg) { msg = StringUtils.isEmpty(msg) ? "This is Empty Msg." : msg; try { logProducer.send(msg); } catch (Exception e) { e.printStackTrace(); } return "Activemq has sent OK."; }
啟動類
增加注解@EnableJms
@Configuration//配置控制 @EnableAutoConfiguration//啟用自動配置 @ComponentScan//組件掃描 @EnableConfigurationProperties({EmailProp.class}) @EnableJms public class Bootstrap { private static final Logger LOGGER = LoggerFactory .getLogger(Bootstrap.class); public static void main(String[] args) throws Exception { SpringApplication.run(Bootstrap.class, args); LOGGER.info("Server running..."); } }
測試
運(yùn)行服務(wù),在瀏覽器輸入 http://127.0.0.1:8080/activemq/send?msg=test%20log,會在控制臺看到如下輸出
INFO 1498 --- [enerContainer-1] c.j.a.activemq.consumer.LogConsumer : Has received from sample.log, msg: test log [DefaultMessageListenerContainer-1] INFO c.j.a.activemq.consumer.LogConsumer - Has received from sample.log, msg: test log
打開 ActiveMQ 的管理頁面,用戶名密碼都是admin,可以看到如下信息
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python文件高級操作函數(shù)之文件信息獲取與目錄操作
這篇文章主要介紹了Python文件高級操作函數(shù)之文件信息獲取與目錄操作,在Python中,內(nèi)置了文件(File)對象。在使用文件對象時(shí),首先需要通過內(nèi)置的open()方法創(chuàng)建一個(gè)文件對象,然后通過該對象提供的方法進(jìn)行一些基本文件操作,需要的朋友可以參考下2023-05-05Java消息隊(duì)列JMS實(shí)現(xiàn)原理解析
這篇文章主要介紹了Java消息隊(duì)列JMS實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03記錄一次connection reset 錯(cuò)誤的解決全過程
這篇文章主要介紹了記錄一次connection reset 錯(cuò)誤的解決全過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Spring框架中ImportBeanDefinitionRegistrar的應(yīng)用詳解
這篇文章主要介紹了Spring框架中ImportBeanDefinitionRegistrar的應(yīng)用詳解,如果實(shí)現(xiàn)了ImportSelector接口,在配置類中被@Import加入到Spring容器中以后,Spring容器就會把ImportSelector接口方法返回的字符串?dāng)?shù)組中的類new出來對象然后放到工廠中去,需要的朋友可以參考下2024-01-01SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09