Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MonitoringCometHandler extends AtmosphereGwtHandler {
- /**
- * The size of the thread pool whose threads are used to execute the initialization of the
- * JMS subscribers.
- */
- private static final String SUBSCRIBER_STARTER_THREAD_POOL_SIZE_INIT_PARAM =
- "subscriberStarterThreadPoolSize";
- /**
- * The initial size of the broadcaster collection used by Atmosphere to dispatch the events.
- */
- private static final String BROADCASTER_STORE_INITIAL_SIZE_INIT_PARAM = "broadcasterStoreInitSize";
- /**
- * The initial size of the JMS subscriber collection.
- */
- private static final String SUBSCRIBER_STORE_INITIAL_SIZE_INIT_PARAM = "subscriberStoreInitSize";
- /**
- * The size of the monitoring event queue.
- */
- private static final String MAX_EVENT_QUEUE_SIZE = "maxEventQueueSize";
- /**
- * The size of the JMS subscription request queue.
- */
- private static final String MAX_JMS_EVENT_QUEUE_SIZE = "maxJMSEventQueueSize";
- private static final String SUBSCRIBER_USERNAME = "subscriberUsername";
- private static final String SUBSCRIBER_PASSWORD = "subscriberPassword";
- /*
- * The number of milliseconds.
- */
- //private static final String TIMEOUT_INIT_PARAM = "timeout";
- /**
- * The manager of the JMS subscription request queue.
- */
- private JMSSubscriptionEventQueueManager jmsEventQueueManager;
- /**
- * The manager of the plot event queue.
- */
- private PlotEventQueueManager plotEventQueueManager;
- /**
- * The broadcaster manager.
- */
- private BroadcasterManager broadcasterManager;
- private String subscriberUsername;
- private String subscriberPassword;
- private String subscriberApplicationName;
- public void cometTerminated(GwtAtmosphereResource cometResponse,
- boolean serverInitiated) {
- /*
- * This method is executed when all comet channels are closed.
- */
- super.cometTerminated(cometResponse, serverInitiated);
- logger.debug("Comet terminated.");
- }
- @Override
- public void destroy() {
- //I call the superclass destroy method after the stop of the threads executed using
- //this thread pool.
- super.destroy();
- //release the referenced instance
- this.broadcasterManager = null;
- logger.info("Monitoring Comet Handler destroyed!");
- }
- @Override
- public void disconnect(GwtAtmosphereResource resource) {
- /*
- * This method is executed when a client stops a comet connection by invoking
- * AtmosphereClient.stop()
- */
- super.disconnect(resource);
- //remove the current resource from the broadcaster
- this.broadcasterManager.removeResource(resource.getAtmosphereResource());
- }
- @Override
- public int doComet(GwtAtmosphereResource resource) throws ServletException,
- IOException {
- /*
- * This method is executed when a client starts a comet connection by invoking
- * AtmosphereClient.start()
- */
- HttpServletRequest request = resource.getAtmosphereResource().getRequest();
- //Retrieve the name of the topic and set the broadcaster (NOT ONLY the id).
- String topicName = this.registerSubscriber(request);
- if (topicName != null) {
- //**** BROADCASTER RETRIEVAL/CREATION
- logger.debug("Topic from request: " + topicName);
- //check the existence of a broadcaster with the given ID (= topic name)
- Broadcaster b = this.broadcasterManager.get(topicName);
- if (b == null) {
- logger.debug("The broadcaster for the topic " + topicName + " doesn't exist. A new one " +
- "will be created.");
- //create the new broadcaster
- b = new MonitoringEventBroadcaster4Tomcat(topicName,this.broadcasterManager);
- //set the name of the broadcaster otherwise
- //org.atmosphere.cpr.AtmosphereServlet$AtmosphereConfig.getAtmosphereHandler(
- //AtmosphereServlet.java:338) will throw a NPE
- resource.getBroadcaster().setID(topicName);
- resource.getAtmosphereResource().getBroadcaster().setID(topicName);
- //add the current resource to the one managed by the newly created broadcaster
- b.addAtmosphereResource(resource.getAtmosphereResource());
- //stores the newly created broadcaster
- broadcasterManager.add(topicName, b);
- } else {
- //add the resource to the broadcaster
- resource.getBroadcaster().setID(topicName);
- b.addAtmosphereResource(resource.getAtmosphereResource());
- logger.debug("The current resource has been added to the broadcaster " + b.getID());
- }
- }
- return NO_TIMEOUT;
- }
- @Override
- public void init(ServletConfig servletConfig) throws ServletException {
- super.init(servletConfig);
- //initialize the broadcaster storage
- this.broadcasterManager = new BroadcasterManagerImpl(Integer.parseInt(
- servletConfig.getInitParameter(BROADCASTER_STORE_INITIAL_SIZE_INIT_PARAM)));
- this.subscriberApplicationName = this.getServletContext().getInitParameter(
- ContextParameters.SUBSCRIBER_APPLICATION_NAME);
- this.subscriberPassword = servletConfig.getInitParameter(SUBSCRIBER_PASSWORD);
- this.subscriberUsername = servletConfig.getInitParameter(SUBSCRIBER_USERNAME);
- }
- private String registerSubscriber (HttpServletRequest request) {
- String type = request.getParameter(SubscriptionRequestParameters.TYPE);
- //other stuff omitted for clarity
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment