2012-04-13 67 views
0

您好,最近我開始研究JMX bean實現。當JMX引發通知時引發java.io.NotSerializableException

然後我發佈下面的bean,運行JConsole,連接到bean,註冊通知。然而,當發送通知我得到以下錯誤:

2012年4月13日下午5時31分26秒ClientNotifForwarder NotifFetcher.fetchOneNotif 警告:無法反序列化的通知:java.io.NotSerializableException:COM。 * .jmx.TaskMergeMBean

任何幫助將是最歡迎的,我花了一天試圖找出這一個更好的一部分。

感謝, 喬納森

public class TaskMBean extends NotificationBroadcasterSupport implements DynamicMBean { 

    private final TaskStateChangedEventListener taskChangedListener; 

    public TaskMBean (DriverIf driver) { 
    taskChangedListener= new TaskStateChangedEventListener (this); 
    driver.registerMergeTaskStateChangedListener(mergeTaskChangedListener); 
    } 
    @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 }; 
    } 

    @Override 
    public Object getAttribute(String attribute) throws AttributeNotFoundException,   MBeanException, 
     ReflectionException { 
    // TODO Auto-generated method stub 
    return null; 
    } 

    @Override 
    public void setAttribute(Attribute attribute) throws AttributeNotFoundException, 
     InvalidAttributeValueException, MBeanException, ReflectionException { 
    // TODO Auto-generated method stub 

    } 

    @Override 
    public AttributeList getAttributes(String[] attributes) { 
    // TODO Auto-generated method stub 
    return null; 
    } 

    @Override 
    public AttributeList setAttributes(AttributeList attributes) { 
    // TODO Auto-generated method stub 
    return null; 
    } 

    @Override 
    public Object invoke(String actionName, Object[] params, String[] signature) 
     throws MBeanException, ReflectionException { 
    // TODO Auto-generated method stub 
    return null; 
    } 

    @Override 
    public MBeanInfo getMBeanInfo() { 
    MBeanNotificationInfo haltInfo = 
     new MBeanNotificationInfo(
      new String[] { "NOTIFICATION_TYPE_MERGE_STATE_CHANGE" }, 
      Notification.class.getName(), "server halt on fatal error"); 
    MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { haltInfo }; 
    return new OpenMBeanInfoSupport(XhiveMergeMBean.class.getName(), "", null, null, null, 
     notifications); 
    } 
} 

public class TaskStateChangedEventListener implements Serializable { 

    static final String NOTIFICATION_TYPE_MERGE_STATE_CHANGE = "com.xhive.lucene.merge"; 
    private final NotificationBroadcasterSupport broadcaster; 
    private int notificationSequence = 1; 

    public TaskStateChangedEventListener (NotificationBroadcasterSupport broadcaster) { 
    this.broadcaster = broadcaster; 
    } 

    @Override 
    public void notify(Object source) { 
    Notification n = 
     new AttributeChangeNotification(this, notificationSequence++, System.currentTimeMillis(), "", "", "int", 1, 2); 
    broadcaster.sendNotification(n); 
    } 
} 
+1

'TaskMergeMBean'是否實現了'Serializable'? – 2012-04-13 15:41:17

回答

2

什麼彼得說。但也.....

通知通常(但並不總是)必須序列,因此使用爲通知源往往把一個凹痕。 (雙關語意)

所以你需要完全確定的的實例這是序列化(最好是有效的話)或更好,但送什麼是一個簡單的表示,喜歡的ObjectName MBean。目的是讓接收者(或過濾器)能夠確定通知的來源,所以我發現一致地使用信息對象名稱確實有幫助。

最後,JConsole在業務類(默認情況下,我的意思是)上往往有點偏薄,所以如果您依賴JConsole很多,並希望能夠清晰地查看所有通知,則需要確保您只在有效負載中使用核心JDK類型。 (或使用OpenTypes(相同的東西)或啓用遠程類加載(不值得麻煩))。

// Nicholas

相關問題