4

I'm new to JavaEE 6 and I learnt that it support asynchronous servlets called AsyncServlets. So I tried to create a small program using JavaEE6.

Here is my code for the servlet

@WebServlet(name = "AsyncServlet", urlPatterns = {"/AsyncServlet"}, asyncSupported=true)
public class AsyncServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        AsyncContext aCtx = request.startAsync(request, response);
        Executor executor = Executors.newSingleThreadExecutor();
        executor.execute(new MyClass(aCtx));
        System.out.println("Original thread is freed");
    }




} 

But when I try to run the servlet I get an exception java.lang.IllegalStateException: Not supported. I'm using tomcat 7.0.14 as the server. i did not create a web.xml. Where is the problem in this code?

EDIT:

This is the full stack-trace of the exception

SEVERE: Servlet.service() for servlet [AsyncServlet] in context with path [/AsyncTest] threw exception
java.lang.IllegalStateException: Not supported.
at org.apache.catalina.connector.Request.startAsync(Request.java:1618)
at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:379)
at com.icbt.demo.servlet.AsyncServlet.doGet(AsyncServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662) 
Philippe Marschall
  • 4,452
  • 1
  • 34
  • 52
Krishan
  • 641
  • 4
  • 18
  • I don't see any problem in your code (apart from creating a thread pool per each request and never closing it!) - can you post the full stack trace of the exception being thrown? – Tomasz Nurkiewicz Jun 10 '12 at 17:47
  • 1
    Are you sure `asyncSupported` attribute is set to `true`? Try restarting/recompiling to make sure it is reflected. Your servlet works for me, however I get the same error when `asyncSupported=false` is used. – Tomasz Nurkiewicz Jun 10 '12 at 18:01
  • @ Tomasz Yeah.. Even if I create a new application, problem still remains. Seems like something's wrong with the server – Krishan Jun 10 '12 at 18:06
  • anyhow it works in glassfish 3.1.2.1. i think the problem lies is tomcat – Krishan Jun 15 '12 at 15:14
  • SOLVED: I refer you to this post: http://stackoverflow.com/questions/7855712/how-to-avoid-request-set-async-supported-true-to-enable-async-servlet-3-0-proces?rq=1 – Given Nov 28 '12 at 11:44

2 Answers2

9

if you use tomcat as your web server then you should maintion that you are going to use asyc request to tomcat

before start async request put this line code

request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);

i hope you can get what u want from it

Nirav Prajapati
  • 2,987
  • 27
  • 32
4

An IllegalStateException will be thrown if an application tries to start an asynchronous operation and there is a servlet or servlet filter in the request processing chain that does not support asynchronous processing.So there might be have some mistake.plz check your code.

Biswajit
  • 2,434
  • 2
  • 28
  • 35
  • yeah I'm pretty sure of that. It works in glassfish. I think this is a bug in apache tomcat 7.0.14. It works in later tomcat versions as well. – Krishan Oct 02 '12 at 10:46