简体   繁体   中英

How to properly interrupt JAX-RS AJAX request

I have a Java EE application deployed to JBoss 7.1.1.Final. The application makes use of JAX-RS to provide REST services to clients. One of the services holds the client connection until the response object is received via a BlockingQueue . That seems to work fine so far. However, the connection is kept open even if the application deployment is removed / the application is shutdown. It even prevents JBoss from shutting down.

So I was trying to perform a clean shutdown by interrupting the waiting thread via a @PreDestroy callback. The service looks like this:

@Path("/mypath")
@SessionScoped
public class MyResource implements Serializable {
  private static final long serialVersionUID = 1L;

  @Inject
  private EntityManager em;

  private Thread thread = null;

  @GET
  @Path("/{id:[0-9][0-9]*}")
  @Produces({
    MediaType.TEXT_PLAIN,
    MediaType.TEXT_XML
  })
  public Response getObjects(@PathParam("id") long id) {
    MyGroup group = this.em.find(MyGroup.class, id);
    if (group == null) {
      return Response.status(Status.NOT_FOUND)
          .entity("Group not found\n")
          .build();
    }

    if (group.isEmpty()) {
      return Response.status(Status.PRECONDITION_FAILED)
          .entity("Group is empty\n")
          .build();
    }

    BlockingQueue<?> queue = ... // Get queue for group

    this.thread = Thread.currentThread();
    try {
      MyObject object = queue.take();
    } catch (InterruptedException e) {
      return Response.noContent().build();
    }

    return Response.ok(object)
        .type(MediaType.TEXT_XML)
        .build();
  }

  @PreDestroy
  public void shutdown() {
    if (this.thread != null) {
      this.thread.interrupt();
    }
  }
}

It seems to work, sort of... The client receives the expected response. However, JBoss prints this exception:

13:38:44,489 ERROR [org.jboss.as.txn] JBAS010151: Unable to get transaction state: java.lang.IllegalStateException
    at org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:47)
    at org.jboss.as.txn.deployment.TransactionRollbackSetupAction.teardown(TransactionRollbackSetupAction.java:43)
    at org.jboss.as.web.ThreadSetupBindingListener.unbind(ThreadSetupBindingListener.java:61) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:636) [rt.jar:1.6.0_18]

13:38:44,492 ERROR [org.apache.catalina.connector.CoyoteAdapter] An exception or error occurred in the container during the request processing: java.lang.RuntimeException: java.lang.IllegalStateException
    at org.jboss.as.web.ThreadSetupBindingListener.unbind(ThreadSetupBindingListener.java:67) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:636) [rt.jar:1.6.0_18]
Caused by: java.lang.IllegalStateException
    at org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:47)
    at org.jboss.as.connector.deployers.processors.CachedConnectionManagerSetupProcessor$CachedConnectionManagerSetupAction.teardown(CachedConnectionManagerSetupProcessor.java:83)
    at org.jboss.as.web.ThreadSetupBindingListener.unbind(ThreadSetupBindingListener.java:61) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    ... 8 more

That just doesn't feel right. What is the proper way to interrupt such a waiting request?

You are interrupting a thread that was created by the container, hence the container fails to finish the thread itself. Instead of interrupting you could use a state variable to check if the thread should end, eg (interpret it as pseudo-code):

while( ! stopped ) {
    MyObject object = queue.poll(25, TimeUnit.MILLISECONDS);

    return Response.ok(object)
        .type(MediaType.TEXT_XML)
        .build();
}

return Response.noContent().build();

and set stopped to true in the @PreDestroy . Also there is the AsyncResponse feature. Where you could store all open requests in a list and then cancel them at @PreDestroy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM