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);
}
Aggregations