2011-03-24 41 views
3

在我的Spring應用程序(這是部署在WebLogic服務器上),我有以下的Spring bean定義:自動註銷的MBean(被Spring註冊),當WebLogic應用被卸載

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

它註冊新的MBean時應用被部署並完美運作。但是,當我取消部署應用程序並再次部署它時,它會抱怨MBean已經存在。

如何在應用程序取消部署期間自動取消註冊MBean?可以通過Spring來完成嗎?還是我需要爲此做一些WebLogic魔術?

+0

Hows是創建的上下文 - 使用ContextLoaderListener?這可能是因爲上下文沒有關閉 - 這會導致其他問題。 – gkamal 2011-03-24 16:12:06

回答

5

添加以下屬性:

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

讓你的MBeanExporter的樣子:

<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> 
2

使用REGISTRATION_REPLACE_EXISTING可以解決你的問題,即它可以讓應用程序重新啓動,但它並沒有解決您問的問題 - 「在卸載應用程序時,我如何自動取消註冊MBean?」。

MBeanExporter實現了DisposableBean,所以當ApplicationContext關閉時,當應用程序被取消部署時應該發生這種情況,它的destroy方法被調用,它取消註冊之前註冊的bean和偵聽器。

有各種各樣的日誌記錄,可以輸出在註銷過程中發生的任何問題。

您應該檢查ApplicationContext實際上是否被關閉,因爲那是觸發器。

1

目前接受的答案現在引用的代碼已被棄用。遵守更新(Spring 3.2及更高版本)需要做一些小改動。

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

「registrationPolicy」屬性將在所提供的答案中替換「registrationBehaviorName」。

+0

你可以簡化爲:,因爲Spring確實可以識別枚舉名 – berhauz 2017-06-27 21:32:04