Asynchronous Actions
This feature is work-in-progress, and available in 1.7.0-SNAPSHOT. Requires a Servlet3+ container.
Description
Async Actions allows handler methods to behave in an asynchronous (Servlet3) fashion. This is mostly used for performance reasons, when a Stripes Action has to can connect to external services (e.g. a web service) in a non-blocking fashion.
Usage
Async event handlers must return void, and accept a single argument of type AsyncResponse :
signature
public void doSomething(AsyncResponse asyncResponse);
When handling such events, Stripes will start the asynchronous cycle, and let you complete by invoking one of the asyncResponse.complete() variants.
Examples
SimpleAsync
public class SimpleAsync implements ActionBean {
public void simpleEvent(AsyncResponse asyncResponse) {
// simple without really anything async...
asyncResponse.complete(new ForwardResolution("/WEB-INF/stuff.jsp"));
}
}
ReallyAsync
public class ReallyAsync implements ActionBean {
public void asyncEvent(final AsyncResponse asyncResponse) {
// submit to executor service
ExecutorService executor = ... ;
executorService.submit(new Runnable() {
@Override
public void run() {
// do anything in a separate thread and complete with a forward
asyncResponse.complete(new ForwardResolution("/WEB-INF/stuff.jsp"));
}
});
}
}
ReallyAsyncWithLambdas
public class ReallyAsyncWithLambdas implements ActionBean {
public void asyncEventWithLambda(final AsyncResponse asyncResponse) {
// submit to executor service
ExecutorService executor = ... ;
executorService.submit(() -> {
// do anything in a separate thread and complete with a forward
asyncResponse.complete(new ForwardResolution("/WEB-INF/stuff.jsp"));
});
}
}
NonBlockingClient
public class WithNonBlockingClient implements ActionBean {
public void nonBlocking(final AsyncResponse asyncResponse) {
// call non blocking http client
httpClient.get('/foo/bar', httpResponse -> {
// client completed : use http response
httpResponse.xyz();
// and complete...
asyncResponse.complete(new ForwardResolution("/WEB-INF/stuff.jsp"));
});
}
}
, multiple selections available,