How to add NotificationListener to MBean in WebSphere 7. Are listeners never added to mbean?

I am trying to add a NotificationListener to listen for notifications from a registered MBean.

Through my MBean installation, I can connect to my WAS7 test environment and see / manage my "HelloMBean". In addition, I can call methods on it using com.ibm.websphere.management.AdminClient . I created a test listener class:

 /** * 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()); } } } 

I am doing the following steps:

  • Create MBean
  • Register the MBean (I see it on the console).
  • Then, with the correct ObjectName, I try to add a notification listener in three different ways.

All this never calls addNotificationListener on the MBean

 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); 

Then I call the method on the MBean, which should send a notification.

The problem is that in NotificationBroadcasterSupport.sendNotification () there are no listeners in the listener list for this MBean.

I suggest that the addNotificationListener () method for mbean should be called when I try to add it to the previous steps, but never does

Any thoughts?

I am also trying to manually add them, which also does not work:

//interface

 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); 

}

// realizing:

  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(">>>>> 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(">>>>> getIdentification " + this); return name; } @Override public void initialize(Object paramObject) { System.out.println(">>>>> INIT "+ this); } @Override public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) { System.out.println(">>>>> 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(">>>>> 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(">>>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; } } 

btw This does not work either:

 adminClient.invoke(objectName,"addNotificationListener" ,new Object[]{listener,null, null},new String[]{ "javax.management.NotificationListener","javax.management.NotificationFilter", "java.lang.Object" }); 

Adding XML:

 <?xml version="1.0" encoding="UTF-8"?> 

 <attribute name="name" getMethod="getName" type="java.lang.String" proxyInvokeType="unicall" /> <attribute name="cacheSize" getMethod="getCacheSize" type="int" setMethod="setCacheSize" proxyInvokeType="unicall" /> <operation name="sayHello" role="operation" type="void" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"> <signature> </signature> </operation> <operation name="add" role="operation" type="int" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"> <signature> <parameter name="x" description="x" type="int" /> <parameter name="y" description="x" type="int" /> </signature> </operation> <operation name="getNotificationInfo" role="operation" type="javax.management.MBeanNotificationInfo[]" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"> <signature> </signature> </operation> <operation name="addNotificationListener" role="operation" type="void" targetObjectType="objectReference" impact="ACTION" proxyInvokeType="multicall"> <signature> <!-- javax.management.NotificationListener","javax.management.NotificationFilter", "java.lang.Object --> <parameter name="listener" description="javax.management.NotificationListener" type="javax.management.NotificationListener" /> <parameter name="filter" description="javax.management.NotificationFilter" type="javax.management.NotificationFilter" /> <parameter name="handback" description="java.lang.Object" type="java.lang.Object" /> </signature> </operation> 

Basic steps:

 [7/9/12 10:28:08:813 CDT] 00000017 SystemOut O >>>>> CONSTRUCTOR cat.dcs.core.services.mbean.HelloNotificationImpl@7f4c7f4c [7/9/12 10:28:08:813 CDT] 00000017 SystemOut O >>>>> INIT cat.dcs.core.services.mbean.HelloNotificationImpl@7f4c7f4c [7/9/12 10:28:08:813 CDT] 00000017 SystemOut O >>>>> 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 >>>>> 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 >>>> CALLING METHOD WITH NOTIFICATION [7/9/12 10:28:08:827 CDT] 00000017 SystemOut O >>>>> 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 >>>LISTENER LIST ON MBEAN: [] 

When sendNotification gets Called, there are no listeners. When sendNotification gets Called, There aren't any listeners.

@Nicholas: Not sure if that matters?

 [7/9/12 14:00:48:811 CDT] 00000012 SystemOut O >>>>> AC CLASSLOADER IS: 

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 / TestIBewar. , cell = C001460186Node02Cell, specs = 1.0] [7/9/12 14: 00: 48: 814 CDT] 00000012 SystemOut O β†’ β†’> FIVE 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 CLASSATOR: com.ibm.ws.classloader.CompoundClassLoader@586a586a [war: TestItEAR / TestIt .war] Local ClassPath: C: \ workstation \ TestIt \ WebContent \ WEB-INF \ classes; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Wikimedia Commons BeanUtils-1.7.0.jar; C: \ workspace \ TestIt \ WebContent \ WEB -INF \ Lib \ Common Codec-1.4.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Wikimedia Commons collection-3.2.1.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ General-discovery-0.2.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Common-FileUpload-1.2.1.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Common-HttpClient-3.1.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Common-1.4.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Common Languages ​​-2.4.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ General Logging 1.1.1.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Wikimedia Commons Logging Adapters -1.1.1.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ General logging api-1.1.1.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ Network-1.4.1.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ jgl.jar; C: \ workspace \ TestIt \ WebContent \ WEB-INF \ Lib \ JUnit-add-ons-1.4.jar; C: \ work Space \ 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. a jar; C: \ workspace \ TestIt \ WebContent parent: com.ibm.ws.classloader.CompoundClassLoader@56cb56cb [app: TestItEAR]
Delegation Mode: PARENT_FIRST

Where are the cool bootloader for mbean and client-client different? : perhaps easier to read: enter image description here

+4
source share
2 answers

This will help if you post your HelloMBean code. Here are a few pointers:

Does it use NotificationBroadcaster or NotificationEmitter or continue with NotificationBroadcasterSupport ?

Verify that the called method calls sendNotification .

It is also possible that the bean needs MBeanNotificationInfo in it MBeanInfo , which describes the notifications that you can subscribe to.

0
source

So ... how did it happen.

I changed the RMI connection to the SOAP connection and I was able to register the listener.

I do not know why the RMI connection allowed me to work with the MBean, but not to set the listener, while I can do everything through the admin client that uses the soap connection.

I have no good reason, but how I corrected it.

0
source

All Articles