| ||||||||||||||||||||||||||||||||||
Resin 3.1 Documentation Examples Changes Quercus Database Amber EJB SOA/ESB IoC JMS Servlet JMX Hessian Security Basic Listener Registration MBeanServer |
Example showing configuration of MBean event listeners. Files in this tutorial
Emitter and ListenerJMX 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.ListenerA listener implements The listener implements the single 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; } } EmitterThe Emitter sends notifications. Any managed bean which implements
the
The first argument for the The second argument is typically the 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); } } web.xml configurationThe 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>
Using the listenerThis example provides a In this case, invoking the 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) Configuration with Dependency InjectionThe 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 <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> CompatibilityNotifications and listeners are part of the JMX standard.
Client MBean proxies are standard and can be generated
with The <resource> configuration is Resin-specific. The support for the Dependency Injection for servlet configuration and the "mbean:" JNDI scheme are also Resin-specific.
|