AsyncResponse ConnectionCallback不火泽西 [英] AsyncResponse ConnectionCallback does not fire in Jersey

查看:638
本文介绍了AsyncResponse ConnectionCallback不火泽西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关异步编程,新泽西州(JAX-RS)提供了一个 ConnectionCallback 回调是当连接中断执行。从新泽西文档

For asynchronous programming, Jersey (JAX-RS) provides a ConnectionCallback callback that is to be executed when a connection is broken. From the Jersey docs:

由于一些异步请求可能要花很长的时间来处理可能在客户端
  决定终止反应之前其连接到服务器
  已经恢复或之前它已被完全写入到客户端。至
  处理这些用例一ConnectionCallback可以使用。这个
  如果连接prematurely回调将只执行
  终止或丢失而响应正被写入到背
  客户。请注意,当一个反应是这个回调将不会被调用
  成功地写入,并预期客户端连接被关闭。

As some async requests may take long time to process the client may decide to terminate its connection to the server before the response has been resumed or before it has been fully written to the client. To deal with these use cases a ConnectionCallback can be used. This callback will be executed only if the connection was prematurely terminated or lost while the response is being written to the back client. Note that this callback will not be invoked when a response is written successfully and the client connection is closed as expected.

听起来很棒,但我永远不能得到这个火。

Sounds great, but I can never get this to fire.

下面是一些code:

@GET
@Produces(MediaType.TEXT_PLAIN)
@ManagedAsync
@Path("/poll")
public void poll(@Suspended final AsyncResponse asyncResponse) {
    asyncResponse.register(new CompletionCallback() {
        @Override
        public void onComplete(Throwable throwable) {
            logger.info("onComplete called.");
        }
    });

    asyncResponse.register(new ConnectionCallback() {
        @Override
        public void onDisconnect(AsyncResponse disconnected) {
            logger.info("onDisconnect called.");
        }
    });

    asyncResponse.setTimeout(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    asyncResponse.setTimeoutHandler(new TimeoutHandler() {
        @Override
        public void handleTimeout(AsyncResponse asyncResponse) {
            logger.info("handleTimeout called.");
            asyncResponse.resume(Response.status(Response.Status.OK).entity("TIMEOUT").build());
        }
    });
}

另外两个回调所示,CompletionCallback和TimeoutHandler,火就好了,没有失败。如果指定的超时时间到达,TimeoutHandler火灾。如果AsyncResponse实例恢复,CompletionCallback火灾。

The other two callbacks shown, CompletionCallback and TimeoutHandler, fire just fine, without fail. If the specified timeout duration is reached, TimeoutHandler fires. If an AsyncResponse instance is resumed, CompletionCallback fires.

然而,随着ConnectionCallback,我可以关闭,杀,或以其他方式停止坐在连接到Web服务上面显示的客户端,ConnectionCallback永远不会被解雇了。

However, with ConnectionCallback, I can close, kill, or otherwise stop the client that is sitting connected to the web service shown above, and ConnectionCallback never gets fired.

我缺少的东西吗?是ConnectionCallback泽西实施? (这是在JAX-RS规范可选的,但是泽西文档谈论它就像它的执行。)

Am I missing something? Is ConnectionCallback implemented in Jersey? (It's optional in the JAX-RS spec, but the Jersey docs talk about it as though it's implemented.)

任何投入将是AP preciated。

Any input would be appreciated.

推荐答案

ConnectionCallback确实是在泽西实施。和onDisconnect'回调也调用。你可以看看下面的code泽西岛:

ConnectionCallback is indeed implemented in Jersey. And 'onDisconnect' callback is also invoked. You can take a look at the following code in Jersey:

<一个href=\"https://github.com/jersey/jersey/blob/a6ff4d50da13d45ad90fd7375a15a31afa02e489/core-server/src/main/java/org/glassfish/jersey/server/ServerRuntime.java#L723\" rel=\"nofollow\">https://github.com/jersey/jersey/blob/a6ff4d50da13d45ad90fd7375a15a31afa02e489/core-server/src/main/java/org/glassfish/jersey/server/ServerRuntime.java#L723

<一个href=\"https://github.com/jersey/jersey/blob/b7907e279010e7035a7a3e529993d22f77a21e08/core-server/src/main/java/org/glassfish/jersey/server/ChunkedOutput.java#L246-L252\" rel=\"nofollow\">https://github.com/jersey/jersey/blob/b7907e279010e7035a7a3e529993d22f77a21e08/core-server/src/main/java/org/glassfish/jersey/server/ChunkedOutput.java#L246-L252

在编写响应这些被抛出IOException的情况下。因此,要回答你的问题,有没有轮询或类似的机制,不断检查客户端是否连接,而在 onDisconnect 当存在而写的反应通常发生IOException方法被调用而已。

These are thrown in case of IOException while writing response. So, to answer your question, there is no polling or similar mechanism that keeps checking whether the client is connected, rather the onDisconnect method is called only when there is an IOException which usually occurs while writing response.

更新1:

此外,我想引用你自己的问题:

Also I would like to quote your own question:

这个回调将仅如果连接prematurely执行
  终止或丢失而响应正被写入到背
  客户端

"This callback will be executed only if the connection was prematurely terminated or lost while the response is being written to the back client"

因此​​,除非你尝试写入该流后,您的回调将永远不会被解雇。需要明确的是,这并不意味着要调用的时候没有反应写入或当响应是202,而它的意思被调用时,在终止连接prematurely 而响应被写入

So, unless you try to write to that stream, your callback will never be fired. To be clear, it's not meant to be called when there is no response to write or when the response is 202, rather it's meant to be invoked when the connection was terminated prematurely while response is being written.

恐怕没有解决此问题的,除非你写的用某种轮询一些低级别的网络编程。但我不建议你去这一点。

I am afraid there is no workaround to this problem unless you write some low level network programming with polling of some sort. But I wouldn't recommend you to go for this.

我建议你重新考虑要处理这种故障的方式。

I suggest that you rethink the way you want to handle this failure.

这篇关于AsyncResponse ConnectionCallback不火泽西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆