public class AsyncServlet extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) {
// set container thread free
final AsyncContext context = request.startAsync();
// process request in an App thread
executor.submit(()->{
try {
// call AsyncServlet to process
serviceInt(context.getRequest(), context.getResponse());
}
finally {
// close user request / response / etc
context.complete();
}
})
}
}
public interface AsyncContext {
public ServletRequest getRequest();
public ServletResponse getResponse();
public void complete();
// what is dispatch??
public void dispatch();
}
web.xml
NocacheFilter
ru.naumen.core.server.NocacheFilter
true
jwt-servlet
ru.naumen.sec.server.jwt.provider.JwtTokenServlet
true
Annotations
@WebServlet(urlPatterns = { "/async" }, asyncSupported=true)
public class AsyncServlet extends HttpServlet {
}
@WebFilter("/*", asyncSupported=true)
public class MyFilter implements Filter {
}
with Tomcat 8
public class AsyncServlet extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) {
final AsyncContext context = request.startAsync();
executor.submit(()->{
try {
// WTF ?!?!
context.getRequest().getRequestDispatcher("/denied.jsp")
.forward(request, response);
}
finally {
context.complete();
}
});
}
}
public class AsyncServlet extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) {
final AsyncContext context = request.startAsync();
executor.submit(()->{
boolean forward = false;
try {
forward = true;
// no RequestDispatcher needed
context.dispatch("/denied.jsp");
}
finally {
if(!forward)
context.complete();
}
});
}
}
public class NocacheFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, final ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
// do pre processing
try {
// call handlers
chain.doFilter(request, response);
} finally {
// do post processing
}
}
// from Spring Security with love!
// somewhere in filter chain
chain.doFilter(new WrappedRequest(request),
new WrappedResponse(response));
// async servlet
public void service(HttpServletRequest req, HttpServletResponse resp) {
AsyncContext context = request.startAsync();
// at execution moment
// is context.getRequest() wrapped ?
// is context.getRequest() equals to req ?
context.getRequest().getInputStream().available();
context.complete();
}
// from JavaMelody with love!
public class AsyncServlet extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) {
final AsyncContext context = request.startAsync();
executor.submit(()->{
try {
doSomeWorkAndGenerateException();
}
finally {
context.complete();
// what user will get in case of error?
}
});
}
}
public void service(HttpServletRequest req, HttpServletResponse resp) {
// if an error has occured during async processing
if(req.getAttribute(RequestDispatcher.ERROR_EXCEPTION)){
throw new ServletException();
}
// if everything is ok
final AsyncContext context = request.startAsync();
executor.submit(()->{
// doSomeWorkAndGenerateException();
catch(Throwable tr) {
context.getRequest().setAttribute(
RequestDispatcher.ERROR_EXCEPTION, tr);
// send request back
context.dispatch();
return;
}
// in case everything is ok
context.complete();
});
}
public interface HttpServletResponse {
public PrintWriter getWriter();
public ServletOutputStream getOutputStream();
public int getBufferSize();
public void resetBuffer();
public void flushBuffer();
}
public interface HttpServletRequest {
public String getRequestURI();
public String getContextPath();
public String getPathInfo();
public String getServletPath();
public String getQueryString();
}
public interface AsyncContext {
/**
* The name of the request attribute under which the original
* .... is made available to the target of a
* {@link #dispatch(String)} or {@link #dispatch(ServletContext,String)}
*/
static final String ASYNC_REQUEST_URI="javax.servlet.async.request_uri";
static final String ASYNC_CONTEXT_PATH="javax.servlet.async.context_path";
static final String ASYNC_PATH_INFO="javax.servlet.async.path_info";
static final String ASYNC_SERVLET_PATH="javax.servlet.async.servlet_path";
static final String ASYNC_QUERY_STRING="javax.servlet.async.query_string";
}
public interface HttpServletResponse {
public void setContentLength(int len);
public void setContentLengthLong(long len);
// name = "Content-Length"
public void addHeader(String name, String value);
public void setIntHeader(String name, int value);
}
public void service(HttpServletRequest req, HttpServletResponse resp) {
// if everything is ok
final AsyncContext context = request.startAsync();
executor.submit(()->{
try{
doSomeWorkAndGenerateException();
}
catch(Throwable tr) {
// send request back
context.dispatch();
return;
}
// in case everything is ok
context.complete();
});
}
// avoid setContentLength()
// start
AsyncContext context = request.startAsync();
try{
// avoid setContentLength()
doWork();
}
catch(Throwable e) {
// store e in the request attribute
context.dispatch();
}
finally {
if(!forward){
// no flush - no data
response.flush();
// cleanup
context.complete();
}
}