Search in sources :

Example 51 with AsyncEvent

use of javax.servlet.AsyncEvent in project jwt by emweb.

the class ServletApi3 method doHandleRequest.

@Override
public void doHandleRequest(final WtServlet servlet, final WebRequest request, final WebResponse response) {
    if (request.isAsyncSupported()) {
        request.startAsync();
        final long asyncContextTimeout = servlet.getConfiguration().getAsyncContextTimeout();
        request.getAsyncContext().setTimeout(asyncContextTimeout);
        final AtomicBoolean handleRequestFinished = new AtomicBoolean(false);
        class MutableWrapper<T> {

            T t = null;
        }
        final MutableWrapper<Thread> threadWrapper = new MutableWrapper<Thread>();
        request.getAsyncContext().addListener(new AsyncListener() {

            @Override
            public void onTimeout(AsyncEvent e) throws IOException {
                if (handleRequestFinished.get())
                    return;
                String newLine = "\n\t";
                StringBuilder params = new StringBuilder("Parameters: " + newLine);
                for (Map.Entry<String, String[]> param : request.getParameterMap().entrySet()) params.append(param.getKey()).append(": ").append(Arrays.toString(param.getValue())).append(newLine);
                int uploadedFilesCount = 0;
                if (request.getUploadedFiles() != null)
                    uploadedFilesCount = request.getUploadedFiles().size();
                StringBuilder msg = new StringBuilder();
                msg.append("Timeout: waiting more then ").append(asyncContextTimeout).append(newLine).append("url: ").append(request.getRequestURL()).append(newLine).append("Uploaded files count: ").append(uploadedFilesCount).append(newLine).append(params);
                if (threadWrapper.t != null)
                    msg.append("AsyncContext thread stack trace: ").append(newLine).append(Arrays.toString(threadWrapper.t.getStackTrace()).replaceAll(", ", newLine));
                logger.error(msg.toString(), e.getThrowable());
            }

            @Override
            public void onStartAsync(AsyncEvent arg0) throws IOException {
            }

            @Override
            public void onError(AsyncEvent e) throws IOException {
                logger.error("Error during async request ", e.getThrowable());
            }

            @Override
            public void onComplete(AsyncEvent arg0) throws IOException {
            }
        });
        request.getAsyncContext().start(new Runnable() {

            @Override
            public void run() {
                threadWrapper.t = Thread.currentThread();
                handleRequest(servlet, request, response);
                handleRequestFinished.set(true);
            }
        });
    } else
        handleRequest(servlet, request, response);
}
Also used : IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncListener(javax.servlet.AsyncListener)

Aggregations

AsyncEvent (javax.servlet.AsyncEvent)51 IOException (java.io.IOException)38 AsyncContext (javax.servlet.AsyncContext)35 AsyncListener (javax.servlet.AsyncListener)32 HttpServletResponse (javax.servlet.http.HttpServletResponse)23 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 Test (org.junit.Test)19 ServletException (javax.servlet.ServletException)12 HttpServlet (javax.servlet.http.HttpServlet)9 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ServletResponse (javax.servlet.ServletResponse)5 OutputStream (java.io.OutputStream)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 FilterChain (javax.servlet.FilterChain)4 Socket (java.net.Socket)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 HttpTester (org.eclipse.jetty.http.HttpTester)3