Java Reference
In-Depth Information
for
for ( AsyncResponse async : listeners )
{
try
try
{
send ( base , async , message );
}
catch
catch ( Exception e )
{
e . printStackTrace ();
}
}
listeners . clear ();
}
}
});
}
Finally, the writer thread loops through all waiting chat clients and sends them the new mes-
sage.
Handling poll requests
The CustomerChat.receive() method handles GET requests from chat clients:
@GET
public
public void
void receive ( @QueryParam ( "current" ) String id ,
@Suspended AsyncResponse async )
{
final
final UriBuilder base = uriInfo . getBaseUriBuilder ();
Message message = null
null ;
synchronized
synchronized ( messages )
{
Message current = messages . get ( id );
iif ( current == null
null ) message = first ;
else
else message = current . next ;
iif ( message == null
null ) {
queue ( async );
}
}
// do this outside of synchronized block to reduce lock hold time
iif ( message != null
null ) send ( base , async , message );
}
The receive() method takes a query parameter, current . This parameter is the id of the
last message the chat client read. This parameter is allowed to be null if this is the chat cli-
Search WWH ::




Custom Search