resin MBean listeners tutorial Example showing configuration of MBean event listeners. listener
Configures the JMX-managed bean The listener bean implementation. The management interface for the listener. The emitter bean implementation. The management interface for the emitter. Using the managed bean.

JMX provides a general notification capability where MBean send data to MBean . Any managed bean can be an emitter or a listener by implementing the proper interfaces. The listeners are hooked up to the emitters either in the configuration file or through MBeanServer calls.

A listener implements NotificationListener to receive Notification events. The notification contains information for the type of the notification, the sender of the notification, and any notification-specific information.

The listener implements the single handleNotification method. It's parameters are the notification and an opaque object. The is specified during the listener registration and can be any information the listener wants.

package example; import javax.management.NotificationListener; import javax.management.Notification; public class Listener implements NotificationListener, ListenerMBean { private int _count; public void handleNotification(Notification notif, Object handback) { _count++; } public int getNotificationCount() { return _count; } }

The Emitter sends notifications. Any managed bean which implements the NotificationEmitter interface can be an emitter. Many Emitters will extend the NotificationBroadcasterSupport, although this is not required.

NotificationBroadcasterSupport will handle the logic for adding and removing listeners as well as sending notifications to the proper listener. By extending NotificationBroadcasterSupport, the emitter only needs to call sendNotification to send the notification.

The first argument for the Notification is the notification type. Because each emitter can send multiple notifications, the type tells the listener which event has happened.

The second argument is typically the ObjectName for the emitter. Often, emitters will use the MBeanRegistration interface to find out the ObjectName.

package example; import javax.management.NotificationBroadcasterSupport; import javax.management.Notification; /** * Implements an MBean which sends notifications. */ public class Emitter extends NotificationBroadcasterSupport implements EmitterMBean { private long _sequence; /** * Sends a notification. */ public void send() { Notification notif; notif = new Notification("example.send", this, _sequence++); sendNotification(notif); } }

The web.xml (or resin.conf) configures the resource with the <resource> tag just as with other resources. The resources is registered as an MBean by specifying an . <web-app xmlns="http://caucho.com/ns/resin"> <resource mbean-name="example:name=emitter" type="example.Emitter"> </resource> <resource mbean-name="example:name=listener" type="example.Listener"> <listener mbean-name="example:name=emitter" handback="tutorial"/> </resource> </web-app> tagdescription resourcedefines the resource mbean-namethe MBean name of the resource typethe class name of the resource bean listenerregisters the mbean with a notification emitter mbean handbacka custom object to be passed back to the listener

This example provides a send() method to trigger a notification, but most notifications occuring when specific events occur, e.g. when a pool fills up.

In this case, invoking the send() method triggers the notification which will be sent to any waiting listeners. Calling listener.getNotificationCount() checks that the listener is getting called back.

public class ListenerServlet extends GenericServlet { private EmitterMBean _emitter; private ListenerMBean _listener; public void setEmitter(EmitterMBean emitter) { _emitter = emitter; } public void setListener(ListenerMBean listener) { _listener = listener; } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); _emitter.send(); out.println("listener count: " + _listener.getNotificationCount()); } } count: 15 [15:37:15.545] notification(type=example.send,handback=tutorial) [15:37:16.624] notification(type=example.send,handback=tutorial) [15:37:17.453] notification(type=example.send,handback=tutorial)

The ListenerServlet example follows the Dependency Injection pattern. Resin's web.xml will assemble the correct EmitterMBean and ListenerMBean. Using the Dependency Injection pattern simplifies the servlet, makes it more configurable, and more testable.

The configuration takes advantage of the "mbean:" JNDI scheme in Resin. The name following "mbean:" is used to lookup the mbean instance. The "mbean:" scheme then constructs a proxy for the mbean. The proxy of the JNDI lookup is then passed to setEmitter and setListener.

<servlet-mapping url-pattern="/listener" servlet-class="example.ListenerServlet"> <init> <emitter>\${jndi:lookup("mbean:example:name=emitter")}</emitter> <listener>\${jndi:lookup("mbean:example:name=listener")}</listener> </init> </servlet-mapping>

Notifications and listeners are part of the JMX standard. Client MBean proxies are standard and can be generated with javax.management.MBeanServerInvocationHandler

The <resource> configuration is Resin-specific. The support for the Dependency Injection for servlet configuration and the "mbean:" JNDI scheme are also Resin-specific.