Timer not working for jersy asyncContext case

Issue #6 new
Atul Phalke created an issue

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)

  1. Atul Phalke reporter

    Hey Marshall when we can get fix on this. I have change ready. Can I create a PR for this?

  2. Marshall Pierce 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 why FINISHED never gets sent. There may be better ways to do it, but response.resume((Object) null) right after asyncContext.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.

  3. Log in to comment