在Wildfly上的Startup bean中通过JMX进行AttributeNotFoundException
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Wildfly上的Startup bean中通过JMX进行AttributeNotFoundException相关的知识,希望对你有一定的参考价值。
大家好!我正在尝试通过Startup bean的@PostConstruct方法中的JMX加载wildfly服务器的系统属性。它在部署开始时在已启动的服务器实例上正常工作,但在启动服务器实例引导时失败。
Wildfly 11.0.0.CR1启动bean代码:
package ru.wildfly.test.ejb.wildflyconsulregistrar.startup;
import ru.wildfly.test.ejb.wildflyconsulregistrar.api.ConsulRegistrar;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
@Startup
@Singleton
public class WildflyConsulRegistrarStartupBean {
@Inject
private ConsulRegistrar consulRegistrar;
@PostConstruct
public void initialize() {
registerServices();
}
private void registerServices() {
consulRegistrar.registerService("WildflyTestCluster");
}
.............
}
ConsulRegistrar代码:
package ru.wildfly.test.ejb.wildflyconsulregistrar.impl;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;
import ru.test.ejb.wildflyconsulregistrar.api.ConsulRegistrar;
import ru.wildfly.test.ejb.wildflyconsulregistrar.serversettings.api.CurrentServerNodeSettings;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
@Dependent
public class ConsulRegistrarImpl implements ConsulRegistrar {
...............
@Inject
private CurrentServerNodeSettings currentServerNodeSettings;
.............
@Override
public void registerService(String serviceName) {
String currentNodeName = currentServerNodeSettings.getCurrentNodeName();
........................
}
.......................
}
CurrentServerNodeSettings代码:
package ru.wildfly.test.ejb.wildflyconsulregistrar.serversettings.impl;
import ru.wildfly.test.ejb.wildflyconsulregistrar.serversettings.api.CurrentServerNodeSettings;
import javax.enterprise.context.Dependent;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
@Dependent
public class CurrentServerNodeSettingsWildflyImpl implements CurrentServerNodeSettings {
....................
@Override
public String getCurrentNodeName() {
String currentNodeName = getPlatformMBeanServerAttributeValue(String.class, "jboss.as:system-property=server.name", "value");
return currentNodeName;
}
private <T> T getPlatformMBeanServerAttributeValue(Class<T> valueType, String objectName, String attributeName) {
T attributeValue = null;
try {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Object attributeObject = mBeanServer.getAttribute(new ObjectName(objectName), attributeName);
attributeValue = attributeObject != null ? (valueType.cast(attributeObject)) : null;
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
return attributeValue;
}
}
错误信息:
Caused by: java.lang.IllegalStateException:
javax.management.AttributeNotFoundException:
"WFLYCTL0216: Management resource '[("system-property" => "server.name")]' not found"
at ru.wildfly.test.ejb.wildflyconsulregistrar.serversettings.impl.CurrentServerNodeSettingsWildflyImpl
.getPlatformMBeanServerAttributeValue(CurrentServerNodeSettingsWildflyImpl.java:41)
我在jboss论坛https://developer.jboss.org/message/971717#971717上发现了同样的问题,但是没有答案。
有什么建议?
答案
这是启动期间的依赖性问题,即在执行@PostConstruct
方法之后设置服务器名称。第一次从应用程序访问服务器名称时尝试加载服务器名称。
在Wildfly中,尽管定义了模块依赖性,但没有通用的方法来强制执行应用程序的部署顺序。但这对你的情况没有帮助。
以上是关于在Wildfly上的Startup bean中通过JMX进行AttributeNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章
Spring中通过变量和import标签来控制加载哪些bean
关于在Spring中通过注解形式的IoC容器 纯使用 @ComponentScan注解实现对包的自动扫描,实现非XML形式的 注解形式装配Bean类