springboot集成activemq的實例代碼
ActiveMQ
ActiveMQ 是Apache出品,最流行的,能力強(qiáng)勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規(guī)范的 JMS Provider實現(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è)計上保證了高性能的集群,客戶端-服務(wù)器,點對點
- 支持Ajax
- 支持與Axis的整合
- 可以很容易的調(diào)用內(nèi)嵌JMS provider,進(jìn)行測試
更多關(guān)于 ActiveMQ 的內(nèi)容可以點擊這里。
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> 沒有直接使用注釋的依賴,是因為其含有如下依賴
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency>
而它的作用是什么呢,會在程序中直接內(nèi)嵌 ActivityMQ,也就是說不需要安裝 ActiveMQ,但是這個如果服務(wù)宕機(jī)了,內(nèi)嵌的 ActiveMQ 也就沒了。關(guān)鍵,這個內(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)行一個簡短說明,61616為消息接口 ,8161 為管理界面
JAVA代碼實現(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);
}
}
消息消費者
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...");
}
}
測試
運行服務(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)文章
Java并發(fā)編程之ReadWriteLock讀寫鎖的操作方法
這篇文章主要介紹了Java并發(fā)編程之ReadWriteLock讀寫鎖的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
spring?cloud?配置阿里數(shù)據(jù)庫連接池?druid的示例代碼
這篇文章主要介紹了spring?cloud?配置阿里數(shù)據(jù)庫連接池?druid,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
Map映射LinkedHashSet與LinkedHashMap應(yīng)用解析
這篇文章主要為大家介紹了Map映射LinkedHashSet與LinkedHashMap的應(yīng)用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步2022-03-03
Java Swing最詳細(xì)基礎(chǔ)知識總結(jié)
這篇文章主要介紹了Java Swing最詳細(xì)基礎(chǔ)知識總結(jié),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)Java Swing的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05
intellij idea中spring boot properties文件不能自動提示問題解決
這篇文章主要介紹了intellij idea中spring boot properties文件不能自動提示問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Java MultipartFile實現(xiàn)上傳文件/上傳圖片
這篇文章主要介紹了Java MultipartFile實現(xiàn)上傳文件/上傳圖片,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12
MyBatis的注解使用、ORM層優(yōu)化方式(懶加載和緩存)
這篇文章主要介紹了MyBatis的注解使用、ORM層優(yōu)化方式(懶加載和緩存),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

