Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MBean persistence

I have a problem with persistence of my config MBean. My configuration:

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

My goal is to save state after a property change (save to file or metadata - not to db). After a JBoss restart, my MBean is initialized with standard values. It seems PersistPolicy is not working... I tried to implement PersistentMBean, but store() and load() were never invoked. I found that it may be a JBoss JMX implementation issue. Also some people used AOP and annotated methods in MBean to store them. All these posts were from 2008-2010, so maybe something has changed?

My JBoss config is default (jboss-service.xml)

like image 992
Jakub C Avatar asked Feb 20 '12 10:02

Jakub C


1 Answers

I think your problem is the JBoss implementation of JMX. According to JSR160, specifying persistPolicy=OnUpdate for an attribute should result in persisting every time the attribute is updated (from 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.

It is most likely caused by this very strange text in Sun's Javadoc for PersistMBean (as pointed out by @Plínio Pantaleão):

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

= "never"

= "onUpdate"

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

Other than reporting this to JBoss (and the Javadoc issue to Sun), you may be able to work around it by using persistPolicy=Always policy (again, from 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”.
like image 101
dovetalk Avatar answered Oct 05 '22 14:10

dovetalk