Skip to content

Commit 13505f7

Browse files
author
jiang
committed
RBlockingQueue延迟队列
1 parent 14aceff commit 13505f7

File tree

10 files changed

+247
-6
lines changed

10 files changed

+247
-6
lines changed

delay-queue-redisson/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.dawn</groupId>
8+
<artifactId>delay-queue</artifactId>
9+
<version>0.0.1</version>
10+
</parent>
11+
12+
<artifactId>delay-queue-redisson</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- spring boot begin -->
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-redis</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.redisson</groupId>
34+
<artifactId>redisson-spring-boot-starter</artifactId>
35+
</dependency>
36+
37+
38+
39+
<dependency>
40+
<groupId>io.springfox</groupId>
41+
<artifactId>springfox-swagger2</artifactId>
42+
<version>2.10.5</version>
43+
<exclusions>
44+
<exclusion>
45+
<groupId>io.swagger</groupId>
46+
<artifactId>swagger-annotations</artifactId>
47+
</exclusion>
48+
<exclusion>
49+
<groupId>io.swagger</groupId>
50+
<artifactId>swagger-models</artifactId>
51+
</exclusion>
52+
</exclusions>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.springfox</groupId>
56+
<artifactId>springfox-swagger-ui</artifactId>
57+
<version>2.10.5</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.swagger</groupId>
61+
<artifactId>swagger-annotations</artifactId>
62+
<version>1.5.21</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>io.swagger</groupId>
66+
<artifactId>swagger-models</artifactId>
67+
<version>1.5.21</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.projectlombok</groupId>
72+
<artifactId>lombok</artifactId>
73+
<optional>true</optional>
74+
</dependency>
75+
76+
</dependencies>
77+
78+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.dawn.delayqueue.redisson;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
6+
7+
8+
@SpringBootApplication(exclude = {RedisAutoConfiguration.class,})
9+
public class DelayQueueRedissonApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(DelayQueueRedissonApplication.class, args);
13+
}
14+
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.dawn.delayqueue.redisson.config;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.redisson.api.RBlockingQueue;
5+
import org.redisson.api.RDelayedQueue;
6+
import org.redisson.api.RedissonClient;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Slf4j
11+
@Configuration
12+
public class RDelayedQueueConfig {
13+
14+
/**
15+
* 创建基本队列
16+
* @param redissonClient
17+
* @return
18+
*/
19+
@Bean
20+
public RBlockingQueue<String> queue(RedissonClient redissonClient) {
21+
log.info("queue注入成功!!");
22+
return redissonClient.getBlockingQueue("redisson_delay_Queue");
23+
}
24+
25+
/**
26+
* 延迟队列
27+
* @param redissonClient
28+
* @param queue
29+
* @return
30+
*/
31+
@Bean
32+
public RDelayedQueue<String> delayedQueue(RedissonClient redissonClient, RBlockingQueue<String> queue ) {
33+
log.info("delayedQueue注入成功!!");
34+
// 创建延迟队列并关联到基本队列
35+
return redissonClient.getDelayedQueue(queue);
36+
}
37+
38+
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.dawn.delayqueue.redisson.listener;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.redisson.api.RBlockingQueue;
5+
import org.redisson.api.RedissonClient;
6+
import org.springframework.stereotype.Component;
7+
8+
import javax.annotation.PostConstruct;
9+
import javax.annotation.Resource;
10+
import java.text.DateFormat;
11+
import java.util.Date;
12+
13+
@Slf4j
14+
@Component
15+
public class DelayQueueExec {
16+
17+
@Resource
18+
private RedissonClient redissonClient;
19+
20+
@Resource
21+
private RBlockingQueue<String> queue;
22+
23+
@PostConstruct
24+
public void init() {
25+
new Thread(() -> {
26+
27+
while (!redissonClient.isShutdown() && !redissonClient.isShuttingDown()) {
28+
String message = null;
29+
try {
30+
// 停止时,报错Redisson is shutdown,原因take还在阻塞获取,Redisson链接断开
31+
message = queue.take();
32+
log.info("now:{}, message: {}", DateFormat.getDateTimeInstance().format(new Date()), message);
33+
} catch (InterruptedException e) {
34+
log.error("error", e);
35+
}
36+
}
37+
}).start();
38+
}
39+
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.dawn.delayqueue.redisson.rest;
2+
3+
import io.swagger.annotations.ApiOperation;
4+
import io.swagger.annotations.ApiParam;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.redisson.api.RDelayedQueue;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import javax.annotation.Resource;
13+
import java.text.DateFormat;
14+
import java.util.Date;
15+
import java.util.concurrent.TimeUnit;
16+
17+
@Slf4j
18+
@RestController
19+
@RequestMapping("/rest/test")
20+
public class TestController {
21+
22+
@Resource
23+
private RDelayedQueue<String> delayedQueue;
24+
25+
@ApiOperation("添加延迟任务")
26+
@GetMapping(value = "/push")
27+
public String push(@ApiParam(name = "ttrTime", value = "延迟任务执行超时时间(单位:秒)", required = true, example = "5")
28+
@RequestParam("ttrTime") Long ttrTime,
29+
@ApiParam(name = "message", value = "消息内容", required = true)
30+
@RequestParam("message") String message) {
31+
log.info("now:{}, ttrTime:{}, message:{}", DateFormat.getDateTimeInstance().format(new Date()), ttrTime, message);
32+
delayedQueue.offer(message, ttrTime, TimeUnit.SECONDS);
33+
return "success";
34+
}
35+
36+
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
server:
2+
port: 16080
3+
4+
spring:
5+
redis:
6+
host: localhost
7+
port: 6379
8+
password: 123456
9+
timeout: 300000
10+
database: 1
11+
jedis:
12+
pool:
13+
max-idle: 10
14+
min-idle: 2
15+
max-wait: -1
16+
max-active: 8
17+
time-between-eviction-runs: 100000
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
log4j.rootLogger=DEBUG, Console, RollingFile
2+
3+
#Console
4+
log4j.appender.Console=org.apache.log4j.ConsoleAppender
5+
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
6+
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%c{5}] - %m%n
7+
8+
#RollingFile
9+
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
10+
log4j.appender.RollingFile.File=/logs/delay-queue.log
11+
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
12+
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
13+
14+
log4j.logger.org.springframework=INFO
15+
log4j.logger.org.hibernate=INFO
16+
log4j.logger.org.redisson.cluster.ClusterConnectionManager=INFO

delay-queue-sample-springboot/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<dependency>
3838
<groupId>io.springfox</groupId>
3939
<artifactId>springfox-swagger2</artifactId>
40-
<version>2.9.2</version>
40+
<version>2.10.5</version>
4141
<exclusions>
4242
<exclusion>
4343
<groupId>io.swagger</groupId>
@@ -52,7 +52,7 @@
5252
<dependency>
5353
<groupId>io.springfox</groupId>
5454
<artifactId>springfox-swagger-ui</artifactId>
55-
<version>2.9.2</version>
55+
<version>2.10.5</version>
5656
</dependency>
5757
<dependency>
5858
<groupId>io.swagger</groupId>

delay-queue-sample-springboot/src/main/java/com/dawn/delayqueue/Swagger2Config.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import springfox.documentation.service.Contact;
99
import springfox.documentation.spi.DocumentationType;
1010
import springfox.documentation.spring.web.plugins.Docket;
11-
import springfox.documentation.swagger2.annotations.EnableSwagger2;
1211

1312
/**
1413
* Create By IntelliJ IDEA
@@ -17,7 +16,6 @@
1716
* @date 2018/1/4 15:04
1817
*/
1918
@Configuration
20-
@EnableSwagger2
2119
public class Swagger2Config {
2220

2321
@Bean

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
<modules>
88
<module>delay-queue-core</module>
99
<module>delay-queue-sample-springboot</module>
10+
<module>delay-queue-redisson</module>
1011
</modules>
1112

1213
<parent>
1314
<groupId>org.springframework.boot</groupId>
1415
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>2.2.13.RELEASE</version>
16+
<version>2.7.17</version>
1617
<relativePath/> <!-- lookup parent from repository -->
1718
</parent>
1819

@@ -35,7 +36,7 @@
3536
<dependency>
3637
<groupId>org.redisson</groupId>
3738
<artifactId>redisson-spring-boot-starter</artifactId>
38-
<version>3.17.4</version>
39+
<version>3.24.1</version>
3940
</dependency>
4041
</dependencies>
4142
</dependencyManagement>

0 commit comments

Comments
 (0)