Skip to content

Commit ba646c3

Browse files
committed
Create distributed queue
1 parent 01edd76 commit ba646c3

File tree

7 files changed

+53
-0
lines changed

7 files changed

+53
-0
lines changed

consumer/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'org.springframework.boot' version '2.2.7.RELEASE'
33
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
4+
id "io.freefair.lombok" version "5.0.1"
45
id 'java'
56
}
67

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package com.example.consumer;
22

3+
import org.redisson.api.RScoredSortedSet;
4+
import org.redisson.api.RedissonClient;
5+
import org.springframework.beans.factory.annotation.Autowired;
36
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PostMapping;
49
import org.springframework.web.bind.annotation.RestController;
510

611
@RestController
712
public class ConsumerController {
13+
@Autowired
14+
RedissonClient redisson;
15+
816
@GetMapping("/hello")
917
public String hello() {
1018
return "Hello Consumer";
1119
}
20+
21+
@PostMapping("/pollJob/{queueId}")
22+
public String pollJob(@PathVariable String queueId) {
23+
RScoredSortedSet<String> set = redisson.getScoredSortedSet(queueId);
24+
25+
return set.pollLast();
26+
}
1227
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.consumer;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Job {
7+
private String queueId;
8+
private double score;
9+
private String name;
10+
}

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ services:
1616

1717
redis:
1818
image: "redis:alpine"
19+
ports:
20+
- 9379:6379

producer/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'org.springframework.boot' version '2.2.7.RELEASE'
33
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
4+
id "io.freefair.lombok" version "5.0.1"
45
id 'java'
56
}
67

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.producer;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Job {
7+
private String queueId;
8+
private double score;
9+
private String name;
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package com.example.producer;
22

3+
import org.redisson.api.RScoredSortedSet;
4+
import org.redisson.api.RedissonClient;
5+
import org.springframework.beans.factory.annotation.Autowired;
36
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
49
import org.springframework.web.bind.annotation.RestController;
510

611
@RestController
712
public class ProducerController {
13+
@Autowired
14+
RedissonClient redisson;
15+
816
@GetMapping("/hello")
917
public String hello() {
1018
return "Hello Producer";
1119
}
20+
21+
@PostMapping("/queueJob")
22+
public void queueJob(@RequestBody Job job) {
23+
RScoredSortedSet<String> set = redisson.getScoredSortedSet(job.getQueueId());
24+
set.add(job.getScore(), job.getName());
25+
}
1226
}

0 commit comments

Comments
 (0)