/
AsyncResource.java
58 lines (52 loc) · 1.68 KB
/
AsyncResource.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.hantsylab.example.ee7.blog.api;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Response;
/**
*
* @author hantsy
*/
@Path("async")
@Stateless
public class AsyncResource {
@Resource
private ManagedExecutorService executor;
@Inject
Logger LOG;
@GET
public void getAsync(final @Suspended AsyncResponse res) {
res.setTimeoutHandler(
(ar) -> {
ar.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE)
.entity("Operation timed out --- please try again.").build());
}
);
res.setTimeout(1000, TimeUnit.MILLISECONDS);
executor.submit(() -> {
//do long run operations.
try {
LOG.log(Level.INFO, " execute long run task in AsyncResource");
//Thread.sleep(new Random().nextInt(1005));
Thread.sleep(500);
} catch (InterruptedException ex) {
LOG.log(Level.SEVERE, "error :" + ex.getMessage());
}
res.resume(Response.ok("asynchronous resource").build());
});
}
}