Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to an asynchronous servlet on WebSphere 8 results in AsyncIllegalStateException

I'm getting the following exception while calling a servlet:

com.ibm.ws.webcontainer.async.AsyncIllegalStateException: SRVE8010E: The current request does not support asynchronous servlet processing.

The servlet looks like this:

public class AsyncServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

      try {
          AsyncContext async = req.startAsync();
          async.start(new Runnable(){
              @Override
              public void run() {
                System.out.println("Bazinga");
              }
          });
      } catch (Exception e) {
        e.printStackTrace();
      }

  }
}

I've tried to set it as asynchronous using annotations:

@WebServlet(urlPatterns = "/asyncServlet", asyncSupported = true)

And also in web.xml after reading a post in a forum:

<servlet>
    <display-name>AsyncServlet</display-name>
    <servlet-name>AsyncServlet</servlet-name>
    <servlet-class>com.lala.lala.AsyncServlet</servlet-class>
    <init-param>
        <param-name>com.ibm.ws.webcontainer.async-supported</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>AsyncServlet</servlet-name>
    <url-pattern>/asyncServlet</url-pattern>
</servlet-mapping>

Still getting the AsyncIllegalStateException. How did you get async servlets run on WAS8?

like image 931
Maxim Suponya Avatar asked Nov 04 '22 04:11

Maxim Suponya


1 Answers

I found something similar on one of the forums, maybe it will help:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/AsyncServlet", asyncSupported = true)
public class AsyncServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    // Record the servlet's thread
    PrintWriter out = response.getWriter();
    out.printf("<h3>Servlet's thread: %s</h3>", Thread.currentThread());
    out.flush();

    // Put the request into asynchronous mode
    request.startAsync();

    // Run an asynchronous task via servlet 3.0's abstractions
    AsyncContext asyncCtx = request.getAsyncContext();
    MyTask mt = new MyTask(asyncCtx);
    asyncCtx.start(mt);

    // Run another asynchronous task via java's abstractions
    MyTask mt02 = new MyTask(asyncCtx);
    new Thread(mt02).start();

    out.printf("<h3>Servlet finishes its job</h3>");
}

public class MyTask implements Runnable {

    AsyncContext asyncContext;

    public MyTask(AsyncContext asyncContext) {
        this.asyncContext = asyncContext;
    }

    @Override
    public void run() {
        ServletResponse response = asyncContext.getResponse();
        try {
            // do the time-consuming job
            PrintWriter out = response.getWriter();
            for (int i = 0; i < 5; i++) {
                out.printf("<h2>Hello from thread: %s (%d)</h2>", Thread.currentThread(), i);
                out.flush();
                Thread.sleep(1 /* secs */ * 1000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            asyncContext.complete();
        }
    }
}
}

found here

like image 61
knowbody Avatar answered Nov 12 '22 22:11

knowbody