我們在使用一些開源調度系統(比如:elastic-job等)的時候,對于任務的執行時間通常都是有規律性的,可能是每隔半小時執行一次,或者每天凌晨一點執行一次。然而實際業務中還存在另外一種定時任務,它可能需要一些觸發條件才開始定時,比如:編寫博文時候,設置2小時之后發送。對于這些開始時間不確定的定時任務,我們也可以通過spring cloud stream來很好的處理。
為了實現開始時間不確定的定時任務觸發,我們將引入延遲消息的使用。rabbitmq中提供了關于延遲消息的插件,所以本文就來具體介紹以下如何利用spring cloud stream以及rabbitmq輕松的處理上述問題。
動手試試
插件安裝
關于RabbitMQ延遲消息的插件介紹可以查看官方網站: https://www.rabbitmq.com/blog/2015/04/16/scheduling-messages-with-rabbitmq/
安裝方式很簡單,只需要在這個頁面: http://www.rabbitmq.com/community-plugins.html 中找到 rabbitmq_delayed_message_exchange
插件,根據您使用的rabbitmq版本選擇對應的插件版本下載即可。
注意:只有rabbitmq 3.6.x以上才支持
在下載好之后,解壓得到 .ez
結尾的插件包,將其復制到rabbitmq安裝目錄下的 plugins
文件夾。
然后通過命令行啟用該插件:
1
|
rabbitmq-plugins enable rabbitmq_delayed_message_exchange |
該插件在通過上述命令啟用后就可以直接使用,不需要重啟。
另外,如果您沒有啟用該插件,您可能為遇到類似這樣的錯誤:
error 156 --- [ 127.0.0.1:5672] o.s.a.r.c.cachingconnectionfactory : channel shutdown: connection error; protocol method: #method(reply-code=503, reply-text=command_invalid - unknown exchange type 'x-delayed-message', class-id=40, method-id=1
應用編碼
下面通過編寫一個簡單的例子來具體體會一下這個屬性的用法:
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
@enablebinding (testapplication.testtopic. class ) @springbootapplication public class testapplication { public static void main(string[] args) { springapplication.run(testapplication. class , args); } @slf4j @restcontroller static class testcontroller { @autowired private testtopic testtopic; /** * 消息生產接口 * * @param message * @return */ @getmapping ( "/sendmessage" ) public string messagewithmq( @requestparam string message) { log.info( "send: " + message); testtopic.output().send(messagebuilder.withpayload(message).setheader( "x-delay" , 5000 ).build()); return "ok" ; } } /** * 消息消費邏輯 */ @slf4j @component static class testlistener { @streamlistener (testtopic.input) public void receive(string payload) { log.info( "received: " + payload); } } interface testtopic { string output = "example-topic-output" ; string input = "example-topic-input" ; @output (output) messagechannel output(); @input (input) subscribablechannel input(); } } |
內容很簡單,既包含了消息的生產,也包含了消息消費。在 /sendmessage
接口的定義中,發送了一條消息,一條消息的頭信息中包含了 x-delay
字段,該字段用來指定消息延遲的時間,單位為毫秒。所以上述代碼發送的消息會在5秒之后被消費。在消息監聽類 testlistener
中,對 testtopic.input
通道定義了 @streamlistener
,這里會對延遲消息做具體的邏輯。由于消息的消費是延遲的,從而變相實現了從消息發送那一刻起開始的定時任務。
在啟動應用之前,還要需要做一些必要的配置,下面分消息生產端和消費端做說明:
消息生產端
1
2
|
spring.cloud.stream.bindings.example-topic-output.destination=delay-topic spring.cloud.stream.rabbit.bindings.example-topic-output.producer.delayed-exchange= true |
注意這里的一個新參數 spring.cloud.stream.rabbit.bindings.example-topic-output.producer.delayed-exchange
,用來開啟延遲消息的功能,這樣在創建exchange的時候,會將其設置為具有延遲特性的exchange,也就是用到上面我們安裝的延遲消息插件的功能。
消息消費端
1
2
3
|
spring.cloud.stream.bindings.example-topic-input.destination=delay-topic spring.cloud.stream.bindings.example-topic-input.group=test spring.cloud.stream.rabbit.bindings.example-topic-input.consumer.delayed-exchange= true |
在消費端也一樣,需要設置 spring.cloud.stream.rabbit.bindings.example-topic-output.producer.delayed-exchange=true
。如果該參數不設置,將會出現類似下面的錯誤:
error 9340 --- [ 127.0.0.1:5672] o.s.a.r.c.cachingconnectionfactory : channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=precondition_failed - inequivalent arg 'type' for exchange 'delay-topic' in vhost '/': received 'topic' but current is ''x-delayed-message'', class-id=40, method-id=10)
完成了上面配置之后,就可以啟動應用,并嘗試訪問 localhost:8080/sendmessage?message=hello
接口來發送一個消息到mq中了。此時可以看到類似下面的日志:
1
2
3
4
|
2019 - 01 - 02 23 : 28 : 45.318 info 96164 --- [ctor-http-nio- 3 ] c.d.s.testapplication$testcontroller : send: hello 2019 - 01 - 02 23 : 28 : 45.328 info 96164 --- [ctor-http-nio- 3 ] o.s.a.r.c.cachingconnectionfactory : attempting to connect to: [localhost: 5672 ] 2019 - 01 - 02 23 : 28 : 45.333 info 96164 --- [ctor-http-nio- 3 ] o.s.a.r.c.cachingconnectionfactory : created new connection: rabbitconnectionfactory.publisher#5c5f9a03: 0 /simpleconnection @3278a728 [delegate=amqp: //[email protected]:5672/, localport= 53536] 2019 - 01 - 02 23 : 28 : 50.349 info 96164 --- [ay-topic.test- 1 ] c.d.stream.testapplication$testlistener : received: hello |
從日志中可以看到, send: hello
和 received: hello
兩條輸出之間間隔了5秒,符合我們上面編碼設置的延遲時間。
深入思考
在代碼層面已經完成了定時任務,那么我們如何查看延遲的消息數等信息呢?
此時,我們可以打開rabbitmq的web控制臺,首先可以進入exchanges頁面,看看這個特殊exchange,具體如下:
可以看到,這個exchange的type類型是 x-delayed-message
。點擊該exchange的名稱,進入詳細頁面,就可以看到更多具體信息了:
代碼示例
本文示例讀者可以通過查看下面倉庫的中的 stream-delayed-message
項目:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.didispace.com/spring-cloud-starter-finchley-7-7/