📄 index.xtp
字号:
<document> <header> <product>resin</product> <title>MBean listeners</title> <type>tutorial</type> <description> <p> Example showing configuration of MBean event listeners. </p> </description> <tutorial-startpage>listener</tutorial-startpage> </header><body><summary/><s1 title="Files in this tutorial"><deftable><tr><td><viewfile-link file="WEB-INF/web.xml"/> </td><td>Configures the JMX-managed bean</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/Listener.java"/> </td><td>The listener bean implementation.</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/ListenerMBean.java"/> </td><td>The management interface for the listener.</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/Emitter.java"/> </td><td>The emitter bean implementation.</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/EmitterMBean.java"/> </td><td>The management interface for the emitter.</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/ListenerServlet.java"/> </td><td>Using the managed bean.</td></tr></deftable></s1><s1 title="Emitter and Listener"><p>JMX provides a general notification capability where MBean<var>emitters</var> send data to MBean <var>listeners</var>. Any managedbean can be an emitter or a listener by implementing theproper interfaces. The listeners are hooked up to the emitters eitherin the configuration file or through MBeanServer calls.</p><s2 title="Listener"><p>A listener implements <code>NotificationListener</code> to receive<code>Notification</code> events. The notification contains informationfor the type of the notification, the sender of the notification, andany notification-specific information.</p><p>The listener implements the single <code>handleNotification</code>method. It's parameters are the notification and an opaque<var>handback</var> object. The <var>handback</var> is specified duringthe listener registration and can be any information the listenerwants.</p><example title="Listener.java">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; }}</example></s2><s2 title="Emitter"><p>The Emitter sends notifications. Any managed bean which implementsthe <code>NotificationEmitter</code> interface can be an emitter. ManyEmitters will extend the <code>NotificationBroadcasterSupport</code>,although this is not required.</p><p><code>NotificationBroadcasterSupport</code> will handle the logicfor adding and removing listeners as well as sending notifications tothe proper listener. By extending<code>NotificationBroadcasterSupport</code>, the emitter only needs tocall <code>sendNotification</code> to send the notification.</p><p>The first argument for the <code>Notification</code> is the notificationtype. Because each emitter can send multiple notifications, the typetells the listener which event has happened.</p><p>The second argument is typically the <code>ObjectName</code> forthe emitter. Often, emitters will usethe <code>MBeanRegistration</code> interface to find out the<code>ObjectName</code>.</p><example title="Emitter.java">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); }}</example></s2><s2 title="web.xml configuration"><p>The web.xml (or resin.conf) configures the resource with the<resource> tag just as with<a href="doc|ioc-bean.xtp">other resources</a>. The resources isregistered as an MBean by specifying an <var>mbean-name</var>.</p><example title="web.xml"><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></example><deftable><tr><th>tag</th><th>description</th></tr><tr><td>resource</td><td>defines the resource</td></tr><tr><td>mbean-name</td><td>the MBean name of the resource</td></tr><tr><td>type</td><td>the class name of the resource bean</td></tr><tr><td>listener</td><td>registers the mbean with a notification emitter mbean</td></tr><tr><td>handback</td><td>a custom object to be passed back to the listener</td></tr></deftable></s2><s2 title="Using the listener"><p>This example provides a <code>send()</code> method totrigger a notification, but most notifications occuring whenspecific events occur, e.g. when a pool fills up.</p><p>In this case, invoking the <code>send()</code> method triggersthe notification which will be sent to any waiting listeners. Calling<code>listener.getNotificationCount()</code> checks that the listeneris getting called back.</p><example title="ListenerServlet.java">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()); }}</example><results title="output">count: 15</results><results title="log">[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)</results></s2><s2 title="Configuration with Dependency Injection"><p>The ListenerServlet example follows theDependency Injection pattern. Resin's web.xml will assemble thecorrect EmitterMBean and ListenerMBean. Using the Dependency Injectionpattern simplifies the servlet, makes it more configurable,and more testable.</p><p>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 proxyof the JNDI lookup is then passed to <code>setEmitter</code>and <code>setListener</code>.</p><example title="web.xml"><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></example></s2></s1><s1 title="Compatibility"><p>Notifications and listeners are part of the JMX standard.Client MBean proxies are standard and can be generatedwith <code>javax.management.MBeanServerInvocationHandler</code></p><p>The <resource> configuration is Resin-specific. The supportfor the Dependency Injection for servlet configurationand the "mbean:" JNDI scheme are also Resin-specific.</p></s1> </body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -