我有一个 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
那感觉不对。中断这种等待请求的正确方法是什么?