ActiveMQ
ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規范的 JMS Provider實現,盡管JMS規范出臺已經是很久的事情了,但是JMS在當今的J2EE應用中間仍然扮演著特殊的地位。
特性
- 多種語言和協議編寫客戶端。語言: Java,C,C++,C#,Ruby,Perl,Python,PHP。應用協議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
- 完全支持JMS1.1和J2EE 1.4規范 (持久化,XA消息,事務)
- 對Spring的支持,ActiveMQ可以很容易內嵌到使用Spring的系統里面去,而且也支持Spring2.0的特性
- 通過了常見J2EE服務器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何兼容J2EE 1.4 商業服務器上
- 支持多種傳送協議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
- 支持通過JDBC和journal提供高速的消息持久化
- 從設計上保證了高性能的集群,客戶端-服務器,點對點
- 支持Ajax
- 支持與Axis的整合
- 可以很容易的調用內嵌JMS provider,進行測試
更多關于 ActiveMQ 的內容可以點擊這里。
Spring-Boot 集成 ActiveMQ
添加maven依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!-- <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 > |
沒有直接使用注釋的依賴,是因為其含有如下依賴
1
2
3
4
|
< dependency > < groupId >org.apache.activemq</ groupId > < artifactId >activemq-broker</ artifactId > </ dependency > |
而它的作用是什么呢,會在程序中直接內嵌 ActivityMQ,也就是說不需要安裝 ActiveMQ,但是這個如果服務宕機了,內嵌的 ActiveMQ 也就沒了。關鍵,這個內嵌的 ActiveMQ 而無法看到圖形化界面,所以這里沒有直接使用注釋里的依賴。
在application.properties中增加如下配置
1
2
3
4
5
6
|
# 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 的端口做一個簡短說明,61616為消息代理接口 ,8161 為管理界面
JAVA代碼實現
定義QUEUE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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); } } |
消息生產者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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); } } |
消息消費者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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); } } |
測試接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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..." ); } } |
測試
運行服務,在瀏覽器輸入 http://127.0.0.1:8080/activemq/send?msg=test%20log,會在控制臺看到如下輸出
1
2
|
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,可以看到如下信息
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/zl18310999566/article/details/54313464