JAX-RS and a long survey

I am trying to use a lengthy poll with JAX-RS (Jersey performance) and it does not work as I expect. Maybe I don’t understand something. I would be grateful for any advice.

Please note that using a reverse connection (something like Atmosphere, Comet, etc.) is not an option for security. Not that I am developing with Tomcat 7 right now.

The following method is called from a jQuery Ajax call (using $.ajax).

@Path("/poll")
@GET
public void poll(@Suspended final AsyncResponse asyncResponse)
        throws InterruptedException {
    new Thread(new Runnable() {
        @Override
        public void run() {
            this.asyncResponse = asyncResponse;
            // wait max. 30 seconds using a CountDownLatch
            latch.await(getTimeout(), TimeUnit.SECONDS);
        }
    }).start();
}

Another method is called from my application (after a JMS call):

@POST
@Path("/printed")
public Response printCallback() {
    // ...

    // I expect the /poll call to be ended here from the client perspective but that is not the case
    asyncResponse.resume("UPDATE"); 
    latch.countDown();

    return Response.ok().build();
}

If I remove the Thread creation in the method poll. Then it works, but the problem is that the thread is busy. If I use Thread creation, the method returns directly, and the browser does not detect the end of a long poll.

What am I doing wrong?

+4
1

. . , async, :

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <async-supported>true</async-supported>
    ...
</servlet>

, , async-supported true.

. :

@Path("/poll")
@GET
public void poll(@Suspended final AsyncResponse asyncResponse)
        throws InterruptedException {
    asyncResponse.setTimeout(30, TimeUnit.SECONDS);
    this.asyncResponse = asyncResponse;
}

@POST
@Path("/printed")
public Response printCallback(String barcode) throws IOException {
    // ...

    this.asyncResponse.resume("MESSAGE");

    return Response.ok().build();
}

poll MESSAGE HTTP 503, . , . JavaScript, , -, - .

+3

All Articles