Async Servlet not working as expected
Hi,
i want to use a asynchronous servlet to let the user be informed about resent events. So i create a async servlet and everything works got as long the user stays on the page. But when the user closes the page / browser curiously the session stays open and the servlet thread sends data to a not more existing client. I expected to get a IOException in this case, so i can react on the exception and shutdown everything in the backend, e.g. unregister the streamwriter, interrupt open threads and so on.
But no excpetion is thrown! As part of a plain J2EE application running on a tomcat it works as expected but not inside AEM 6. Is there something special i had not in mind? Doesn't support the embedded jetty asyncrounos servlets? What i have overlooked?
My servlet looks like the following:
@SlingServlet(name = "TestServlet", paths = { "/bin/servlet/test" }, methods = { "GET" }) public class TestServlet extends SlingAllMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { handleRequest(request, response); } private void handleRequest(final SlingHttpServletRequest request, final SlingHttpServletResponse response) { // set ASYNC request.isAsyncSupported(); request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true); AsyncContext asyncContext = request.startAsync(request, response); asyncContext.setTimeout(10000); // subscribe to the events service which starts a new thread for the user // and pushes data to the response... }And the Thread which writes the data:
public void run() { try { response = asyncContext.getResponse(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); // write in stream ... response.getWriter().write(o.toString()); response.getWriter().flush(); response.flushBuffer(); } } catch (Exception e) { LOG.debug("Connection lost, maybe client is disconnected.", e); cancel(); } }Best regards
Mike