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
Solved! Go to Solution.
Views
Replies
Total Likes
We talked about this use case today - in AEM, there does not appear to be a way of preventing the servlet from completing execution of code once it was invoked. The thread will stop once the servlet reached the end of the app logic. It will attempt to return a value as well.
Views
Replies
Total Likes
Why can't you intercept the browser close event in the javascript and send an HTTP request to your servlet to close everything ?
Views
Replies
Total Likes
Thanks kunal23 for your response.
Yes thats a possible solution. But what happens for example when the browser crashes? In this case i doesn't get any request form javascript and so i never get informed that i can do my cleanup. And at some point my AEM will be sticky with threads.
I hope you got my point. I don't want to be dependent on the client side. There can be happen many things form a crashing system to network interruption.
Best regards
Mike
Views
Replies
Total Likes
We talked about this use case today - in AEM, there does not appear to be a way of preventing the servlet from completing execution of code once it was invoked. The thread will stop once the servlet reached the end of the app logic. It will attempt to return a value as well.
Views
Replies
Total Likes
Hi Mikes/Donald, We are stuck with similar situation and want to implement Async Sling servlet within AEM. Do you have any boilerplate code snippet which can be shared for this one ?
Views
Replies
Total Likes
is it okay to have a sling job to do the task asynchronously even if the servlet has responded back ?
Views
Replies
Total Likes