7

我有一个 Java EE 应用程序部署到 JBoss 7.1.1.Final。该应用程序利用 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

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

4

1 回答 1

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.

于 2014-05-02T15:37:09.027 回答