主題 activemq spring boot 小程序開發(fā)
1.引入依賴
1
2
3
4
5
6
7
8
9
10
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 3 .release</version> <relativepath /> <!-- lookup parent from repository --> </parent> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-activemq</artifactid> </dependency> |
2.修改配置文件(其實配置也是默認值,不配置也可以)
1
2
|
spring.activemq.in-memory= true spring.activemq.pool.enabled= false |
3.添加activemq連接池(如果不開啟連接池,則每發(fā)送一條數(shù)據(jù)創(chuàng)建一個連接)
①.添加依賴
1
2
3
4
|
<dependency> <groupid>org.apache.activemq</groupid> <artifactid>activemq-pool</artifactid> </dependency> |
②.修改配置文件
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
33
34
35
36
37
38
39
40
41
42
43
|
#服務端口, 8080 被另一服務占用 server.port= 9090 spring.activemq.broker-url=tcp: //127.0.0.1:61616 # 在考慮結束之前等待的時間 #spring.activemq.close-timeout=15s # 默認代理url是否應該在內(nèi)存中。如果指定了顯式代理,則忽略此值。 spring.activemq.in-memory= true # 是否在回滾回滾消息之前停止消息傳遞。這意味著當啟用此命令時,消息順序不會被保留。 spring.activemq.non-blocking-redelivery= false # 等待消息發(fā)送響應的時間。設置為 0 等待永遠。 spring.activemq.send-timeout= 0 #默認情況下activemq提供的是queue模式,若要使用topic模式需要配置下面配置 spring.jms.pub-sub-domain= true #賬號 spring.activemq.user=admin # 密碼 spring.activemq.password=admin # 是否信任所有包 #spring.activemq.packages.trust-all= # 要信任的特定包的逗號分隔列表(當不信任所有包時) #spring.activemq.packages.trusted= # 當連接請求和池滿時是否阻塞。設置 false 會拋“jmsexception異常”。 #spring.activemq.pool.block- if -full= true # 如果池仍然滿,則在拋出異常前阻塞時間。 #spring.activemq.pool.block- if -full-timeout=-1ms # 是否在啟動時創(chuàng)建連接。可以在啟動時用于加熱池。 #spring.activemq.pool.create-connection-on-startup= true # 是否用pooledconnectionfactory代替普通的connectionfactory。 #spring.activemq.pool.enabled= false # 連接過期超時。 #spring.activemq.pool.expiry-timeout=0ms # 連接空閑超時 #spring.activemq.pool.idle-timeout=30s # 連接池最大連接數(shù) #spring.activemq.pool.max-connections= 1 # 每個連接的有效會話的最大數(shù)目。 #spring.activemq.pool.maximum-active-session-per-connection= 500 # 當有 "jmsexception" 時嘗試重新連接 #spring.activemq.pool.reconnect-on-exception= true # 在空閑連接清除線程之間運行的時間。當為負數(shù)時,沒有空閑連接驅逐線程運行。 #spring.activemq.pool.time-between-expiration-check=-1ms # 是否只使用一個messageproducer #spring.activemq.pool.use-anonymous-producers= true |
4.添加jms相關配置
①.開啟jms掃描注解:@enablejms 相當于application.xml中的<jms:annotation-d riven/>
②.配置queue類:
1
2
3
4
|
@bean public queue queue() { return new activemqqueue( "queuename1" ); } |
③.創(chuàng)建生產(chǎn)者:
1
2
3
4
5
|
@resource jmsmessagingtemplate jmsmessagingtemplate; public void sendmessage(destination destination, string message) { jmsmessagingtemplate.convertandsend(destination, message); } |
④.創(chuàng)建消費者:
1
2
3
4
5
|
@jmslistener (destination = "queuename1" ) public void receivequeue(string message) { log.info( "=========接受到了消息:" + message); grabservice.addsearchcontent(message, mainconfig.getcharset()); } |
ps:@jmslistener(destination = "queuename1")注解用于監(jiān)聽指定名稱的消息
參數(shù)message代表具體的消息
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/zhangsansan/p/10597569.html