2
2
3
3
import com .meitu .platform .lmstfy .exception .LmstfyException ;
4
4
import com .meitu .platform .lmstfy .exception .LmstfyIllegalRequestException ;
5
+ import com .meitu .platform .lmstfy .exception .LmstfyNotJobException ;
5
6
import com .meitu .platform .lmstfy .exception .LmstfyUnexpectedException ;
6
- import com .meitu .platform .lmstfy .response .ErrorResponse ;
7
+ import com .meitu .platform .lmstfy .response .* ;
7
8
import com .meitu .platform .lmstfy .Job ;
8
- import com .meitu .platform .lmstfy .response .LmstfyResponse ;
9
- import com .meitu .platform .lmstfy .response .PublishResponse ;
10
- import com .meitu .platform .lmstfy .response .QueueSizeResponse ;
11
9
import okhttp3 .*;
12
10
13
11
import java .io .IOException ;
@@ -31,6 +29,7 @@ public class LmstfyClient {
31
29
private static final String QUERY_TRIES = "tries" ;
32
30
private static final String QUERY_TIMEOUT = "timeout" ;
33
31
private static final String QUERY_TTR = "ttr" ;
32
+ private static final String QUERY_LIMIT = "limit" ;
34
33
35
34
private String namespace ;
36
35
private String token ;
@@ -39,7 +38,9 @@ public class LmstfyClient {
39
38
private HttpUrl serviceAddress ;
40
39
41
40
42
- public LmstfyClient (String host , int port , String namespace , String token ) {
41
+ public LmstfyClient (String host , int port , String namespace , String token ,
42
+ int readTimeoutSecond , int writeTimeoutSecond , int connectTimeoutSecond ,
43
+ int retryTimes , int retryIntervalMilliseconds ) {
43
44
this .namespace = namespace ;
44
45
this .token = token ;
45
46
this .serviceAddress = new HttpUrl .Builder ()
@@ -50,14 +51,18 @@ public LmstfyClient(String host, int port, String namespace, String token) {
50
51
51
52
this .http = new OkHttpClient .Builder ()
52
53
.retryOnConnectionFailure (true )
53
- .addInterceptor (new OkHttpRetryInterceptor (3 , 100 ))
54
- .readTimeout (600 , TimeUnit .SECONDS )
55
- .writeTimeout (600 , TimeUnit .SECONDS )
56
- .connectTimeout (5 , TimeUnit .SECONDS )
54
+ .addInterceptor (new OkHttpRetryInterceptor (retryTimes , retryIntervalMilliseconds ))
55
+ .readTimeout (readTimeoutSecond , TimeUnit .SECONDS )
56
+ .writeTimeout (writeTimeoutSecond , TimeUnit .SECONDS )
57
+ .connectTimeout (connectTimeoutSecond , TimeUnit .SECONDS )
57
58
.connectionPool (new ConnectionPool (100 , 5 , TimeUnit .MINUTES ))
58
59
.build ();
59
60
}
60
61
62
+ public LmstfyClient (String host , int port , String namespace , String token ) {
63
+ this (host , port , namespace , token , 600 , 600 , 5 , 3 , 100 );
64
+ }
65
+
61
66
private LmstfyResponse doRequest (String method , HttpUrl url , RequestBody body ) throws LmstfyException {
62
67
Request request = new Request .Builder ()
63
68
.url (url )
@@ -114,7 +119,7 @@ public String publish(String queue, byte[] data, int ttlSecond, short tries, int
114
119
* a job; if it's positive, this method would polling for new job until timeout.
115
120
* @param queues You can consume multiple queues of the same namespace at once. The order of the queues in
116
121
* the params implies the priority.
117
- * @return Return the job, if there is no job available, it will return null .
122
+ * @return Return the job.
118
123
* @throws LmstfyException
119
124
*/
120
125
public Job consume (int ttrSecond , int timeoutSecond , String ... queues ) throws LmstfyException {
@@ -126,14 +131,12 @@ public Job consume(int ttrSecond, int timeoutSecond, String... queues) throws Lm
126
131
LmstfyResponse response = this .doRequest ("GET" , url , null );
127
132
switch (response .getCode ()) {
128
133
case HTTP_OK :
129
- Job job = response .unmarshalBody (Job .class );
130
- job .setData (new String (Base64 .getDecoder ().decode (job .getBase64Data ())));
131
- return job ;
134
+ return response .unmarshalToJob ();
132
135
case HTTP_BAD_REQUEST :
133
136
ErrorResponse errorResponse = response .unmarshalBody (ErrorResponse .class );
134
137
throw new LmstfyIllegalRequestException (response .getCode (), errorResponse .getError ());
135
138
case HTTP_NOT_FOUND :
136
- return null ;
139
+ throw new LmstfyNotJobException () ;
137
140
default :
138
141
throw new LmstfyUnexpectedException (response .getCode ());
139
142
}
@@ -223,20 +226,83 @@ public Job peekJob(String queue, String jobID) throws LmstfyException {
223
226
return this .peek (url );
224
227
}
225
228
229
+ /**
230
+ * Peek the deadletter of the queue
231
+ *
232
+ * @param queue
233
+ * @return
234
+ * @throws LmstfyException
235
+ */
236
+ public DeadLetterResponse peekDeadLetter (String queue ) throws LmstfyException {
237
+ HttpUrl url = genServiceUrlBuilder (PATH_API , this .namespace , queue , "deadletter" )
238
+ .build ();
239
+
240
+ LmstfyResponse response = doRequest ("GET" , url , null );
241
+ switch (response .getCode ()) {
242
+ case HTTP_OK :
243
+ DeadLetterResponse deadLetterResponse = response .unmarshalBody (DeadLetterResponse .class );
244
+ return deadLetterResponse ;
245
+ default :
246
+ throw new LmstfyUnexpectedException (response .getCode ());
247
+ }
248
+ }
249
+
226
250
private Job peek (HttpUrl url ) throws LmstfyException {
227
251
LmstfyResponse response = doRequest ("GET" , url , null );
228
252
switch (response .getCode ()) {
229
253
case HTTP_OK :
230
- Job job = response .unmarshalBody (Job .class );
231
- job .setData (new String (Base64 .getDecoder ().decode (job .getBase64Data ())));
232
- return job ;
254
+ return response .unmarshalToJob ();
233
255
case HTTP_NOT_FOUND :
234
256
return null ;
235
257
default :
236
258
throw new LmstfyUnexpectedException (response .getCode ());
237
259
}
238
260
}
239
261
262
+ /**
263
+ * Respawn job(s) in the dead letter
264
+ *
265
+ * @param queue
266
+ * @param limit The number (upper limit) of the jobs to be respawned.
267
+ * @param ttlSecond Time-to-live of this job in seconds, 0 means forever.
268
+ * @return The number of jobs that're respawned.
269
+ * @throws LmstfyException
270
+ */
271
+ public int respawnDeadLetter (String queue , int limit , int ttlSecond ) throws LmstfyException {
272
+ HttpUrl url = genServiceUrlBuilder (PATH_API , this .namespace , queue , "deadletter" )
273
+ .addQueryParameter (QUERY_LIMIT , String .valueOf (limit ))
274
+ .addQueryParameter (QUERY_TTL , String .valueOf (ttlSecond ))
275
+ .build ();
276
+ LmstfyResponse response = doRequest ("PUT" , url , null );
277
+ switch (response .getCode ()) {
278
+ case HTTP_OK :
279
+ RespawnResponse respawnResponse = response .unmarshalBody (RespawnResponse .class );
280
+ return respawnResponse .getCount ();
281
+ default :
282
+ throw new LmstfyUnexpectedException (response .getCode ());
283
+ }
284
+ }
285
+
286
+ /**
287
+ * Delete job(s) in the dead letter
288
+ *
289
+ * @param queue
290
+ * @param limit The number (upper limit) of the jobs to be deleted
291
+ * @throws LmstfyException
292
+ */
293
+ public void deleteDeadLetter (String queue , int limit ) throws LmstfyException {
294
+ HttpUrl url = genServiceUrlBuilder (PATH_API , this .namespace , queue , "deadletter" )
295
+ .addQueryParameter (QUERY_LIMIT , String .valueOf (limit ))
296
+ .build ();
297
+ LmstfyResponse response = doRequest ("DELETE" , url , null );
298
+ switch (response .getCode ()) {
299
+ case HTTP_NO_CONTENT :
300
+ return ;
301
+ default :
302
+ throw new LmstfyUnexpectedException (response .getCode ());
303
+ }
304
+ }
305
+
240
306
private HttpUrl .Builder genServiceUrlBuilder (String ... pathSegments ) {
241
307
HttpUrl .Builder builder = serviceAddress .newBuilder ();
242
308
for (String pathSegment : pathSegments ) {
0 commit comments