如何将NotificationListener添加到WebSphere 7上的MBean。监听器永远不会添加到mbean中?

时间:2012-07-09 14:40:17

标签: websphere jmx

我尝试添加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的侦听器列表上没有任何侦听器。

我猜测' addNotificationListener()'当我尝试在前面的步骤中添加mbean时,应调用mbean上的方法,但从不

有什么想法吗?

我还试图手动添加它们,但这也不起作用:

//接口

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

}

// IMPL:

   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这不起作用:

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被调用时,没有任何侦听器。 When sendNotification gets Called, There aren't any listeners.

@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:命名= TestItEAR / TestIt.war / HelloMBean,TUFimpl = cat.dcs.core.services.mbean.HelloNotificationImpl,过程= server1的,TUFinterface = cat.dcs.core.services.mbean.HelloMBean,TUFtype = APPMBean,平台= dynamicproxy,节点= C001460186Node02,版本= 7.0.0.17,类型= HelloMBean,mbeanIdentifier = TestItEAR / TestIt.war / HelloMBean,细胞= C001460186Node02Cell,规格= 1.0]   [7/9/12 14:00:48:814 CDT] 00000012 SystemOut O&gt;&gt;&gt;&gt;&gt;试着添加   关于这个mbean的LISTENER:[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&gt;&gt;&gt;&gt;&gt;我的经纪人是:   com.ibm.ws.classloader.CompoundClassLoader@586a586a [战争:TestItEAR / TestIt.war]   本地ClassPath:   C:\工作空间\的TestIt \的WebContent \ WEB-INF \类; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地BeanUtils的-1.7.0.jar; C:\工作空间\的TestIt \的WebContent \ WEB -INF \ lib中\公地编解码器-1.4.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地集合-3.2.1.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地发现-0.2.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地文件上传-1.2.1.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地HttpClient的-3.1.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地-IO-1.4.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地琅-2.4.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\共享记录-1.1.1.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\共享记录的适配器-1.1.1.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\共享记录-API-1.1.1.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\公地-net-1.4.1.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\ jgl.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\ JUnit插件-1.4.jar ; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\ TUF-common.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\ TUF-mbeans.jar; C:\工作空间\的TestIt \的WebContent \ WEB- INF \ lib中\ TUF-的server.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\ TUF-web.jar; C:\工作空间\的TestIt \的WebContent \ WEB-INF \ lib中\ TUF-web服务。罐; C:\工作空间\的TestIt \的WebContent   家长:   com.ibm.ws.classloader.CompoundClassLoader@56cb56cb [应用程式:TestItEAR]
  委派模式:PARENT_FIRST

mbean的类加载器和admin客户端有什么不同? :可能更容易阅读: enter image description here

2 个答案:

答案 0 :(得分:0)

如果您发布HelloMBean的代码,将会有所帮助。这里有几点建议:

是否实施NotificationBroadcasterNotificationEmitter或延长NotificationBroadcasterSupport

确保调用的方法调用sendNotification

也可能(不确定WAS)bean必须在MBeanNotificationInfo中发布MBeanInfo来描述可以订阅的通知。

答案 1 :(得分:0)

所以......它是如何运作的。

我从RMI连接更改为SOAP连接,我可以注册监听器。

我不知道为什么RMI连接允许我对MBean执行操作,但不设置侦听器,而我可以通过使用soap连接的adminclient执行所有操作。

我没有充分的理由,但这就是我修复它的方式。