-
Notifications
You must be signed in to change notification settings - Fork 4
/
Example.java
140 lines (125 loc) · 4.57 KB
/
Example.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.meitu.platform.lmstfy;
import com.meitu.platform.lmstfy.client.LmstfyClient;
import com.meitu.platform.lmstfy.exception.LmstfyException;
import com.meitu.platform.lmstfy.exception.LmstfyNotJobException;
import com.meitu.platform.lmstfy.response.DeadLetterResponse;
/**
* Description:
*
* @author Yesphet
* @date 2019-12-06
*/
public class Example {
private static String token = "01EXKH08T648VHWD64BND16CXR";
private static String host = "localhost";
private static int port = 7777;
private static String namespace = "sdk-test";
private static String queue = "sdk-test-queue";
private static LmstfyClient client = new LmstfyClient(host, port, namespace, token);
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
publish();
}
consume();
deleteAndAck();
batchConsume();
queueSize();
peekQueue();
peekJob();
peekDeadLetter();
respawnDeadLetter();
}
private static void publish() {
try {
String jobID = client.publish(queue, "test data".getBytes(), 5, (short) 2, 0);
System.out.println(jobID);
} catch (LmstfyException e) {
e.printStackTrace();
}
}
private static void consume() {
try {
Job job = client.consume(3, 2, queue);
System.out.println(job.getData());
} catch (LmstfyNotJobException e) {
System.out.println("There has no available job");
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
private static void batchConsume() {
try {
Job[] jobs = client.batchConsume(3, 3, 2, queue);
System.out.println(jobs.length);
} catch (LmstfyNotJobException e) {
System.out.println("There has no available job");
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
private static void deleteAndAck() {
try {
String jobID1 = client.publish(queue, "test data".getBytes(), 5, (short) 2, 0);
client.delete(queue, jobID1);
String jobID2 = client.publish(queue, "test data".getBytes(), 5, (short) 2, 0);
client.ack(queue, jobID2);
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
private static void queueSize() {
try {
int size1 = client.queueSize(queue);
client.publish(queue, "test data".getBytes(), 5, (short) 2, 0);
int size2 = client.queueSize(queue);
System.out.printf("%d %d\n", size1, size2);
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
private static void peekQueue() {
try {
Job job = client.peekQueue(queue);
if (job == null) {
System.out.println("There has no available job to peek");
return;
}
if (job.getData() == null) {
System.out.printf("The job %s is expired\n", job.getJobID());
} else {
System.out.println(job.getData());
}
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
private static void peekJob() {
try {
String jobID = client.publish(queue, "test data".getBytes(), 5, (short) 2, 0);
Job job = client.peekJob(queue, jobID);
if (job == null) {
System.out.println("There has no available job");
} else {
System.out.printf("%s\n", job.getData());
}
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
private static void peekDeadLetter() {
try {
DeadLetterResponse deadLetterResponse = client.peekDeadLetter(queue);
System.out.println(deadLetterResponse.getDeadLetterSize());
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
private static void respawnDeadLetter() {
try {
int count = client.respawnDeadLetter(queue, 1, 60);
System.out.println("Respawn deadletter count: " + count);
} catch (LmstfyException e) {
System.out.println("Request Lmstfy error, " + e.getMessage());
}
}
}