java - MBean 持久性

标签 java spring jmx jboss5.x

我的配置 MBean 的持久性存在问题。我的配置:

<bean id="adminMBean" class="pl.mobileexperts.catchme.mbeans.AdminSettingsMBean"></bean>

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="assembler" ref="assembler" />
    <property name="autodetect" value="true" />
    <property name="namingStrategy" ref="namingStrategy"/>
</bean>

<bean id="attributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
    <property name="attributeSource" ref="attributeSource" />
</bean>
<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="attributeSource" />
</bean>

 @ManagedResource(objectName = "pl.mobileexperts.catchme:name=adminMBean",
 description  ="admin settings",
 persistPolicy = "OnUpdate",
 persistLocation = "c:/", persistName = "adminSettings.jmx")
 public class AdminSettingsMBean {

      private boolean moderatorModeEnabled;

      public AdminSettingsMBean() {
      }

      @ManagedAttribute(persistPolicy = "OnUpdate")
      public boolean isModeratorModeEnabled() {
        return moderatorModeEnabled;
      }

      @ManagedAttribute(persistPolicy = "OnUpdate")
      public void setModeratorModeEnabled(boolean moderatorModeEnabled) {
        this.moderatorModeEnabled = moderatorModeEnabled;
      }
 }

我的目标是在属性更改后保存状态(保存到文件或元数据 - 而不是 db)。 JBoss 重新启动后,我的 MBean 使用标准值初始化。似乎 PersistPolicy 不起作用...我尝试实现 PersistentMBean,但从未调用过 store() 和 load()。我发现这可能是 JBoss JMX 实现问题。还有一些人使用 AOP 和 MBean 中的注释方法来存储它们。所有这些帖子都是从 2008 年到 2010 年发布的,所以可能发生了一些变化?

我的 JBoss 配置是默认的 (jboss-service.xml)

最佳答案

我认为你的问题是 JMX 的 JBoss 实现。根据 JSR160,为属性指定 persistPolicy=OnUpdate 应该会导致每次更新属性时都保持不变(来自 JSR160 1.4):

persistPolicy - Defines the default persistence policy for attributes in this MBean that do not define their own persistPolicy. Takes on one of the following values:

[...]

  • OnUpdate - The attribute is stored every time the attribute is updated.

这很可能是由 Sun 的 PersistMBean 的 Javadoc 中的这个非常奇怪的文本引起的(正如 @Plínio Pantaleão 所指出的):

Do not store the MBean if 'persistPolicy' field is:

= "never"

= "onUpdate"

= "onTimer" && now < 'lastPersistTime' + 'persistPeriod'

除了向 JBoss 报告此问题(以及向 Sun 报告 Javadoc 问题)之外,您还可以使用 persistPolicy=Always 策略(同样来自 JSR160)来解决它:

  • Always - This is a synonym of OnUpdate, which is recognized for compatibility reasons. It is recommended that applications use OnUpdate instead. An implementation of the Descriptor interface, such as DescriptorSupport, can choose to replace a value of “Always” for persistPolicy by a value of “OnUpdate”.

关于java - MBean 持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359483/

相关文章:

java - 如何以jsp页面的spring形式获取当前日期

java - HTTP header 中缺少 Spring WebServiceTemplate SOAPAction

jboss - 如何使用-cp通过内联脚本执行JMXterm?

java - Spring 注入(inject)值

java - java中是否需要添加 volatile 关键字来保证单例类的线程安全?

java - 无法在开放类次环境中启动 hazelcast。服务器套接字绑定(bind)失败。没有权限

java - JMX 密码读取访问问题

命令行中的 Java 执行因 JMX 错误而失败

java - 在 ReportPortal、Java Cucumber 中手动设置启动名称

java - Java 中使用 ArrayList 的基本冒泡排序