我正在尝试添加一个 NotificationListener 来监听来自已注册 MBean 的通知。
通过我的 MBean 设置,我可以连接到我的 WAS7 测试环境,并查看/管理我的“HelloMBean”。另外,我可以通过com.ibm.websphere.management.AdminClient
. 我创建了一个测试监听器类:
/**
* 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());
}
}
}
我采取的步骤是:
- 创建 MBean
- 注册 MBean(我可以在控制台上看到它)。
- 然后,使用正确的 ObjectName,我尝试以三种不同的方式添加通知侦听器
所有这些都不会在 MBean 上调用 addNotificationListener
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);
然后我调用应该发送通知的 MBean 上的方法。
问题在于,在 NotificationBroadcasterSupport.sendNotification() 中,此 MBean 的侦听器列表中没有任何侦听器。
我猜当我尝试在前面的步骤中添加它时,应该调用 mbean 上的 'addNotificationListener()' 方法,但它从来没有
有什么想法吗?
我也在尝试手动添加它们,但效果不佳:
//界面
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);
}
//实现:
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;
}
}
顺便说一句,这也不起作用:
adminClient.invoke(objectName,"addNotificationListener" ,new Object[]{listener,null, null},new String[]{ "javax.management.NotificationListener","javax.management.NotificationFilter", "java.lang.Object" });
添加 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>
基本步骤概述:
[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: []
当调用 sendNotification 时,没有任何侦听器。
@Nicholas:不确定这是否有所作为?
[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/TestIt.war/HelloMBean,cell=C001460186Node02Cell,spec=1.0] [7/9/12 14:00:48:814 CDT] 00000012 SystemOut O >>>> 尝试在下面的这个 mbean 上添加侦听器: [7/9/12 14:00:48:814 CDT] 00000012 SystemOut O 你好,世界:cat.dcs.core.services.mbean.HelloNotificationImpl@36c436c4 [7/9/12 14:00:48:814 CDT] 00000012 SystemOut O >>>>> 我的类加载器是:com.ibm.ws.classloader。CompoundClassLoader@586a586a[war:TestItEAR/TestIt.war] 本地类路径: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:\工作区\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:\工作区\TestIt\WebContent 父级:com.ibm.ws.classloader.CompoundClassLoader@56cb56cb[app:TestItEAR]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 父级:com.ibm.ws.classloader.CompoundClassLoader@56cb56cb[app:TestItEAR]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 父级:com.ibm.ws.classloader.CompoundClassLoader@56cb56cb[app:TestItEAR]
委托模式:PARENT_FIRST
mbean 的类加载器和管理客户端在哪里不同?: 可能更容易阅读: