Search in sources :

Example 16 with Notification

use of javax.management.Notification in project camel by apache.

the class JMXNotificationFilterTest method initRegistry.

@Override
protected void initRegistry() {
    super.initRegistry();
    // initialize the registry with our filter
    getRegistry().put("myFilter", new NotificationFilter() {

        private static final long serialVersionUID = 1L;

        public boolean isNotificationEnabled(Notification aNotification) {
            // only accept even notifications
            boolean enabled = aNotification.getSequenceNumber() % 2 == 0;
            if (!enabled) {
                mRejected.add(aNotification);
            }
            return enabled;
        }
    });
}
Also used : NotificationFilter(javax.management.NotificationFilter) Notification(javax.management.Notification)

Example 17 with Notification

use of javax.management.Notification in project camel by apache.

the class JMXNotificationFilterTest method testNotificationFilter.

@Test
public void testNotificationFilter() throws Exception {
    ISimpleMXBean bean = getSimpleMXBean();
    assertEquals("no notifications should have been filtered at this point", 0, mRejected.size());
    // we should only get 5 messages, which is 1/2 the number of times we touched the object.
    // The 1/2 is due to the behavior of the test NotificationFilter implemented below 
    getMockFixture().getMockEndpoint().setExpectedMessageCount(5);
    for (int i = 0; i < 10; i++) {
        bean.touch();
    }
    getMockFixture().waitForMessages();
    assertEquals("5 notifications should have been filtered", 5, mRejected.size());
    // assert that all of the rejected ones are odd and accepted ones even
    for (Notification rejected : mRejected) {
        assertEquals(1, rejected.getSequenceNumber() % 2);
    }
    for (Exchange received : getMockFixture().getMockEndpoint().getReceivedExchanges()) {
        Notification n = (Notification) received.getIn().getBody();
        assertEquals(0, n.getSequenceNumber() % 2);
    }
}
Also used : Exchange(org.apache.camel.Exchange) Notification(javax.management.Notification) ISimpleMXBean(org.apache.camel.component.jmx.beans.ISimpleMXBean) Test(org.junit.Test)

Example 18 with Notification

use of javax.management.Notification in project camel by apache.

the class JmxNotificationEventNotifierTest method testExchangeDone.

public void testExchangeDone() throws Exception {
    // START SNIPPET: e2
    // register the NotificationListener
    ObjectName on = ObjectName.getInstance("org.apache.camel:context=camel-1,type=eventnotifiers,name=JmxEventNotifier");
    MyNotificationListener listener = new MyNotificationListener();
    context.getManagementStrategy().getManagementAgent().getMBeanServer().addNotificationListener(on, listener, new NotificationFilter() {

        private static final long serialVersionUID = 1L;

        public boolean isNotificationEnabled(Notification notification) {
            return notification.getSource().equals("MyCamel");
        }
    }, null);
    // END SNIPPET: e2
    getMockEndpoint("mock:result").expectedMessageCount(1);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    assertEquals("Get a wrong number of events", 8, listener.getEventCounter());
    context.stop();
}
Also used : NotificationFilter(javax.management.NotificationFilter) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName)

Example 19 with Notification

use of javax.management.Notification in project camel by apache.

the class JmxNotificationEventNotifierTest method testExchangeFailed.

public void testExchangeFailed() throws Exception {
    ObjectName on = ObjectName.getInstance("org.apache.camel:context=camel-1,type=eventnotifiers,name=JmxEventNotifier");
    MyNotificationListener listener = new MyNotificationListener();
    context.getManagementStrategy().getManagementAgent().getMBeanServer().addNotificationListener(on, listener, new NotificationFilter() {

        private static final long serialVersionUID = 1L;

        public boolean isNotificationEnabled(Notification notification) {
            return true;
        }
    }, null);
    try {
        template.sendBody("direct:fail", "Hello World");
        fail("Should have thrown an exception");
    } catch (Exception e) {
        // expected
        assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
    }
    assertEquals("Get a wrong number of events", 4, listener.getEventCounter());
    context.stop();
}
Also used : NotificationFilter(javax.management.NotificationFilter) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName)

Example 20 with Notification

use of javax.management.Notification in project camel by apache.

the class JmxNotificationEventNotifier method notify.

public void notify(EventObject event) throws Exception {
    if (notificationBroadcaster != null) {
        // its recommended to send light weight events and we don't want to have the entire Exchange/CamelContext etc
        // serialized as these are the typical source of the EventObject. So we use our own source which is just
        // a human readable name, which can be configured.
        String type = event.getClass().getSimpleName();
        String message = event.toString();
        Notification notification = new Notification(type, source, counter.getAndIncrement(), message);
        LOG.trace("Broadcasting JMX notification: {}", notification);
        notificationBroadcaster.sendNotification(notification);
    }
}
Also used : Notification(javax.management.Notification)

Aggregations

Notification (javax.management.Notification)204 ObjectName (javax.management.ObjectName)68 NotificationListener (javax.management.NotificationListener)35 AttributeChangeNotification (javax.management.AttributeChangeNotification)29 CompositeData (javax.management.openmbean.CompositeData)25 IOException (java.io.IOException)23 Test (org.junit.Test)20 HashMap (java.util.HashMap)19 MalformedObjectNameException (javax.management.MalformedObjectNameException)19 Test (org.testng.annotations.Test)18 ListenerNotFoundException (javax.management.ListenerNotFoundException)17 ArrayList (java.util.ArrayList)16 MBeanServer (javax.management.MBeanServer)16 NotificationFilter (javax.management.NotificationFilter)15 NotificationEmitter (javax.management.NotificationEmitter)14 JMXConnector (javax.management.remote.JMXConnector)14 JMXServiceURL (javax.management.remote.JMXServiceURL)14 MBeanServerConnection (javax.management.MBeanServerConnection)12 MalformedURLException (java.net.MalformedURLException)11 JMXConnectorServer (javax.management.remote.JMXConnectorServer)11