Async Servlet not working as expected | Community
Skip to main content
December 10, 2015
Solved

Async Servlet not working as expected

  • December 10, 2015
  • 5 replies
  • 2695 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

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. 

5 replies

Kunal_Gaba_
December 10, 2015

Why can't you intercept the browser close event in the javascript and send an HTTP request to your servlet to close everything ?

December 10, 2015

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

smacdonald2008
smacdonald2008Accepted solution
Level 10
December 10, 2015

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. 

Sunjeet_Gupta
Level 2
August 8, 2018

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 ?

April 27, 2023

is it okay to have a sling job to do the task asynchronously even if the servlet has responded back ?