How to do it...

The following five steps are required to complete this recipe:

  1. Let's first create a User POJO:
public class User {

private Long id;
private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public User(Long id, String name) {
this.id = id;
this.name = name;
}

@Override
public String toString() {
return "User{" + "id=" + id + ",
name=" + name + '}';
}
}
  1. Then, create a slow bean to return User:
public class UserBean {

public User getUser(){
try {
TimeUnit.SECONDS.sleep(5);
long id = new Date().getTime();
return new User(id, "User " + id);
} catch (InterruptedException ex) {
long id = new Date().getTime();
return new User(id, "Error " + id);
}
}
}
  1. Now, create a Managed task so that we can monitor it:
@Stateless
public class AsyncTask implements Callable<User>, ManagedTaskListener {

private final long instantiationMili = new Date().getTime();

private static final Logger LOG = Logger.getAnonymousLogger();

@Override
public User call() throws Exception {
return new UserBean().getUser();
}

@Override
public void taskSubmitted(Future<?> future,
ManagedExecutorService mes, Object o) {
long mili = new Date().getTime();
LOG.log(Level.INFO, "taskSubmitted: {0} -
Miliseconds since instantiation: {1}",
new Object[]{future, mili - instantiationMili});
}

@Override
public void taskAborted(Future<?> future,
ManagedExecutorService mes, Object o, Throwable thrwbl)
{
long mili = new Date().getTime();
LOG.log(Level.INFO, "taskAborted: {0} -
Miliseconds since instantiation: {1}",
new Object[]{future, mili - instantiationMili});
}

@Override
public void taskDone(Future<?> future,
ManagedExecutorService mes, Object o,
Throwable thrwbl) {
long mili = new Date().getTime();
LOG.log(Level.INFO, "taskDone: {0} -
Miliseconds since instantiation: {1}",
new Object[]{future, mili - instantiationMili});
}

@Override
public void taskStarting(Future<?> future,
ManagedExecutorService mes, Object o) {
long mili = new Date().getTime();
LOG.log(Level.INFO, "taskStarting: {0} -
Miliseconds since instantiation: {1}",
new Object[]{future, mili - instantiationMili});
}

}
  1. Finally, create a service endpoint to execute our task and return its results:
@Stateless
@Path("asyncService")
public class AsyncService {

@Resource
private ManagedExecutorService executor;

@GET
public void asyncService(@Suspended AsyncResponse response) {
int i = 0;

List<User> usersFound = new ArrayList<>();
while (i < 4) {
Future<User> result = executor.submit(new AsyncTask());

while (!result.isDone()) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
System.err.println(ex.getMessage());
}
}

try {
usersFound.add(result.get());
} catch (InterruptedException | ExecutionException ex) {
System.err.println(ex.getMessage());
}

i++;
}

response.resume(Response.ok(usersFound).build());
}

}
  1. To try this code, simply deploy it to Eclipse GlassFish 5 and open the following URL:
http://localhost:8080/ch09-task-status/asyncService
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.199.162