package org.realityforge.gwt.packetio.example.server;

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import org.glassfish.jersey.media.sse.EventOutput;
import org.glassfish.jersey.media.sse.SseBroadcaster;

@Path("/time")
@Singleton
public class TimeResource
{
  private final SseBroadcaster _broadcaster = new SseBroadcaster();
  private final Collection<AsyncResponse> _waiters = new ConcurrentLinkedQueue<>();
  private final ScheduledExecutorService _scheduledExecutor = Executors.newSingleThreadScheduledExecutor();

  @PostConstruct
  public void postConstruct()
  {
    _scheduledExecutor.scheduleWithFixedDelay( new TimeGenerator( _broadcaster, _waiters ), 0, 1, TimeUnit.SECONDS );
  }

  @GET
  @Produces("text/event-stream")
  public EventOutput getMessages()
  {
    final EventOutput eventOutput = new EventOutput();
    _broadcaster.add( eventOutput );
    return eventOutput;
  }

  @Path("long_poll")
  @GET
  @Produces("text/plain")
  public void hangUp( @Suspended AsyncResponse response )
  {
    _waiters.add( response );
  }
}

@@ -73,24 +73,47 @@
 Collection;%0A
+import java.util.Date;%0A
 import java.
@@ -151,16 +151,16 @@
 dQueue;%0A
-
 import j
@@ -930,16 +930,43 @@
 cutor();
+%0A  private String _message;
 %0A%0A  @Pos
@@ -1018,94 +1018,286 @@
     
-_scheduledExecutor.scheduleWithFixedDelay( new TimeGenerator( _broadcaster, _waiters )
+final TimeGenerator command = new TimeGenerator( _broadcaster, _waiters );%0A    _scheduledExecutor.scheduleWithFixedDelay( new Runnable()%0A    %7B%0A      @Override%0A      public void run()%0A      %7B%0A        command.run();%0A        _message = %22p: The time is %22 + new Date();%0A      %7D%0A    %7D
 , 0,
@@ -1637,24 +1637,24 @@
 ponse )%0A  %7B%0A
-
     _waiters
@@ -1674,11 +1674,170 @@
 e );%0A  %7D
+%0A%0A  @Path(%22poll%22)%0A  @GET%0A  @Produces(%22text/plain%22)%0A  public String poll()%0A  %7B%0A    final String message = _message;%0A    _message = null;%0A    return message;%0A  %7D
 %0A%7D%0A

Add a pol endpoint