Automatically unregister MBeans (registered by Spring) when WebLogic application is undeployed

Question!

In my Spring application (which is deployed on WebLogic server) I have the following Spring beans definitions:

<context:mbean-server />

<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
        <map>
            <entry key="SpringBeans:name=hibernateStatisticsMBean,subsystem=${subsystem}" value-ref="hibernateStatisticsMBean" />                           
        </map>
    </property>
</bean>

<bean name="hibernateStatisticsMBean" class="org.hibernate.jmx.StatisticsService">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

It registers new MBeans when application is being deployed and works perfectly. However, when I undeploy the application and deploy it again it complains about MBean being there already.

How can I automatically unregister MBean during application undeploy? Can it be done through Spring or do I need to do some WebLogic magic for that?



Answers

The currently accepted answer now references code that has since become deprecated. A small change is necessary to comply with the update (Spring 3.2 and beyond).

<property name="registrationPolicy">
   <util:constant static-field="org.springframework.jmx.support.RegistrationPolicy.REPLACE_EXISTING" />
</property>

The "registrationPolicy" property replaces "registrationBehaviorName" in the answer provided.

By : Jeremy D


Using REGISTRATION_REPLACE_EXISTING may solve your problem i.e. it lets the app restart, but it doesn't address the question that you asked - "How can I automatically unregister MBean during application undeploy?".

MBeanExporter implements DisposableBean so when the ApplicationContext is closed, which should be happening when the application is undeployed, its destroy method gets called which unregisters the beans and listeners that it previously registered.

There are various bits of logging that will output any problems that occur during the unregistering.

You should check that the ApplicationContext is actually being closed, because that is the trigger.



Add the following property:

<property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>

so that your mBeanExporter looks like:

<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
    <property name="beans">
        <map>
            <entry key="SpringBeans:name=hibernateStatisticsMBean,subsystem=${subsystem}" value-ref="hibernateStatisticsMBean" />                           
        </map>
    </property>
</bean>


This video can help you solving your question :)
By: admin