179.5.4. 如何使用 JMX NotificationListener 侦听 camel 事件?
Camel 通知事件提供指导性概述情况。您可以查看上下文和端点的生命周期事件,您可以看到被接收并发送到端点的交换。
从 Camel 2.4 中,您可以使用自定义 JMX NotificationListener 来侦听 camel 事件。
首先,您需要在启动 CamelContext 前设置 JmxNotificationEventNotifier
:
// Set up the JmxNotificationEventNotifier notifier = new JmxNotificationEventNotifier(); notifier.setSource("MyCamel"); notifier.setIgnoreCamelContextEvents(true); notifier.setIgnoreRouteEvents(true); notifier.setIgnoreServiceEvents(true); CamelContext context = new DefaultCamelContext(createRegistry()); context.getManagementStrategy().addEventNotifier(notifier);
其次,您可以注册您的监听程序以侦听事件:
// 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);