4

i have a jersey service which generates a response. what i want to do is to poll a resource (in my case, a singleton class instance) for a success value, and as soon as i get the success value, perform some action

@Path("/generate")
class Generation{
    @POST
    @Produces("javax.ws.rs.core.MediaType.TEXT_PLAIN")
    public String generateAndPoll(){
        //Generate response
        /*Polling to start

        */
    return someValue;
    }
}

what may be a good way to accomplish that? Would timer be of any use?

Gaurav Sood
  • 680
  • 4
  • 17
  • 38

2 Answers2

2

As of Jersey 2.3.1, a new feature has been added to support server-sent events. For your use-case, you might want to read more into the Jersey documentation

Sujay
  • 6,753
  • 2
  • 30
  • 49
  • i read up on the documentation and was trying the AsyncResponse module of JAXRS2 API. I was unable to find the module even though I put the jar as a dependency in pom.xml as well as adding the jar to the project. what to do for that? – Gaurav Sood Oct 03 '13 at 10:00
  • I tried using the Timer class and the scheduleAtFixed rate method, and that seems to give me an error 405: method not allowed. – Gaurav Sood Oct 04 '13 at 03:41
0

If you don't mind using an external library, I have been using atmosphere for a few years and it is a great server push / comet implementation. It has support for just about ever server and yes it will depend on the server. They support long poll and websockets natively. Almost the entire service can be configured with just a couple of annotations. Here is an example of how to use it on a jersey 2 service.

https://github.com/Atmosphere/atmosphere-samples/blob/master/samples/jersey2-chat/src/main/java/org/atmosphere/samples/chat/jersey/Jersey2Resource.java

Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65