新聞中心
這篇文章將為大家詳細講解有關Spring Cloud Stream如何使用延遲消息實現(xiàn)定時任務,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價值觀,專業(yè)網(wǎng)站建設服務10余年為成都紙箱小微創(chuàng)業(yè)公司專業(yè)提供企業(yè)網(wǎng)站建設營銷網(wǎng)站建設商城網(wǎng)站建設手機網(wǎng)站建設小程序網(wǎng)站建設網(wǎng)站改版,從內(nèi)容策劃、視覺設計、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設服務。
動手試試
插件安裝
安裝方式很簡單,只需要在這個頁面: http://www.rabbitmq.com/community-plugins.html 中找到 rabbitmq_delayed_message_exchange 插件,根據(jù)您使用的RabbitMQ版本選擇對應的插件版本下載即可。
注意:只有RabbitMQ 3.6.x以上才支持
在下載好之后,解壓得到 .ez 結(jié)尾的插件包,將其復制到RabbitMQ安裝目錄下的 plugins 文件夾。
然后通過命令行啟用該插件:
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
應用編碼
下面通過編寫一個簡單的例子來具體體會一下這個屬性的用法:
@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;
/**
* 消息生產(chǎn)接口
*
* @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();
}
}內(nèi)容很簡單,既包含了消息的生產(chǎn),也包含了消息消費。在 /sendMessage 接口的定義中,發(fā)送了一條消息,一條消息的頭信息中包含了 x-delay 字段,該字段用來指定消息延遲的時間,單位為毫秒。所以上述代碼發(fā)送的消息會在5秒之后被消費。在消息監(jiān)聽類 TestListener 中,對 TestTopic.INPUT 通道定義了 @StreamListener ,這里會對延遲消息做具體的邏輯。由于消息的消費是延遲的,從而變相實現(xiàn)了從消息發(fā)送那一刻起開始的定時任務。
在啟動應用之前,還要需要做一些必要的配置,下面分消息生產(chǎn)端和消費端做說明:
消息生產(chǎn)端
spring.cloud.stream.bindings.example-topic-output.destination=delay-topic spring.cloud.stream.rabbit.bindings.example-topic-output.producer.delayed-exchange=true
注意這里的一個新參數(shù) spring.cloud.stream.rabbit.bindings.example-topic-output.producer.delayed-exchange ,用來開啟延遲消息的功能,這樣在創(chuàng)建exchange的時候,會將其設置為具有延遲特性的exchange,也就是用到上面我們安裝的延遲消息插件的功能。
消息消費端
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 。如果該參數(shù)不設置,將會出現(xiàn)類似下面的錯誤:
ERROR 9340 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error; protocol method: #method
(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 接口來發(fā)送一個消息到MQ中了。此時可以看到類似下面的日志:
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://guest@127.0.0.1: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秒,符合我們上面編碼設置的延遲時間。
深入思考
在代碼層面已經(jīng)完成了定時任務,那么我們?nèi)绾尾榭囱舆t的消息數(shù)等信息呢?
此時,我們可以打開RabbitMQ的Web控制臺,首先可以進入Exchanges頁面,看看這個特殊exchange,具體如下:

可以看到,這個exchange的Type類型是 x-delayed-message 。點擊該exchange的名稱,進入詳細頁面,就可以看到更多具體信息了:

關于“Spring Cloud Stream如何使用延遲消息實現(xiàn)定時任務”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章標題:SpringCloudStream如何使用延遲消息實現(xiàn)定時任務
網(wǎng)站路徑:http://biofuelwatch.net/article/pegeoj.html


咨詢
建站咨詢
