Advertisement
ImpariTuriddu

Atmosphere 0.7.1 and GWT 2.2 - Broadcasting

Oct 4th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. public class EventQueueManager implements Runnable {
  2.  
  3.     private volatile boolean isRunning;
  4.    
  5.     private final Logger logger;
  6.    
  7.     private final BroadcasterManager broadcasters;
  8.     //private final EventExecutorService eventService;
  9.    
  10.     public EventQueueManager (BroadcasterManager broadcasters) {
  11.         if (broadcasters == null)
  12.             throw new IllegalArgumentException ("The broadcaster storage cannot be null.");
  13.         this.broadcasters = broadcasters;
  14.         this.isRunning = true;
  15.         logger = LoggerFactory.getLogger(getClass());
  16.     }
  17.    
  18.     /* (non-Javadoc)
  19.      * @see java.lang.Runnable#run()
  20.      */
  21.     @Override
  22.     public void run() {
  23.         logger.debug("Plot Event Queue Manager STARTED.");
  24.         Broadcaster b;
  25.         Event event;
  26.         String id;
  27.         while (isRunning) {
  28.             event = EventQueue.getEvent();
  29.             if (event != null) {
  30.                 id = event.getDomain();
  31.                 b = broadcasters.get(id);
  32.                 if (b == null)
  33.                     //if the broadcaster does not exist, a STOP Subscription is sent
  34.                     SubscriptionEventQueue.addEvent(new SubscriptionEvent(Type.STOP,event.getDomain()));
  35.                 else if (b.getAtmosphereResources().size() == 0){
  36.                     //if the broadcaster haven't any resource, the broadcaster is removed.
  37.                     broadcasters.remove(id);
  38.                 }
  39.                 else {
  40.                     //otherwise use the retrieved broadcaster to broadcast the event
  41.                     b.broadcast(event);
  42.                     logger.debug("Broadcasting to " + b.getID() + ". " + event.toString());
  43.                 }
  44.             }
  45.         }
  46.         logger.debug("Event Queue Manager STOPPED.");
  47.     }
  48.    
  49.     public boolean isRunning () {
  50.         return this.isRunning;
  51.     }
  52.    
  53.     public void stop () {
  54.         this.isRunning = false;
  55.     }
  56. }
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement