5

サーブレットの呼び出し中に次の例外が発生します。

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

サーブレットは次のようになります。

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();
      }

  }
}

注釈を使用して非同期として設定しようとしました:

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

また、フォーラムで投稿を読んだ後の web.xml にも:

<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>

まだ AsyncIllegalStateException を取得しています。WAS8 で非同期サーブレットを実行する方法を教えてください。

4

1 に答える 1

2

私はフォーラムの1つで似たようなものを見つけました、多分それは役立つでしょう:

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();
        }
    }
}
}

ここで見つかりました

于 2013-02-21T22:24:49.063 に答える