Spring JMX通知:警告:无法反序列化通知

问题描述

|| 我从JConsole收到警告消息,每次我的应用程序将通知发送到已注册的Spring MBean服务器时,都会显示该警告消息。 我正在尝试将JMX与Spring 3.0结合使用。 我已经将我的ServiceImpl类注册为MBean服务器。这是我的配置
<!-- MBean Server Factory -->
<bean id=\"mbeanServer\" class=\"org.springframework.jmx.support.MBeanServerfactorybean\">
 <property name=\"locateExistingServerIfPossible\" value=\"true\"></property>
 <property name=\"agentId\" value=\"MMAC-056_1306399012572\"></property>
</bean>

<!-- this bean must not be lazily initialized if the exporting is to happen -->
<bean id=\"exporter\" 
class=\"org.springframework.jmx.export.MBeanExporter\" lazy-init=\"false\">
<property name=\"beans\">
<map>
   <entry key=\"bean:name=searchCampaignService\" value-ref=\"campaignService\" />
</map>
</property>
<property name=\"server\" ref=\"mbeanServer\"></property>
<property name=\"autodetect\" value=\"true\"></property>
<property name=\"registrationBehaviorName\" value=\"REGISTRATION_REPLACE_EXISTING\" />
<property name=\"assembler\">
<bean class=\"org.springframework.jmx.export.assembler
                                               .MethodNameBasedMBeanInfoAssembler\">
<property name=\"managedMethods\">
    <value>searchCampaignById</value>
</property>
</bean>
</property>
<property name=\"notificationListenerMappings\">
<map>
    <entry key=\"bean:name=searchCampaignService\">
        <bean class=\"com....TestLoggingNotificationHandler\" />
    </entry>
</map>
</property>

</bean>
我的serviceimpl类实现了NotificationPublisherAware接口,并且我正在从作为托管方法公开的服务方法之一发送通知。我想先在本地测试通知。 我还可以看到JConsole显示带有操作和通知节点的bean。 我订阅通知并运行我的客户端应用程序。 一旦运行客户端应用程序,JConsole就会显示警告,指出
May 26,2011 4:53:41 AM ClientNotifForwarder NotifFetcher.fetchOneNotif
WARNING: Failed to deserialize a notification: java.io.NotSerializableException:   

org.springframework.jmx.export.notification.ModelMBeanNotificationPublisher
我将服务和UserDataObject设置为可序列化的。
notification.setUserData(\"Test notification\"+ctr);
但JConsole仍然继续显示警告。     

解决方法

曼尼什 根据您提供的错误消息,JConsle无法反序列化通知,因为它包含对org.springframework.jmx.export.notification.ModelMBeanNotificationPublisher实例的引用,我认为这是无意的。您可以张贴发送通知的代码吗?     ,发送通知时,您创建的Notification对象应具有source属性,该属性是
MBean/MXBean
的实例。因为对于JMX客户端(例如JConsole和其他客户端),只有这些对象是已知的并且可序列化。 因此,如果要在MBean实例中发送此通知,只需在Notification类中使用ѭ4作为源。
 public class NotificationManager extends 
       NotificationBroadcasterSupport implements 
             **NotificationManagerMXBean** {

 private AtomicInteger sequence = new AtomicInteger();

@Override
public void initialize() {
    this.sendNotification(new Notification(\"Initialization\",**this**,sequence.incrementAndGet()));

}
可以使用以下通知类型来发送更详细的通知。您可以自己序列化对象,即JSon序列化。
this.sendNotification(
      new Notification(\"New\",this,sequence.incrementAndGet(),Instant.now().toEpochMilli(),myJSONSeiazedObject*));