public static MessageDispatcher getDispatcher(Object targetObject) { MessageDispatcher dispatcher; synchronized (s_targetMap) { dispatcher = s_targetMap.get(targetObject); if (dispatcher == null) { dispatcher = new MessageDispatcher(targetObject); s_targetMap.put(targetObject, dispatcher); } } return dispatcher; }
public MessageDispatcher(Object targetObject) { _targetObject = targetObject; buildHandlerMethodCache(targetObject.getClass()); }
@Override public void onPublishMessage(String senderAddress, String subject, Object args) { dispatch(_targetObject, subject, senderAddress, args); }
@Override public void subscribe() { /* subscribe to DB events */ _messageBus.subscribe(EventTypes.EVENT_PROJECT_CREATE, MessageDispatcher.getDispatcher(this)); _messageBus.subscribe(EventTypes.EVENT_PROJECT_DELETE, MessageDispatcher.getDispatcher(this)); _messageBus.subscribe(EventTypes.EVENT_DOMAIN_CREATE, MessageDispatcher.getDispatcher(this)); _messageBus.subscribe(EventTypes.EVENT_DOMAIN_DELETE, MessageDispatcher.getDispatcher(this)); } }
public static Method resolveHandler(Class<?> handlerClz, String subject) { synchronized (s_handlerCache) { List<Method> handlerList = s_handlerCache.get(handlerClz); if (handlerList != null) { for (Method method : handlerList) { MessageHandler annotation = method.getAnnotation(MessageHandler.class); assert (annotation != null); if (match(annotation.topic(), subject)) { return method; } } } else { s_logger.error("Handler class " + handlerClz.getName() + " is not registered"); } } return null; }
public static boolean dispatch(Object target, String subject, String senderAddress, Object args) { assert (subject != null); assert (target != null); Method handler = resolveHandler(target.getClass(), subject); if (handler == null) return false; try { handler.invoke(target, subject, senderAddress, args); } catch (IllegalArgumentException e) { s_logger.error("Unexpected exception when calling " + target.getClass().getName() + "." + handler.getName(), e); throw new RuntimeException("IllegalArgumentException when invoking event handler for subject: " + subject); } catch (IllegalAccessException e) { s_logger.error("Unexpected exception when calling " + target.getClass().getName() + "." + handler.getName(), e); throw new RuntimeException("IllegalAccessException when invoking event handler for subject: " + subject); } catch (InvocationTargetException e) { s_logger.error("Unexpected exception when calling " + target.getClass().getName() + "." + handler.getName(), e); throw new RuntimeException("InvocationTargetException when invoking event handler for subject: " + subject); } return true; }
@Override public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException { messageBus.subscribe(AsyncJob.Topics.JOB_EVENT_PUBLISH, MessageDispatcher.getDispatcher(this)); return true; }
@Override public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { _messageBus.subscribe(AsyncJob.Topics.JOB_HEARTBEAT, MessageDispatcher.getDispatcher(this)); _timer.scheduleAtFixedRate(new ManagedContextTimerTask() { @Override protected void runInContext() { heartbeat(); } }, _inactivityCheckIntervalMs, _inactivityCheckIntervalMs); return true; }
@Override public boolean configure(final String name, final Map<String, Object> xmlParams) throws ConfigurationException { ReservationContextImpl.init(_entityMgr); VirtualMachineProfileImpl.init(_entityMgr); VmWorkMigrate.init(_entityMgr); _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Vm-Operations-Cleanup")); _nodeId = ManagementServerNode.getManagementServerId(); _agentMgr.registerForHostEvents(this, true, true, true); _messageBus.subscribe(VirtualMachineManager.Topics.VM_POWER_STATE, MessageDispatcher.getDispatcher(this)); return true; }