JmxUtils.java 文件源码

java
阅读 38 收藏 0 点赞 0 评论 0

项目:lams 作者:
/**
 * Attempt to find a locally running {@code MBeanServer}. Fails if no
 * {@code MBeanServer} can be found. Logs a warning if more than one
 * {@code MBeanServer} found, returning the first one from the list.
 * @param agentId the agent identifier of the MBeanServer to retrieve.
 * If this parameter is {@code null}, all registered MBeanServers are considered.
 * If the empty String is given, the platform MBeanServer will be returned.
 * @return the {@code MBeanServer} if found
 * @throws org.springframework.jmx.MBeanServerNotFoundException
 * if no {@code MBeanServer} could be found
 * @see javax.management.MBeanServerFactory#findMBeanServer(String)
 */
public static MBeanServer locateMBeanServer(String agentId) throws MBeanServerNotFoundException {
    MBeanServer server = null;

    // null means any registered server, but "" specifically means the platform server
    if (!"".equals(agentId)) {
        List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
        if (servers != null && servers.size() > 0) {
            // Check to see if an MBeanServer is registered.
            if (servers.size() > 1 && logger.isWarnEnabled()) {
                logger.warn("Found more than one MBeanServer instance" +
                        (agentId != null ? " with agent id [" + agentId + "]" : "") +
                        ". Returning first from list.");
            }
            server = servers.get(0);
        }
    }

    if (server == null && !StringUtils.hasLength(agentId)) {
        // Attempt to load the PlatformMBeanServer.
        try {
            server = ManagementFactory.getPlatformMBeanServer();
        }
        catch (SecurityException ex) {
            throw new MBeanServerNotFoundException("No specific MBeanServer found, " +
                    "and not allowed to obtain the Java platform MBeanServer", ex);
        }
    }

    if (server == null) {
        throw new MBeanServerNotFoundException(
                "Unable to locate an MBeanServer instance" +
                (agentId != null ? " with agent id [" + agentId + "]" : ""));
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Found MBeanServer: " + server);
    }
    return server;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号