Advertisement
Guest User

event bus subscriber

a guest
Dec 9th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package com.cloud.eventsubscriber;
  2.  
  3.  
  4. import com.cloud.server.ManagementServer;
  5. import com.cloud.utils.component.Adapters;
  6. import com.cloud.utils.component.ComponentLocator;
  7. import com.cloud.utils.component.Manager;
  8. import org.apache.cloudstack.framework.events.Event;
  9. import org.apache.cloudstack.framework.events.EventBus;
  10. import org.apache.cloudstack.framework.events.EventSubscriber;
  11. import org.apache.cloudstack.framework.events.EventTopic;
  12. import org.apache.log4j.Logger;
  13.  
  14. import javax.ejb.Local;
  15. import javax.naming.ConfigurationException;
  16. import java.util.Enumeration;
  17. import java.util.Map;
  18. import java.util.UUID;
  19.  
  20. @Local(value = {EventNotificationsSubscriber.class})
  21. public class EventNotificationSubscriberImpl implements EventNotificationsSubscriber, Manager {
  22.  
  23.     protected static EventBus _eventBus = null;
  24.     protected static boolean _eventBusLoaded = false;
  25.     private static final Logger s_logger = Logger.getLogger(EventNotificationsSubscriber.class);
  26.  
  27.     UUID one;
  28.  
  29.     @Override
  30.     public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
  31.         _eventBus = getEventBusProvider();
  32.         return true;
  33.     }
  34.  
  35.     @Override
  36.     public boolean start() {
  37.         EventTopic topic = new EventTopic(null, null, null, null, null);
  38.         try {
  39.             if (_eventBus != null) {
  40.                 one = _eventBus.subscribe(topic, new EventNotificationHandler(1));
  41.             }
  42.         } catch (Exception e) {
  43.  
  44.         }
  45.         return true;
  46.     }
  47.  
  48.     @Override
  49.     public boolean stop() {
  50.         return true;
  51.     }
  52.  
  53.     @Override
  54.     public String getName() {
  55.         return null;
  56.     }
  57.  
  58.     private static EventBus getEventBusProvider() {
  59.  
  60.         ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
  61.         Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
  62.         if (eventBusImpls != null) {
  63.             Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
  64.             if (eventBusenum != null && eventBusenum.hasMoreElements()) {
  65.                 _eventBus = eventBusenum.nextElement();
  66.             }
  67.         }
  68.  
  69.         return _eventBus;
  70.     }
  71.  
  72.     public class EventNotificationHandler implements EventSubscriber {
  73.         int id;
  74.         public EventNotificationHandler(int id) {
  75.             this.id = id;
  76.         }
  77.         @Override
  78.         public void onEvent(Event event) {
  79.             s_logger.info("HANDLER" + id + " Category: " + event.getEventCategory() + " type: " + event.getEventType() +
  80.                     " resource type: " + event.getResourceType() + " resource UUID: " + event.getResourceUUID());
  81.             s_logger.info("BODY : " + event.getDescription());
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement