First, we start with a simple annotation:
asyncSupported = true
This tells the application server that this servlet supports asynchronous features. You need to use this throughout the entire servlet chain (including in filters, if there are any); otherwise, the application server will not work.
As the servlets are instantiated by the server, we can inject other context members onto it, such as our stateless bean:
@Inject
private UserBean userBean;
The main servlet method holds the actual request and response references and the request gives the context reference to the async API:
AsyncContext ctx = req.startAsync();
Then, we can execute the preceding blocking function in a non-blocking way:
ctx.start(() -< {
...
ctx.complete();
});