Search in sources :

Example 1 with NotificationListener

use of org.mule.runtime.api.notification.NotificationListener in project mule by mulesoft.

the class ServerNotificationsTestCase method testSyncNotificationException.

@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);
}
Also used : CustomNotification(org.mule.runtime.api.notification.CustomNotification) Notification(org.mule.runtime.api.notification.Notification) CustomNotification(org.mule.runtime.api.notification.CustomNotification) CustomNotificationListener(org.mule.runtime.api.notification.CustomNotificationListener) NotificationListener(org.mule.runtime.api.notification.NotificationListener) Test(org.junit.Test)

Example 2 with NotificationListener

use of org.mule.runtime.api.notification.NotificationListener in project mule by mulesoft.

the class ServerNotificationsTestCase method testSyncNotificationError.

@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);
}
Also used : CustomNotification(org.mule.runtime.api.notification.CustomNotification) Notification(org.mule.runtime.api.notification.Notification) CustomNotification(org.mule.runtime.api.notification.CustomNotification) CustomNotificationListener(org.mule.runtime.api.notification.CustomNotificationListener) NotificationListener(org.mule.runtime.api.notification.NotificationListener) Test(org.junit.Test)

Example 3 with NotificationListener

use of org.mule.runtime.api.notification.NotificationListener in project mule by mulesoft.

the class ServerNotificationManagerConfigurator method getMergedListeners.

protected Set<ListenerSubscriptionPair> getMergedListeners(ServerNotificationManager notificationManager) {
    Set<ListenerSubscriptionPair> mergedListeners = new HashSet<>();
    // Any singleton bean defined in spring that implements
    // NotificationListener or a subclass.
    Set<ListenerSubscriptionPair> adhocListeners = new HashSet<>();
    for (String name : applicationContext.getBeanNamesForType(NotificationListener.class, false, true)) {
        adhocListeners.add(new ListenerSubscriptionPair((NotificationListener<?>) applicationContext.getBean(name)));
    }
    if (notificationListeners != null) {
        mergedListeners.addAll(notificationListeners);
        for (ListenerSubscriptionPair candidate : adhocListeners) {
            boolean explicityDefined = false;
            for (ListenerSubscriptionPair explicitListener : notificationListeners) {
                if (candidate.getListener().equals(explicitListener.getListener())) {
                    explicityDefined = true;
                    break;
                }
            }
            if (!explicityDefined) {
                mergedListeners.add(candidate);
            }
        }
    } else {
        mergedListeners.addAll(adhocListeners);
    }
    return mergedListeners;
}
Also used : ListenerSubscriptionPair(org.mule.runtime.core.api.context.notification.ListenerSubscriptionPair) HashSet(java.util.HashSet) NotificationListener(org.mule.runtime.api.notification.NotificationListener)

Example 4 with NotificationListener

use of org.mule.runtime.api.notification.NotificationListener in project mule by mulesoft.

the class ServerNotificationManagerConfigurator method initialise.

@Override
public void initialise() throws InitialisationException {
    Map<String, Class<? extends Notification>> eventMap = new HashMap<>(EVENT_MAP);
    Map<String, Class<? extends NotificationListener>> interfaceMap = new HashMap<>(INTERFACE_MAP);
    ServerNotificationManager notificationManager = populateNotificationTypeMappings(eventMap, interfaceMap);
    enableNotifications(notificationManager, eventMap, interfaceMap);
    disableNotifications(notificationManager, eventMap, interfaceMap);
    // ii) any singleton beans defined in spring that implement NotificationListener.
    for (ListenerSubscriptionPair sub : getMergedListeners(notificationManager)) {
        // Do this to avoid warnings when the Spring context is refreshed
        if (!notificationManager.isListenerRegistered(sub.getListener())) {
            notificationManager.addListenerSubscriptionPair(sub);
        } else {
            notificationManager.removeListener(sub.getListener());
            notificationManager.addListenerSubscriptionPair(sub);
        }
    }
}
Also used : HashMap(java.util.HashMap) ListenerSubscriptionPair(org.mule.runtime.core.api.context.notification.ListenerSubscriptionPair) Notification(org.mule.runtime.api.notification.Notification) ServerNotificationManager(org.mule.runtime.core.api.context.notification.ServerNotificationManager) NotificationListener(org.mule.runtime.api.notification.NotificationListener)

Example 5 with NotificationListener

use of org.mule.runtime.api.notification.NotificationListener in project mule by mulesoft.

the class DefaultMuleContextBuilderTestCase method notificationManagerContainsTheCorrectInterfaces.

/**
 * <p>
 * After the mule context is built it has to contain the proper notification interfaces in the notification manager
 * </p>
 */
@Test
public void notificationManagerContainsTheCorrectInterfaces() {
    DefaultMuleContextBuilder builder = new DefaultMuleContextBuilder(APP);
    builder.setMuleConfiguration(new MyMuleConfiguration());
    builder.setLifecycleManager(new MyLifeCycleManager());
    MuleContext muleContext = builder.buildMuleContext();
    Map<Class<? extends NotificationListener>, Set<Class<? extends Notification>>> interfaces = muleContext.getNotificationManager().getInterfaceToTypes();
    assertEquals(MuleContextNotification.class, interfaces.get(MuleContextNotificationListener.class).toArray()[0]);
    assertEquals(RoutingNotification.class, interfaces.get(RoutingNotificationListener.class).toArray()[0]);
    assertEquals(SecurityNotification.class, interfaces.get(SecurityNotificationListener.class).toArray()[0]);
    assertEquals(ManagementNotification.class, interfaces.get(ManagementNotificationListener.class).toArray()[0]);
    assertEquals(CustomNotification.class, interfaces.get(CustomNotificationListener.class).toArray()[0]);
    assertEquals(ConnectionNotification.class, interfaces.get(ConnectionNotificationListener.class).toArray()[0]);
    assertEquals(ExceptionNotification.class, interfaces.get(ExceptionNotificationListener.class).toArray()[0]);
    assertEquals(TransactionNotification.class, interfaces.get(TransactionNotificationListener.class).toArray()[0]);
    assertEquals(PipelineMessageNotification.class, interfaces.get(PipelineMessageNotificationListener.class).toArray()[0]);
    assertEquals(AsyncMessageNotification.class, interfaces.get(AsyncMessageNotificationListener.class).toArray()[0]);
    assertEquals(ClusterNodeNotification.class, interfaces.get(ClusterNodeNotificationListener.class).toArray()[0]);
}
Also used : ClusterNodeNotificationListener(org.mule.runtime.api.notification.ClusterNodeNotificationListener) Set(java.util.Set) CustomNotificationListener(org.mule.runtime.api.notification.CustomNotificationListener) TransactionNotificationListener(org.mule.runtime.api.notification.TransactionNotificationListener) MuleContext(org.mule.runtime.core.api.MuleContext) SecurityNotificationListener(org.mule.runtime.api.notification.SecurityNotificationListener) ExceptionNotificationListener(org.mule.runtime.api.notification.ExceptionNotificationListener) ConnectionNotificationListener(org.mule.runtime.api.notification.ConnectionNotificationListener) RoutingNotificationListener(org.mule.runtime.api.notification.RoutingNotificationListener) MuleContextNotificationListener(org.mule.runtime.core.api.context.notification.MuleContextNotificationListener) AsyncMessageNotificationListener(org.mule.runtime.api.notification.AsyncMessageNotificationListener) ManagementNotificationListener(org.mule.runtime.api.notification.ManagementNotificationListener) PipelineMessageNotificationListener(org.mule.runtime.api.notification.PipelineMessageNotificationListener) CustomNotificationListener(org.mule.runtime.api.notification.CustomNotificationListener) MuleContextNotificationListener(org.mule.runtime.core.api.context.notification.MuleContextNotificationListener) PipelineMessageNotificationListener(org.mule.runtime.api.notification.PipelineMessageNotificationListener) SecurityNotificationListener(org.mule.runtime.api.notification.SecurityNotificationListener) ClusterNodeNotificationListener(org.mule.runtime.api.notification.ClusterNodeNotificationListener) AsyncMessageNotificationListener(org.mule.runtime.api.notification.AsyncMessageNotificationListener) ConnectionNotificationListener(org.mule.runtime.api.notification.ConnectionNotificationListener) ManagementNotificationListener(org.mule.runtime.api.notification.ManagementNotificationListener) ExceptionNotificationListener(org.mule.runtime.api.notification.ExceptionNotificationListener) NotificationListener(org.mule.runtime.api.notification.NotificationListener) TransactionNotificationListener(org.mule.runtime.api.notification.TransactionNotificationListener) RoutingNotificationListener(org.mule.runtime.api.notification.RoutingNotificationListener) Test(org.junit.Test)

Aggregations

NotificationListener (org.mule.runtime.api.notification.NotificationListener)6 Test (org.junit.Test)4 CustomNotificationListener (org.mule.runtime.api.notification.CustomNotificationListener)4 Notification (org.mule.runtime.api.notification.Notification)4 CustomNotification (org.mule.runtime.api.notification.CustomNotification)3 MuleContext (org.mule.runtime.core.api.MuleContext)2 ListenerSubscriptionPair (org.mule.runtime.core.api.context.notification.ListenerSubscriptionPair)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AsyncMessageNotificationListener (org.mule.runtime.api.notification.AsyncMessageNotificationListener)1 ClusterNodeNotificationListener (org.mule.runtime.api.notification.ClusterNodeNotificationListener)1 ConnectionNotificationListener (org.mule.runtime.api.notification.ConnectionNotificationListener)1 ExceptionNotificationListener (org.mule.runtime.api.notification.ExceptionNotificationListener)1 ManagementNotificationListener (org.mule.runtime.api.notification.ManagementNotificationListener)1 PipelineMessageNotificationListener (org.mule.runtime.api.notification.PipelineMessageNotificationListener)1 RoutingNotificationListener (org.mule.runtime.api.notification.RoutingNotificationListener)1 SecurityNotificationListener (org.mule.runtime.api.notification.SecurityNotificationListener)1