Advertisement
ImpariTuriddu

Atmosphere 0.7.1 and GWT 2.2

Oct 4th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. public class MonitoringCometHandler extends AtmosphereGwtHandler {
  2.    
  3.     /**
  4.      * The size of the thread pool whose threads are used to execute the initialization of the
  5.      * JMS subscribers.
  6.      */
  7.     private static final String SUBSCRIBER_STARTER_THREAD_POOL_SIZE_INIT_PARAM =
  8.         "subscriberStarterThreadPoolSize";
  9.    
  10.     /**
  11.      * The initial size of the broadcaster collection used by Atmosphere to dispatch the events.
  12.      */
  13.     private static final String BROADCASTER_STORE_INITIAL_SIZE_INIT_PARAM = "broadcasterStoreInitSize";
  14.    
  15.     /**
  16.      * The initial size of the JMS subscriber collection.
  17.      */
  18.     private static final String SUBSCRIBER_STORE_INITIAL_SIZE_INIT_PARAM = "subscriberStoreInitSize";
  19.    
  20.     /**
  21.      * The size of the monitoring event queue.
  22.      */
  23.     private static final String MAX_EVENT_QUEUE_SIZE = "maxEventQueueSize";
  24.    
  25.     /**
  26.      * The size of the JMS subscription request queue.
  27.      */
  28.     private static final String MAX_JMS_EVENT_QUEUE_SIZE = "maxJMSEventQueueSize";
  29.    
  30.     private static final String SUBSCRIBER_USERNAME = "subscriberUsername";
  31.    
  32.     private static final String SUBSCRIBER_PASSWORD = "subscriberPassword";
  33.    
  34.     /*
  35.      * The number of milliseconds.
  36.      */
  37.     //private static final String TIMEOUT_INIT_PARAM = "timeout";
  38.    
  39.     /**
  40.      * The manager of the JMS subscription request queue.
  41.      */
  42.     private JMSSubscriptionEventQueueManager jmsEventQueueManager;
  43.    
  44.     /**
  45.      * The manager of the plot event queue.
  46.      */
  47.     private PlotEventQueueManager plotEventQueueManager;
  48.    
  49.     /**
  50.      * The broadcaster manager.
  51.      */
  52.     private BroadcasterManager broadcasterManager;
  53.    
  54.     private String subscriberUsername;
  55.    
  56.     private String subscriberPassword;
  57.    
  58.     private String subscriberApplicationName;
  59.  
  60.    
  61.     public void cometTerminated(GwtAtmosphereResource cometResponse,
  62.             boolean serverInitiated) {
  63.         /*
  64.          * This method is executed when all comet channels are closed.
  65.          */
  66.         super.cometTerminated(cometResponse, serverInitiated);
  67.         logger.debug("Comet terminated.");
  68.     }
  69.  
  70.     @Override
  71.     public void destroy() {
  72.         //I call the superclass destroy method after the stop of the threads executed using
  73.         //this thread pool.
  74.         super.destroy();
  75.        
  76.         //release the referenced instance
  77.         this.broadcasterManager = null;
  78.         logger.info("Monitoring Comet Handler destroyed!");
  79.     }
  80.  
  81.     @Override
  82.     public void disconnect(GwtAtmosphereResource resource) {
  83.         /*
  84.          * This method is executed when a client stops a comet connection by invoking
  85.          * AtmosphereClient.stop()
  86.          */
  87.         super.disconnect(resource);
  88.         //remove the current resource from the broadcaster
  89.         this.broadcasterManager.removeResource(resource.getAtmosphereResource());
  90.     }
  91.  
  92.     @Override
  93.     public int doComet(GwtAtmosphereResource resource) throws ServletException,
  94.             IOException {
  95.         /*
  96.          * This method is executed when a client starts a comet connection by invoking
  97.          * AtmosphereClient.start()
  98.          */
  99.         HttpServletRequest request = resource.getAtmosphereResource().getRequest();
  100.        
  101.         //Retrieve the name of the topic and set the broadcaster (NOT ONLY the id).
  102.         String topicName = this.registerSubscriber(request);
  103.         if (topicName != null) {
  104.             //**** BROADCASTER RETRIEVAL/CREATION
  105.            
  106.             logger.debug("Topic from request: " + topicName);
  107.             //check the existence of a broadcaster with the given ID (= topic name)
  108.             Broadcaster b = this.broadcasterManager.get(topicName);
  109.             if (b == null) {
  110.                 logger.debug("The broadcaster for the topic " + topicName + " doesn't exist. A new one " +
  111.                     "will be created.");
  112.                 //create the new broadcaster
  113.                 b = new MonitoringEventBroadcaster4Tomcat(topicName,this.broadcasterManager);
  114.                
  115.                 //set the name of the broadcaster otherwise
  116.                 //org.atmosphere.cpr.AtmosphereServlet$AtmosphereConfig.getAtmosphereHandler(
  117.                 //AtmosphereServlet.java:338) will throw a NPE
  118.                 resource.getBroadcaster().setID(topicName);
  119.                 resource.getAtmosphereResource().getBroadcaster().setID(topicName);
  120.                
  121.                 //add the current resource to the one managed by the newly created broadcaster
  122.                 b.addAtmosphereResource(resource.getAtmosphereResource());
  123.                
  124.                 //stores the newly created broadcaster
  125.                 broadcasterManager.add(topicName, b);
  126.             } else {
  127.                 //add the resource to the broadcaster
  128.                 resource.getBroadcaster().setID(topicName);
  129.                 b.addAtmosphereResource(resource.getAtmosphereResource());
  130.                 logger.debug("The current resource has been added to the broadcaster " + b.getID());
  131.             }
  132.         }
  133.         return NO_TIMEOUT;
  134.     }
  135.  
  136.     @Override
  137.     public void init(ServletConfig servletConfig) throws ServletException {
  138.         super.init(servletConfig);
  139.        
  140.         //initialize the broadcaster storage
  141.         this.broadcasterManager = new BroadcasterManagerImpl(Integer.parseInt(
  142.                 servletConfig.getInitParameter(BROADCASTER_STORE_INITIAL_SIZE_INIT_PARAM)));
  143.        
  144.         this.subscriberApplicationName = this.getServletContext().getInitParameter(
  145.             ContextParameters.SUBSCRIBER_APPLICATION_NAME);
  146.         this.subscriberPassword = servletConfig.getInitParameter(SUBSCRIBER_PASSWORD);
  147.         this.subscriberUsername = servletConfig.getInitParameter(SUBSCRIBER_USERNAME);
  148.     }
  149.    
  150.     private String registerSubscriber (HttpServletRequest request) {
  151.         String type = request.getParameter(SubscriptionRequestParameters.TYPE);
  152.         //other stuff omitted for clarity
  153.     }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement