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;
}
});
}
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);
}
}
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();
}
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();
}
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);
}
}
Aggregations