Skip to content

Commit 08cbdb1

Browse files
committed
使用延迟消息实现定时任务(RabbitMQ)
1 parent 5f7e5de commit 08cbdb1

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

4-Finchley/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<!-- @StreamListener根据内容路由 -->
2424
<module>stream-content-route</module>
2525

26+
<!-- 使用延迟消息实现定时任务(RabbitMQ)-->
27+
<module>stream-delayed-message</module>
28+
2629
</modules>
2730

2831
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>stream-delayed-message</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.5.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.projectlombok</groupId>
27+
<artifactId>lombok</artifactId>
28+
<version>1.18.2</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-actuator</artifactId>
43+
</dependency>
44+
</dependencies>
45+
46+
<dependencyManagement>
47+
<dependencies>
48+
<dependency>
49+
<groupId>org.springframework.cloud</groupId>
50+
<artifactId>spring-cloud-dependencies</artifactId>
51+
<version>Finchley.SR1</version>
52+
<type>pom</type>
53+
<scope>import</scope>
54+
</dependency>
55+
</dependencies>
56+
</dependencyManagement>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.didispace.stream;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.cloud.stream.annotation.EnableBinding;
8+
import org.springframework.cloud.stream.annotation.Input;
9+
import org.springframework.cloud.stream.annotation.Output;
10+
import org.springframework.cloud.stream.annotation.StreamListener;
11+
import org.springframework.integration.support.MessageBuilder;
12+
import org.springframework.messaging.MessageChannel;
13+
import org.springframework.messaging.SubscribableChannel;
14+
import org.springframework.stereotype.Component;
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.RequestParam;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
20+
@EnableBinding(TestApplication.TestTopic.class)
21+
@SpringBootApplication
22+
public class TestApplication {
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(TestApplication.class, args);
26+
}
27+
28+
@Slf4j
29+
@RestController
30+
static class TestController {
31+
32+
@Autowired
33+
private TestTopic testTopic;
34+
35+
/**
36+
* 消息生产接口
37+
*
38+
* @param message
39+
* @return
40+
*/
41+
@GetMapping("/sendMessage")
42+
public String messageWithMQ(@RequestParam String message) {
43+
log.info("Send: " + message);
44+
testTopic.output().send(MessageBuilder.withPayload(message).setHeader("x-delay", 5000).build());
45+
return "ok";
46+
}
47+
48+
}
49+
50+
/**
51+
* 消息消费逻辑
52+
*/
53+
@Slf4j
54+
@Component
55+
static class TestListener {
56+
57+
@StreamListener(TestTopic.INPUT)
58+
public void receive(String payload) {
59+
log.info("Received: " + payload);
60+
}
61+
62+
}
63+
64+
interface TestTopic {
65+
66+
String OUTPUT = "example-topic-output";
67+
String INPUT = "example-topic-input";
68+
69+
@Output(OUTPUT)
70+
MessageChannel output();
71+
72+
@Input(INPUT)
73+
SubscribableChannel input();
74+
75+
}
76+
77+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.application.name=stream-delayed-message
2+
server.port=8080
3+
4+
spring.cloud.stream.bindings.example-topic-input.destination=delay-topic
5+
spring.cloud.stream.bindings.example-topic-input.group=test
6+
spring.cloud.stream.rabbit.bindings.example-topic-input.consumer.delayed-exchange=true
7+
8+
spring.cloud.stream.bindings.example-topic-output.destination=delay-topic
9+
spring.cloud.stream.rabbit.bindings.example-topic-output.producer.delayed-exchange=true

0 commit comments

Comments
 (0)