@Override protected ServerNotificationManager createNotificationManager() { ServerNotificationManager manager = new ServerNotificationManager(); manager.addInterfaceToType(MuleContextNotificationListener.class, MuleContextNotification.class); manager.addInterfaceToType(SecurityNotificationListener.class, SecurityNotification.class); manager.addInterfaceToType(ManagementNotificationListener.class, ManagementNotification.class); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); manager.addInterfaceToType(ConnectionNotificationListener.class, ConnectionNotification.class); manager.addInterfaceToType(ExceptionNotificationListener.class, ExceptionNotification.class); manager.addInterfaceToType(ClusterNodeNotificationListener.class, ClusterNodeNotification.class); manager.addInterfaceToType(ExceptionNotificationListener.class, ExtensionNotification.class); return manager; } }
protected void registerDefaultEvents() throws ClassNotFoundException { manager.addInterfaceToType(Listener1.class, SubEvent1.class); manager.addInterfaceToType(Listener2.class, Event2.class); }
private void enableNotificationListeners(List<NotificationListener> notificationListeners) { NotificationListenerRegistry listenerRegistry = policyContext.getRegistry().lookupByType(NotificationListenerRegistry.class).get(); policyContext.getMuleContext().getNotificationManager().addInterfaceToType(PolicyNotificationListener.class, PolicyNotification.class); notificationListeners.forEach(listenerRegistry::registerListener); }
private void enableNotifications(ServerNotificationManager notificationManager, Map<String, Class<? extends Notification>> eventMap, Map<String, Class<? extends NotificationListener>> interfaceMap) throws InitialisationException { for (NotificationConfig<?, ?> notification : enabledNotifications) { final Supplier<InitialisationException> noNotificationExceptionSupplier = () -> new InitialisationException(createStaticMessage("No notification '%s' declared in this applications plugins to enable. Expected one of %s", notification.getEventName(), eventMap.keySet().toString()), this); notificationManager .addInterfaceToType(getInterfaceClass(notification, interfaceMap).orElseThrow(noNotificationExceptionSupplier), getEventClass(notification, eventMap).orElseThrow(noNotificationExceptionSupplier)); } }
@Test public void testSyncNotificationException() { ServerNotificationManager manager = new ServerNotificationManager(); manager.setMuleContext(muleContext); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); Notification notification = mock(CustomNotification.class); when(notification.isSynchronous()).thenReturn(true); NotificationListener notificationListener = mock(CustomNotificationListener.class); doThrow(new IllegalArgumentException()).when(notificationListener).onNotification(any(CustomNotification.class)); manager.addListener(notificationListener); manager.fireNotification(notification); verify(notificationListener, times(1)).onNotification(notification); }
@Test public void testAsyncNotificationRejectedExecution() { MuleContext muleContext = mock(MuleContext.class); SchedulerService schedulerService = mock(SchedulerService.class); Scheduler scheduler = mock(Scheduler.class); when(muleContext.getSchedulerService()).thenReturn(schedulerService); when(schedulerService.cpuLightScheduler()).thenReturn(scheduler); when(scheduler.submit(any(Callable.class))).thenThrow(new RejectedExecutionException()); Notification notification = mock(CustomNotification.class); when(notification.isSynchronous()).thenReturn(false); NotificationListener notificationListener = mock(CustomNotificationListener.class); ServerNotificationManager manager = new ServerNotificationManager(); manager.setMuleContext(muleContext); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); manager.addListener(notificationListener); manager.fireNotification(notification); verify(notificationListener, never()).onNotification(notification); }
private void setupMessageProcessorNotificationListener(List<MessageProcessorNotification> notificationList) { muleContext.getNotificationManager().addInterfaceToType(MessageProcessorNotificationListener.class, MessageProcessorNotification.class); muleContext.getNotificationManager().addListener((MessageProcessorNotificationListener) notification -> { notificationList.add((MessageProcessorNotification) notification); }); }
@Test public void testSyncNotificationError() { ServerNotificationManager manager = new ServerNotificationManager(); manager.setMuleContext(muleContext); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); Notification notification = mock(CustomNotification.class); when(notification.isSynchronous()).thenReturn(true); NotificationListener notificationListener = mock(CustomNotificationListener.class); doThrow(new LinkageError()).when(notificationListener).onNotification(any(CustomNotification.class)); manager.addListener(notificationListener); manager.fireNotification(notification); verify(notificationListener, times(1)).onNotification(notification); }
@Override protected ServerNotificationManager createNotificationManager() { ServerNotificationManager manager = new ServerNotificationManager(); manager.addInterfaceToType(MuleContextNotificationListener.class, MuleContextNotification.class); manager.addInterfaceToType(SecurityNotificationListener.class, SecurityNotification.class); manager.addInterfaceToType(ManagementNotificationListener.class, ManagementNotification.class); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); manager.addInterfaceToType(ConnectionNotificationListener.class, ConnectionNotification.class); manager.addInterfaceToType(ExceptionNotificationListener.class, ExceptionNotification.class); manager.addInterfaceToType(ClusterNodeNotificationListener.class, ClusterNodeNotification.class); manager.addInterfaceToType(ExceptionNotificationListener.class, ExtensionNotification.class); return manager; } }
protected void testAsyncCpuLightNotificationThreads(AtomicReference<Thread> beforeThread, AtomicReference<Thread> afterThread) throws Exception { muleContext.getNotificationManager().addInterfaceToType(MessageProcessorNotificationListener.class, MessageProcessorNotification.class); muleContext.getNotificationManager().addListener((MessageProcessorNotificationListener) notification -> { if (new IntegerAction(MESSAGE_PROCESSOR_PRE_INVOKE).equals(notification.getAction())) { beforeThread.set(currentThread()); } else if (new IntegerAction(MESSAGE_PROCESSOR_POST_INVOKE).equals(notification.getAction())) { afterThread.set(currentThread()); } }); flow = flowBuilder.get().processors(annotatedAsyncProcessor).build(); flow.initialise(); flow.start(); processFlow(testEvent()); }
protected void registerDefaultEvents() throws ClassNotFoundException { manager.addInterfaceToType(Listener1.class, SubEvent1.class); manager.addInterfaceToType(Listener2.class, Event2.class); }
private void enableNotificationListeners(List<NotificationListener> notificationListeners) { NotificationListenerRegistry listenerRegistry = policyContext.getRegistry().lookupByType(NotificationListenerRegistry.class).get(); policyContext.getMuleContext().getNotificationManager().addInterfaceToType(PolicyNotificationListener.class, PolicyNotification.class); notificationListeners.forEach(listenerRegistry::registerListener); }
private void enableNotifications(ServerNotificationManager notificationManager, Map<String, Class<? extends Notification>> eventMap, Map<String, Class<? extends NotificationListener>> interfaceMap) throws InitialisationException { for (NotificationConfig<?, ?> notification : enabledNotifications) { final Supplier<InitialisationException> noNotificationExceptionSupplier = () -> new InitialisationException(createStaticMessage("No notification '%s' declared in this applications plugins to enable. Expected one of %s", notification.getEventName(), eventMap.keySet().toString()), this); notificationManager .addInterfaceToType(getInterfaceClass(notification, interfaceMap).orElseThrow(noNotificationExceptionSupplier), getEventClass(notification, eventMap).orElseThrow(noNotificationExceptionSupplier)); } }
@Test public void testDynamicResponseToDisablingEvents() throws ClassNotFoundException { registerDefaultEvents(); registerDefaultListeners(); assertStandardEventsEnabled(); // now disable event 2 manager.disableType(Event2.class); assertTrue("via subclass", manager.isNotificationEnabled(Event1.class)); assertTrue("direct", manager.isNotificationEnabled(SubEvent1.class)); assertFalse("disabled", manager.isNotificationEnabled(Event2.class)); assertFalse("no listener", manager.isNotificationEnabled(SubEvent2.class)); assertFalse("not specified at all", manager.isNotificationEnabled(Event3.class)); // the subclass should be blocked too manager.addInterfaceToType(Listener2.class, SubEvent2.class); assertTrue("via subclass", manager.isNotificationEnabled(Event1.class)); assertTrue("direct", manager.isNotificationEnabled(SubEvent1.class)); assertFalse("disabled", manager.isNotificationEnabled(Event2.class)); assertFalse("disabled", manager.isNotificationEnabled(SubEvent2.class)); assertFalse("not specified at all", manager.isNotificationEnabled(Event3.class)); }
@Test public void testSyncNotificationError() { ServerNotificationManager manager = new ServerNotificationManager(); manager.setMuleContext(muleContext); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); Notification notification = mock(CustomNotification.class); when(notification.isSynchronous()).thenReturn(true); NotificationListener notificationListener = mock(CustomNotificationListener.class); doThrow(new LinkageError()).when(notificationListener).onNotification(any(CustomNotification.class)); manager.addListener(notificationListener); manager.fireNotification(notification); verify(notificationListener, times(1)).onNotification(notification); }
@Test public void testSyncNotificationException() { ServerNotificationManager manager = new ServerNotificationManager(); manager.setMuleContext(muleContext); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); Notification notification = mock(CustomNotification.class); when(notification.isSynchronous()).thenReturn(true); NotificationListener notificationListener = mock(CustomNotificationListener.class); doThrow(new IllegalArgumentException()).when(notificationListener).onNotification(any(CustomNotification.class)); manager.addListener(notificationListener); manager.fireNotification(notification); verify(notificationListener, times(1)).onNotification(notification); }
private void setupMessageProcessorNotificationListener(List<MessageProcessorNotification> notificationList) { muleContext.getNotificationManager().addInterfaceToType(MessageProcessorNotificationListener.class, MessageProcessorNotification.class); muleContext.getNotificationManager().addListener((MessageProcessorNotificationListener) notification -> { notificationList.add((MessageProcessorNotification) notification); }); }
@Test public void testAsyncNotificationRejectedExecution() { MuleContext muleContext = mock(MuleContext.class); SchedulerService schedulerService = mock(SchedulerService.class); Scheduler scheduler = mock(Scheduler.class); when(muleContext.getSchedulerService()).thenReturn(schedulerService); when(schedulerService.cpuLightScheduler()).thenReturn(scheduler); when(scheduler.submit(any(Callable.class))).thenThrow(new RejectedExecutionException()); Notification notification = mock(CustomNotification.class); when(notification.isSynchronous()).thenReturn(false); NotificationListener notificationListener = mock(CustomNotificationListener.class); ServerNotificationManager manager = new ServerNotificationManager(); manager.setMuleContext(muleContext); manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); manager.addListener(notificationListener); manager.fireNotification(notification); verify(notificationListener, never()).onNotification(notification); }
protected void testAsyncCpuLightNotificationThreads(AtomicReference<Thread> beforeThread, AtomicReference<Thread> afterThread) throws Exception { muleContext.getNotificationManager().addInterfaceToType(MessageProcessorNotificationListener.class, MessageProcessorNotification.class); muleContext.getNotificationManager().addListener((MessageProcessorNotificationListener) notification -> { if (new IntegerAction(MESSAGE_PROCESSOR_PRE_INVOKE).equals(notification.getAction())) { beforeThread.set(currentThread()); } else if (new IntegerAction(MESSAGE_PROCESSOR_POST_INVOKE).equals(notification.getAction())) { afterThread.set(currentThread()); } }); flow = flowBuilder.get().processors(annotatedAsyncProcessor).build(); flow.initialise(); flow.start(); processFlow(testEvent()); }
@Test public void testDynamicResponseToDisablingEvents() throws ClassNotFoundException { registerDefaultEvents(); registerDefaultListeners(); assertStandardEventsEnabled(); // now disable event 2 manager.disableType(Event2.class); assertTrue("via subclass", manager.isNotificationEnabled(Event1.class)); assertTrue("direct", manager.isNotificationEnabled(SubEvent1.class)); assertFalse("disabled", manager.isNotificationEnabled(Event2.class)); assertFalse("no listener", manager.isNotificationEnabled(SubEvent2.class)); assertFalse("not specified at all", manager.isNotificationEnabled(Event3.class)); // the subclass should be blocked too manager.addInterfaceToType(Listener2.class, SubEvent2.class); assertTrue("via subclass", manager.isNotificationEnabled(Event1.class)); assertTrue("direct", manager.isNotificationEnabled(SubEvent1.class)); assertFalse("disabled", manager.isNotificationEnabled(Event2.class)); assertFalse("disabled", manager.isNotificationEnabled(SubEvent2.class)); assertFalse("not specified at all", manager.isNotificationEnabled(Event3.class)); }