如何正确中断JAX-RS AJAX请求

时间:2013-01-15 12:48:11

标签: java ajax rest jboss jax-rs

我有一个部署到JBoss 7.1.1.Final的Java EE应用程序。该应用程序利用JAX-RS为客户端提供REST服务。其中一个服务保持客户端连接,直到通过BlockingQueue收到响应对象。到目前为止,这似乎工作正常。但是,即使删除了应用程序部署/关闭了应用程序,连接也会保持打开状态。它甚至可以阻止JBoss关闭。

所以我试图通过@PreDestroy回调中断等待线程来执行干净关闭。该服务如下所示:

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

它似乎有效,有点......客户收到预期的响应。但是,JBoss会打印此异常:

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

这感觉不对。中断这种等待请求的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您正在中断由容器创建的线程,因此容器无法完成线程本身。您可以使用状态变量检查线程是否应该结束,而不是中断。 (将其解释为伪代码):

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

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

return Response.noContent().build();

并在true中设置为@PreDestroy。还有AsyncResponse功能。您可以将所有打开的请求存储在列表中,然后在@PreDestroy取消它们。