gpt4 book ai didi

JMX 引发通知时引发 java.io.NotSerializedException

转载 作者:行者123 更新时间:2023-12-02 07:51:46 24 4
gpt4 key购买 nike

您好,最近我开始致力于 JMX bean 实现。

然后,我发布以下 bean,运行 JConsole,连接到该 bean,注册通知。但是,当发送通知时,我收到以下错误:

2012 年 4 月 13 日下午 5:31:26 ClientNotifForwarder NotifFetcher.fetchOneNotif警告:无法反序列化通知:java.io.NotSerializedException: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);
}
}

最佳答案

彼得说的话。但也......

通知通常(但并非总是)必须序列化,因此使用this作为通知源往往会削弱that。 (双关语)

因此,您需要完全确保 this 的实例是可序列化的(最好是有用的),或者更好的是,发送 this 的更简单表示,如 MBean 的 ObjectName。目的是让接收者(或过滤器)能够确定通知的来源,因此我发现一致使用信息丰富的 ObjectName 确实很有帮助。

最后,JConsole 在业务类上往往有点薄弱(我的意思是默认情况下),因此如果您非常依赖 JConsole 并且希望能够干净地查看所有通知,则需要确保您仅在有效负载中使用核心 JDK 类型。

(或者使用 OpenTypes(同样的事情)或启用远程类加载(不值得麻烦))。

//尼古拉斯

关于JMX 引发通知时引发 java.io.NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10143939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com