Long polling in java/jersey

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Long polling in java/jersey

Christopher Piggott
Hi,

I want to implement a log follower using long polling in
atmosphere-jersey.  The way I envision this is that you pass a
resource method for log entries greater than some value.  Example:


     GET /log?after=100


If an event greater than id 100 exists (e.g. 101), I want the response
to be generated right there and returned.  If not, I want to suspend.

1. Is there an example of the correct pattern to do this in jersey
atmosphere?  I'm playing around with something like this:

        @GET
        @Path("wait")
        public SuspendResponse longPoll(@QueryParam("after")
@DefaultValue("-1") int after) {
                if (logEntry.available(after)) {
                        /* return an immediate response */
                } else {
                        /* Suspend on a Broadcaster until a response becomes available */
                }
        }

...but I can't see a way to return either a responded response or an
immediate one.

2. Even if this did work, I think there are race conditions up the
wazoo, because a message could have gotten broadcast after the test
but before the suspend.  This tells me that my approach here is wrong,
but I haven't been able to find an example of a more correct way to do
this.

Would somebody point me to one (or paste if it's short enough)?

Thanks,

--Chris
Reply | Threaded
Open this post in threaded view
|

Re: Long polling in java/jersey

Alan Guégan
Hello Chris,

For your 1st question, there is an example that you can easily adapt : "Twitter Sample using Javascript and POJO"

Concerning your second question I have noting to say :-)

Alan

On Mon, Sep 5, 2011 at 7:15 PM, Christopher Piggott <[hidden email]> wrote:
Hi,

I want to implement a log follower using long polling in
atmosphere-jersey.  The way I envision this is that you pass a
resource method for log entries greater than some value.  Example:


    GET /log?after=100


If an event greater than id 100 exists (e.g. 101), I want the response
to be generated right there and returned.  If not, I want to suspend.

1. Is there an example of the correct pattern to do this in jersey
atmosphere?  I'm playing around with something like this:

       @GET
       @Path("wait")
       public SuspendResponse longPoll(@QueryParam("after")
@DefaultValue("-1") int after) {
               if (logEntry.available(after)) {
                       /* return an immediate response */
               } else {
                       /* Suspend on a Broadcaster until a response becomes available */
               }
       }

...but I can't see a way to return either a responded response or an
immediate one.

2. Even if this did work, I think there are race conditions up the
wazoo, because a message could have gotten broadcast after the test
but before the suspend.  This tells me that my approach here is wrong,
but I haven't been able to find an example of a more correct way to do
this.

Would somebody point me to one (or paste if it's short enough)?

Thanks,

--Chris

Reply | Threaded
Open this post in threaded view
|

Re: Long polling in java/jersey

Christopher Piggott
That's neat, but I am looking for something using Jersey.  I did find
some discussion between JFA and Paul Sandoz on the mailing list about
2 years back that suggested throwing a WebApplicationException with a
Response.ok() (200) ... I'm not really delighted with that idea,
throwing an exception for a totally normal response simply to hijack
Jersey ... but I could live with it, I suppose, if I had to; problem
is, though, that I still think there's a race condition and I'm not
sure I can solve it with external synchronization.



On Mon, Sep 5, 2011 at 3:37 PM, Alan Guégan <[hidden email]> wrote:

> Hello Chris,
>
> For your 1st question, there is an example that you can easily adapt :
> "Twitter Sample using Javascript and POJO"
>
> Concerning your second question I have noting to say :-)
>
> Alan
>
> On Mon, Sep 5, 2011 at 7:15 PM, Christopher Piggott <[hidden email]>
> wrote:
>>
>> Hi,
>>
>> I want to implement a log follower using long polling in
>> atmosphere-jersey.  The way I envision this is that you pass a
>> resource method for log entries greater than some value.  Example:
>>
>>
>>     GET /log?after=100
>>
>>
>> If an event greater than id 100 exists (e.g. 101), I want the response
>> to be generated right there and returned.  If not, I want to suspend.
>>
>> 1. Is there an example of the correct pattern to do this in jersey
>> atmosphere?  I'm playing around with something like this:
>>
>>        @GET
>>        @Path("wait")
>>        public SuspendResponse longPoll(@QueryParam("after")
>> @DefaultValue("-1") int after) {
>>                if (logEntry.available(after)) {
>>                        /* return an immediate response */
>>                } else {
>>                        /* Suspend on a Broadcaster until a response
>> becomes available */
>>                }
>>        }
>>
>> ...but I can't see a way to return either a responded response or an
>> immediate one.
>>
>> 2. Even if this did work, I think there are race conditions up the
>> wazoo, because a message could have gotten broadcast after the test
>> but before the suspend.  This tells me that my approach here is wrong,
>> but I haven't been able to find an example of a more correct way to do
>> this.
>>
>> Would somebody point me to one (or paste if it's short enough)?
>>
>> Thanks,
>>
>> --Chris
>
>
Reply | Threaded
Open this post in threaded view
|

[[atmosphere-users]] Re: Long polling in java/jersey

Jeanfrancois Arcand-4
In reply to this post by Christopher Piggott
Salut,

On 11-09-05 10:15 AM, Christopher Piggott wrote:

> Hi,
>
> I want to implement a log follower using long polling in
> atmosphere-jersey.  The way I envision this is that you pass a
> resource method for log entries greater than some value.  Example:
>
>
>       GET /log?after=100
>
>
> If an event greater than id 100 exists (e.g. 101), I want the response
> to be generated right there and returned.  If not, I want to suspend.
>
> 1. Is there an example of the correct pattern to do this in jersey
> atmosphere?  I'm playing around with something like this:
>
> @GET
> @Path("wait")
> public SuspendResponse longPoll(@QueryParam("after")
> @DefaultValue("-1") int after) {
> if (logEntry.available(after)) {
> /* return an immediate response */
Here just throw a WebApplicationException(Response), which will tell the
Atmosphere's Jersey extension to return the response directly.


> } else {
> /* Suspend on a Broadcaster until a response becomes available */
> }
> }
>
> ...but I can't see a way to return either a responded response or an
> immediate one.
>
> 2. Even if this did work, I think there are race conditions up the
> wazoo, because a message could have gotten broadcast after the test
> but before the suspend.  This tells me that my approach here is wrong,
> but I haven't been able to find an example of a more correct way to do
> this.
>
> Would somebody point me to one (or paste if it's short enough)?

I need more information here in order to help. Can you share privately a
test case?

Thanks!

-- Jeanfrancois



>
> Thanks,
>
> --Chris
Reply | Threaded
Open this post in threaded view
|

[[atmosphere-users]] Re: Long polling in java/jersey

Jeanfrancois Arcand-4
In reply to this post by Christopher Piggott


On 11-09-05 2:05 PM, Christopher Piggott wrote:
> That's neat, but I am looking for something using Jersey.  I did find
> some discussion between JFA and Paul Sandoz on the mailing list about
> 2 years back that suggested throwing a WebApplicationException with a
> Response.ok() (200) ... I'm not really delighted with that idea,
> throwing an exception for a totally normal response simply to hijack
> Jersey ... but I could live with it, I suppose, if I had to; problem
> is, though, that I still think there's a race condition and I'm not
> sure I can solve it with external synchronization.
...well, do you have any recommendation? I think one solution would be
to return a specialized version of the SuspendResponse, e.g a
NonSuspendResponse,

WDYT? I can add such support in Atmosphere.

Thanks!

-- Jeanfrancois



>
>
>
> On Mon, Sep 5, 2011 at 3:37 PM, Alan Guégan<[hidden email]>  wrote:
>> Hello Chris,
>>
>> For your 1st question, there is an example that you can easily adapt :
>> "Twitter Sample using Javascript and POJO"
>>
>> Concerning your second question I have noting to say :-)
>>
>> Alan
>>
>> On Mon, Sep 5, 2011 at 7:15 PM, Christopher Piggott<[hidden email]>
>> wrote:
>>> Hi,
>>>
>>> I want to implement a log follower using long polling in
>>> atmosphere-jersey.  The way I envision this is that you pass a
>>> resource method for log entries greater than some value.  Example:
>>>
>>>
>>>      GET /log?after=100
>>>
>>>
>>> If an event greater than id 100 exists (e.g. 101), I want the response
>>> to be generated right there and returned.  If not, I want to suspend.
>>>
>>> 1. Is there an example of the correct pattern to do this in jersey
>>> atmosphere?  I'm playing around with something like this:
>>>
>>>         @GET
>>>         @Path("wait")
>>>         public SuspendResponse longPoll(@QueryParam("after")
>>> @DefaultValue("-1") int after) {
>>>                 if (logEntry.available(after)) {
>>>                         /* return an immediate response */
>>>                 } else {
>>>                         /* Suspend on a Broadcaster until a response
>>> becomes available */
>>>                 }
>>>         }
>>>
>>> ...but I can't see a way to return either a responded response or an
>>> immediate one.
>>>
>>> 2. Even if this did work, I think there are race conditions up the
>>> wazoo, because a message could have gotten broadcast after the test
>>> but before the suspend.  This tells me that my approach here is wrong,
>>> but I haven't been able to find an example of a more correct way to do
>>> this.
>>>
>>> Would somebody point me to one (or paste if it's short enough)?
>>>
>>> Thanks,
>>>
>>> --Chris
>>