[英]Registering an MBean with the JBoss MBean server - Unable to find the JBoss MBean server
我正在使用Spring公開MBean並將其注冊到JBoss MBean服務器。 當戰爭放到JBoss實例中時,這很好用。 但是在運行單元測試時它不起作用(這是有道理的,因為沒有JBoss實例在運行)這是spring配置的摘錄
<bean id="updateConfigMBean" class="mypackage.UpdateConfigMBean"/>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="server">
<bean class="org.jboss.mx.util.MBeanServerLocator" factory-method="locateJBoss"/>
</property>
<property name="beans">
<map>
<entry key="mypackage:name=configurationMBean" value-ref="updateConfigMBean"/>
</map>
</property>
</bean>
我正在尋找的是一種解決此問題的優雅方法(不想有兩個spring配置(用於測試和部署),並且禁用spring配置驗證測試不是一種選擇。
謝謝!
這是您可能必須具有兩個配置的一種情況,一個用於測試,一個用於部署。
這是Maven的優點,因為您在部署配置和測試配置之間有明確的區分。 如果您擔心要使兩個配置保持最新,則需要以一種結構化配置的方式將所有公共位導入到其他配置中(這就是我們的做法)。
我正在使用@Bean解決該問題。 @Bean是為進行特定於環境的Bean創建而量身定制的。
下面的邏輯基本上是在開發(Tomcat)和測試(JUnit)中使用MBeanServerFactoryBean。 否則,請使用JBoss MBean服務器。
@Bean
def mbeanServer: MBeanServer = {
val server = if (environment == "development" || environment == "test") {
val factory = new MBeanServerFactoryBean
factory.setLocateExistingServerIfPossible(true)
factory.setRegisterWithFactory(true)
factory.afterPropertiesSet()
log.info("using default MBeanServer")
factory.getObject
} else {
val clazz = Class.forName("org.jboss.mx.util.MBeanServerLocator")
val locateJboss = clazz.getMethod("locateJBoss", List.empty[Class[_]].toArray: _*)
log.info("using JBoss MBeanServer")
locateJboss.invoke(null,
List.empty[java.lang.Object].toArray: _*).asInstanceOf[MBeanServer]
}
log.info("mbeanServer: " + server)
server
}
在Spring 3.1中,您可以解決此問題,並且仍然可以通過使用配置文件來使用XML配置。 但以上內容適用於Spring 3.0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.