Timer not working for jersy asyncContext case
We have a code like below
@GET
@ManagedAsync
@Path("async/{resource-id}")
@ResourceMetrics
public void asynRead(
@PathParam("resource-id") String abc,
final @Context HttpServletRequest request,
@Suspended AsyncResponse response) throws IOException {
//code to get data
ByteBuffer buffer = getData(abc);
final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
final AsyncContext asyncContext = request.getAsyncContext();
final ServletOutputStream s = asyncContext.getResponse().getOutputStream();
HttpOutput output = (HttpOutput) s;
s.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (s.isReady()) {
if (!readOnlyBuffer.hasRemaining()) {
asyncContext.complete();
return;
}
output.write(readOnlyBuffer);
}
}
@Override
public void onError(Throwable throwable) {
}
});
}
In this case event when request is finished is Type.RESOURCE_METHOD_FINISHED. In code in MetricsRequestEventListener.java check is for only RequestEvent.Type.FINISHED. After adding check for RESOURCE_METHOD_FINISHED its working for us.
Need o fix this.
Comments (7)
-
repo owner -
repo owner -
assigned issue to
-
assigned issue to
-
repo owner - edited description
-
reporter Hey Marshall when we can get fix on this. I have change ready. Can I create a PR for this?
-
repo owner Feel free to submit a PR. I haven't forgotten; I've just been busy. :)
-
repo owner It seems like the issue here is that you're using a Servlet-centric way to close the response (
AsyncContext
), not a Jersey way (AsyncResponse
), so Jersey never realizes the response is done. That's whyFINISHED
never gets sent. There may be better ways to do it, butresponse.resume((Object) null)
right afterasyncContext.complete()
is one way to tell Jersey that you're done.Also, using
RESOURCE_METHOD_FINISHED
is not useful since that is always basically zero on an async method. -
reporter Ok. I will try that. Thanks
- Log in to comment
I'll take a look, thanks for reporting it.