简体   繁体   English

在Wildfly上通过Startup bean中的JMX通过AttributeNotFoundException

[英]AttributeNotFoundException through JMX in Startup bean on Wildfly

Hello everyone! 大家好! I'm trying to load wildfly server's system properties through JMX in Startup bean's @PostConstruct method. 我正在尝试通过Startup bean的@PostConstruct方法中的JMX加载wildfly服务器的系统属性。 It works fine on the already started server instance when deployment starts, but fails while starting with server instance bootstrapping. 部署开始时,它在已经启动的服务器实例上可以正常工作,但是在启动服务器实例引导时失败。

Wildfly 11.0.0.CR1 Startup bean code: 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 code: 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 code: 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;
    }
}

Error message: 错误信息:

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)

I have found the same issue on jboss forum https://developer.jboss.org/message/971717#971717 , but it was unanswered. 我在jboss论坛https://developer.jboss.org/message/971717#971717上发现了相同的问题,但未得到解答。

Any suggestions? 有什么建议么?

This is a dependency problem during startup, ie the server name is set after your @PostConstruct method gets executed. 这是启动过程中的依赖项问题,即在执行@PostConstruct方法后设置服务器名称。 Try to load the server name lazy when it is accessed from the application for the first time. 第一次从应用程序访问服务器名称时,尝试加载服务器名称。

In Wildfly there is no generic way to enforce the sequence of deployments from the application despite the definition of module dependencies. 尽管定义了模块依赖性,但是在Wildfly中,没有通用的方法可以从应用程序强制执行部署顺序。 But this won't help in your case. 但这对您的情况无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM