I found a post on the CXF mailing list that pointed out the very specific
verbiage in the Jersey docs. In the Jersey docs, it says "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 the "while
the response is being written back to the client."
To test this I added these lines to the end of your "poll" method:
ChunkedOutput<String> chunkedOutput = new
ChunkedOutput<String>(String.class);
asyncResponse.resume(chunkedOutput);
for (int i = 0; i < 50000; i++) {
chunkedOutput.write("CHUNK-");
}
When I hit the URL with curl and CTRL-C'ed the response (after seeing
several "CHUNK-"s), the ConnectionCallback fired. (I found this in one of
the Jersey tests
<
https://github.com/jersey/jersey/blob/master/tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/server/AsyncCallbackTest.java>
).
Hope this helps!
- Paul
On Tue, Nov 11, 2014 at 3:03 PM, Ric Bernat <ric_at_brinydeep.net> wrote:
> Really would appreciate any feedback at all on this question. It is a
> significant blocker for us. If this mailing list is not active, are there
> other resources I might check for Jersey development support/mentoring?
>
> Ric
>
>
> On 11/8/2014 8:34 AM, Ric Bernat wrote:
>
>> For asynchronous programming, Jersey provides a ConnectionCallback
>> callback that is to be executed when a connection is broken. From the
>> Jersey docs:
>>
>> > 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.
>>
>> https://jersey.java.net/documentation/latest/async.html#d0e8863
>>
>> Sounds great, but I can never get this to fire.
>>
>> I am running Jersey 2.13 on Tomcat 7.0.53. Clients are connecting
>> directly to Tomcat (no Apache).
>>
>> Here is the web service:
>>
>> @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());
>>
>> }
>> });
>> }
>>
>> 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.
>>
>> 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.
>>
>> Am I missing something?
>>
>> Any input would be appreciated.
>>
>>
>