Example usage for org.springframework.jmx MBeanServerNotFoundException MBeanServerNotFoundException

List of usage examples for org.springframework.jmx MBeanServerNotFoundException MBeanServerNotFoundException

Introduction

In this page you can find the example usage for org.springframework.jmx MBeanServerNotFoundException MBeanServerNotFoundException.

Prototype

public MBeanServerNotFoundException(String msg, Throwable cause) 

Source Link

Document

Create a new MBeanServerNotFoundException with the specified error message and root cause.

Usage

From source file:org.springframework.jmx.access.ConnectorDelegate.java

/**
 * Connects to the remote {@code MBeanServer} using the configured {@code JMXServiceURL}:
 * to the specified JMX service, or to a local MBeanServer if no service URL specified.
 * @param serviceUrl the JMX service URL to connect to (may be {@code null})
 * @param environment the JMX environment for the connector (may be {@code null})
 * @param agentId the local JMX MBeanServer's agent id (may be {@code null})
 *///from w  w  w  .  j  a va  2s  . c o m
public MBeanServerConnection connect(@Nullable JMXServiceURL serviceUrl, @Nullable Map<String, ?> environment,
        @Nullable String agentId) throws MBeanServerNotFoundException {

    if (serviceUrl != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Connecting to remote MBeanServer at URL [" + serviceUrl + "]");
        }
        try {
            this.connector = JMXConnectorFactory.connect(serviceUrl, environment);
            return this.connector.getMBeanServerConnection();
        } catch (IOException ex) {
            throw new MBeanServerNotFoundException(
                    "Could not connect to remote MBeanServer [" + serviceUrl + "]", ex);
        }
    } else {
        logger.debug("Attempting to locate local MBeanServer");
        return JmxUtils.locateMBeanServer(agentId);
    }
}

From source file:org.springframework.jmx.access.NotificationListenerRegistrar.java

/**
 * Registers the specified {@code NotificationListener}.
 * <p>Ensures that an {@code MBeanServerConnection} is configured and attempts
 * to detect a local connection if one is not supplied.
 *///from   w w  w.  j a v a 2  s.com
public void prepare() {
    if (this.server == null) {
        this.server = this.connector.connect(this.serviceUrl, this.environment, this.agentId);
    }
    try {
        this.actualObjectNames = getResolvedObjectNames();
        if (this.actualObjectNames != null) {
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Registering NotificationListener for MBeans " + Arrays.asList(this.actualObjectNames));
            }
            for (ObjectName actualObjectName : this.actualObjectNames) {
                this.server.addNotificationListener(actualObjectName, getNotificationListener(),
                        getNotificationFilter(), getHandback());
            }
        }
    } catch (IOException ex) {
        throw new MBeanServerNotFoundException(
                "Could not connect to remote MBeanServer at URL [" + this.serviceUrl + "]", ex);
    } catch (Exception ex) {
        throw new JmxException("Unable to register NotificationListener", ex);
    }
}

From source file:org.springframework.jmx.support.JmxUtils.java

/**
 * 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 MBeanServerNotFoundException if no {@code MBeanServer} could be found
 * @see javax.management.MBeanServerFactory#findMBeanServer(String)
 *//*from  w  w  w . ja  va2s.c  om*/
public static MBeanServer locateMBeanServer(@Nullable 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 (!CollectionUtils.isEmpty(servers)) {
            // 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;
}