Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I add a NotificationListener to an MBean on WebSphere 7. The listeners are never added to the mbean?
    text
    copied!<p>I'm trying to add a NotificationListener to listen for notifications from a registered MBean.</p> <p>Through my setup of the MBean, I <em>can</em> connect to my WAS7 test environment, and see/manage my 'HelloMBean'. Also, I can call methods on it via the <code>com.ibm.websphere.management.AdminClient</code>. I've created a test listener class:</p> <pre><code>/** * Inner class that will handle the notifications. */ public static class ClientListener implements NotificationListener { public void handleNotification(Notification notification, Object handback) { RasMessage rasMessage = (RasMessage) notification.getUserData(); System.out.println("Localized message: " + rasMessage.getLocalizedMessage(null)); System.out.println("\nReceived notification:"); System.out.println("\tClassName: " + notification.getClass().getName()); System.out.println("\tSource: " + notification.getSource()); System.out.println("\tType: " + notification.getType()); System.out.println("\tMessage: " + notification.getMessage()); if (notification instanceof AttributeChangeNotification) { AttributeChangeNotification acn = (AttributeChangeNotification) notification; System.out.println("\tAttributeName: " + acn.getAttributeName()); System.out.println("\tAttributeType: " + acn.getAttributeType()); System.out.println("\tNewValue: " + acn.getNewValue()); System.out.println("\tOldValue: " + acn.getOldValue()); } } } </code></pre> <p>The steps I take are:</p> <ul> <li>Create MBean </li> <li>Register MBean (I can see it on the console).</li> <li>Then, with the correct ObjectName, I try to add the notification listener three different ways</li> </ul> <p>All of these never call addNotificationListener on the MBean</p> <pre><code>ClientListener listener = new ClientListener(); adminClient.addNotificationListenerExtended(objectName, listener, null, null); adminClient.addNotificationListener(objectName, listener, null, null); AdminServiceFactory.getMBeanFactory().getMBeanServer().addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, listener, null, null); </code></pre> <p>I then call the method on the MBean that should send a notification.</p> <p>The problem is that, in the NotificationBroadcasterSupport.sendNotification(), that there aren't any listeners on the listener list for this MBean. </p> <p>I'm guessing that the 'addNotificationListener()' method on the mbean should get called when I try to add it in the previous steps, but it <strong>never does</strong></p> <p>Any thoughts?</p> <p>I'm also trying to manually add them, which isn't working as well:</p> <p>//interface</p> <pre><code>public interface HelloMBean extends TUFMBean { public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback); public abstract String getName(); public abstract int getCacheSize(); public abstract void setCacheSize(int size); public abstract void sayHello(); public abstract int add(int x, int y); public abstract MBeanNotificationInfo[] getNotificationInfo(); public String getIdentification(); public void initialize(Object paramObject); </code></pre> <p>}</p> <p>//impl:</p> <pre><code> package cat.dcs.core.services.mbean; import javax.management.AttributeChangeNotification; import javax.management.MBeanNotificationInfo; import javax.management.Notification; import javax.management.NotificationBroadcasterSupport; import javax.management.NotificationFilter; import javax.management.NotificationListener; import junitx.util.PrivateAccessor; public class HelloNotificationImpl extends NotificationBroadcasterSupport implements HelloMBean { private String name = "HelloMBean"; private int cacheSize = DEFAULT_CACHE_SIZE; private static final int DEFAULT_CACHE_SIZE = 200; private long sequenceNumber = 1; public HelloNotificationImpl() { System.out.println("&gt;&gt;&gt;&gt;&gt; CONSTRUCTOR " + this); } /* * *************************************************** * TUFMBeanImpl req's * *************************************************** */ public HelloNotificationImpl(String paramString) { setIdentification(paramString); } private void setIdentification(String paramString) { name = paramString; } @Override public String getIdentification() { System.out.println("&gt;&gt;&gt;&gt;&gt; getIdentification " + this); return name; } @Override public void initialize(Object paramObject) { System.out.println("&gt;&gt;&gt;&gt;&gt; INIT "+ this); } @Override public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) { System.out.println("&gt;&gt;&gt;&gt;&gt; ADD NOTIFICATION LISTENER " + this); super.addNotificationListener(listener, filter, handback); try { System.out.println(PrivateAccessor.getField(this, "listenerList")); } catch (NoSuchFieldException e) { e.printStackTrace(); } } /* * *************************************************** * notification broadcaster use * *************************************************** */ /** * TODO Update method documentation for Hello.setCacheSize Description of the method. * * @param size * @see cat.cis.junk.x#setCacheSize(int) * @since 1.0 Jul 5, 2012 9:40:01 AM DudekTA */ public synchronized void setCacheSize(int size) { System.out.println("&gt;&gt;&gt;&gt;&gt; SET CACHE SIZE ON"+ this); int oldSize = cacheSize; cacheSize = size; System.out.println("Cache size now " + cacheSize); Notification n = new AttributeChangeNotification(this, sequenceNumber++, System.currentTimeMillis(), "CacheSize changed", "CacheSize", "int", oldSize, cacheSize); try { System.out.println("&gt;&gt;&gt;LISTENER LIST ON MBEAN: " + PrivateAccessor.getField(this, "listenerList")); } catch (NoSuchFieldException e) { e.printStackTrace(); } sendNotification(n); } /** * TODO Update method documentation for Hello.getNotificationInfo Description of the method. * * @return * @since 1.0 Jul 5, 2012 9:40:01 AM DudekTA */ @Override public MBeanNotificationInfo[] getNotificationInfo() { String[] types = new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE }; String name = AttributeChangeNotification.class.getName(); String description = "An attribute of this MBean has changed"; MBeanNotificationInfo info = new MBeanNotificationInfo(types, name, description); return new MBeanNotificationInfo[] { info }; } /* * *************************************************** * test methods * *************************************************** */ /** * TODO Update method documentation for Hello.sayHello Description of the method. * * @see cat.cis.junk.x#sayHello() * @since 1.0 Jul 5, 2012 9:40:01 AM DudekTA */ public void sayHello() { System.out.println("hello, world : " + this); } /** * TODO Update method documentation for Hello.add Description of the method. * * @param x * @param y * @return * @see cat.cis.junk.x#add(int, int) * @since 1.0 Jul 5, 2012 9:40:01 AM DudekTA */ public int add(int x, int y) { return x + y; } /** * TODO Update method documentation for Hello.getName Description of the method. * * @return * @see cat.cis.junk.x#getName() * @since 1.0 Jul 5, 2012 9:40:01 AM DudekTA */ public String getName() { return name; } /** * TODO Update method documentation for Hello.getCacheSize Description of the method. * * @return * @see cat.cis.junk.x#getCacheSize() * @since 1.0 Jul 5, 2012 9:40:01 AM DudekTA */ public int getCacheSize() { return cacheSize; } } </code></pre> <p>btw This isn't working either:</p> <pre><code>adminClient.invoke(objectName,"addNotificationListener" ,new Object[]{listener,null, null},new String[]{ "javax.management.NotificationListener","javax.management.NotificationFilter", "java.lang.Object" }); </code></pre> <p>Adding XML:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; </code></pre> <p> </p> <pre><code>&lt;attribute name="name" getMethod="getName" type="java.lang.String" proxyInvokeType="unicall" /&gt; &lt;attribute name="cacheSize" getMethod="getCacheSize" type="int" setMethod="setCacheSize" proxyInvokeType="unicall" /&gt; &lt;operation name="sayHello" role="operation" type="void" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"&gt; &lt;signature&gt; &lt;/signature&gt; &lt;/operation&gt; &lt;operation name="add" role="operation" type="int" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"&gt; &lt;signature&gt; &lt;parameter name="x" description="x" type="int" /&gt; &lt;parameter name="y" description="x" type="int" /&gt; &lt;/signature&gt; &lt;/operation&gt; &lt;operation name="getNotificationInfo" role="operation" type="javax.management.MBeanNotificationInfo[]" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"&gt; &lt;signature&gt; &lt;/signature&gt; &lt;/operation&gt; &lt;operation name="addNotificationListener" role="operation" type="void" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"&gt; &lt;signature&gt; &lt;!-- javax.management.NotificationListener","javax.management.NotificationFilter", "java.lang.Object --&gt; &lt;parameter name="listener" description="javax.management.NotificationListener" type="javax.management.NotificationListener" /&gt; &lt;parameter name="filter" description="javax.management.NotificationFilter" type="javax.management.NotificationFilter" /&gt; &lt;parameter name="handback" description="java.lang.Object" type="java.lang.Object" /&gt; &lt;/signature&gt; &lt;/operation&gt; </code></pre> <p></p> <p>Basic Overview of steps:</p> <pre><code>[7/9/12 10:28:08:813 CDT] 00000017 SystemOut O &gt;&gt;&gt;&gt;&gt; CONSTRUCTOR cat.dcs.core.services.mbean.HelloNotificationImpl@7f4c7f4c [7/9/12 10:28:08:813 CDT] 00000017 SystemOut O &gt;&gt;&gt;&gt;&gt; INIT cat.dcs.core.services.mbean.HelloNotificationImpl@7f4c7f4c [7/9/12 10:28:08:813 CDT] 00000017 SystemOut O &gt;&gt;&gt;&gt;&gt; getIdentification cat.dcs.core.services.mbean.HelloNotificationImpl@7f4c7f4c [7/9/12 10:28:08:820 CDT] 00000017 servlet I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [TestItEAR] [/TestIt] [TUFMBeanInitializerServlet]: Initialization successful. [7/9/12 10:28:08:824 CDT] 00000017 SystemOut O O1: [WebSphere:name=TestItEAR/TestIt.war/HelloMBean,TUFimpl=cat.dcs.core.services.mbean.HelloNotificationImpl,process=server1,TUFinterface=cat.dcs.core.services.mbean.HelloMBean,TUFtype=APPMBean,platform=dynamicproxy,node=C001460186Node02,version=7.0.0.17,type=HelloMBean,mbeanIdentifier=TestItEAR/TestIt.war/HelloMBean,cell=C001460186Node02Cell,spec=1.0] [7/9/12 10:28:08:826 CDT] 00000017 SystemOut O &gt;&gt;&gt;&gt;&gt; TRYING TO ADD LISTENER [7/9/12 10:28:08:826 CDT] 00000017 SystemOut O hello, world : cat.dcs.core.services.mbean.HelloNotificationImpl@7f4c7f4c [7/9/12 10:28:08:827 CDT] 00000017 SystemOut O &gt;&gt;&gt;&gt; CALLING METHOD WITH NOTIFICATION [7/9/12 10:28:08:827 CDT] 00000017 SystemOut O &gt;&gt;&gt;&gt;&gt; SET CACHE SIZE ONcat.dcs.core.services.mbean.HelloNotificationImpl@7f4c7f4c [7/9/12 10:28:08:827 CDT] 00000017 SystemOut O Cache size now 999 [7/9/12 10:28:08:829 CDT] 00000017 SystemOut O &gt;&gt;&gt;LISTENER LIST ON MBEAN: [] </code></pre> <p>When sendNotification gets Called, There aren't any listeners. <img src="https://i.stack.imgur.com/NIDhg.png" alt="When sendNotification gets Called, There aren&#39;t any listeners."></p> <p>@Nicholas: Not sure if this makes a difference?</p> <blockquote> <pre><code>[7/9/12 14:00:48:811 CDT] 00000012 SystemOut O &gt;&gt;&gt;&gt;&gt; AC CLASSLOADER IS: </code></pre> <p>org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@3ae03ae [7/9/12 14:00:48:812 CDT] 00000012 SystemOut O O1: [WebSphere:name=TestItEAR/TestIt.war/HelloMBean,TUFimpl=cat.dcs.core.services.mbean.HelloNotificationImpl,process=server1,TUFinterface=cat.dcs.core.services.mbean.HelloMBean,TUFtype=APPMBean,platform=dynamicproxy,node=C001460186Node02,version=7.0.0.17,type=HelloMBean,mbeanIdentifier=TestItEAR/TestIt.war/HelloMBean,cell=C001460186Node02Cell,spec=1.0] [7/9/12 14:00:48:814 CDT] 00000012 SystemOut O >>>>> TRYING TO ADD LISTENER on this mbean below: [7/9/12 14:00:48:814 CDT] 00000012 SystemOut O hello, world : cat.dcs.core.services.mbean.HelloNotificationImpl@36c436c4 [7/9/12 14:00:48:814 CDT] 00000012 SystemOut O >>>>> MY CLASSLOADER IS: com.ibm.ws.classloader.CompoundClassLoader@586a586a[war:TestItEAR/TestIt.war] Local ClassPath: C:\workspace\TestIt\WebContent\WEB-INF\classes;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-beanutils-1.7.0.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-codec-1.4.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-collections-3.2.1.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-discovery-0.2.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-fileupload-1.2.1.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-httpclient-3.1.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-io-1.4.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-lang-2.4.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-logging-1.1.1.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-logging-adapters-1.1.1.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-logging-api-1.1.1.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\commons-net-1.4.1.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\jgl.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\junit-addons-1.4.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\tuf-common.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\tuf-mbeans.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\tuf-server.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\tuf-web.jar;C:\workspace\TestIt\WebContent\WEB-INF\lib\tuf-webservices.jar;C:\workspace\TestIt\WebContent Parent: com.ibm.ws.classloader.CompoundClassLoader@56cb56cb[app:TestItEAR]<br> Delegation Mode: PARENT_FIRST</p> </blockquote> <p>Where the classloader for the mbean, and the admin client are different? : probably easier to read: <img src="https://i.stack.imgur.com/25HAO.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload