Example usage for javax.management MBeanServerFactory createMBeanServer

List of usage examples for javax.management MBeanServerFactory createMBeanServer

Introduction

In this page you can find the example usage for javax.management MBeanServerFactory createMBeanServer.

Prototype

public static MBeanServer createMBeanServer() 

Source Link

Document

Return a new object implementing the MBeanServer interface with a standard default domain name.

Usage

From source file:org.mule.management.agents.JmxAgent.java

public void initialise() throws InitialisationException {
    if (initialized) {
        return;//  w  ww.ja  v  a2 s.c  o m
    }
    if (!locateServer && !createServer) {
        throw new InitialisationException(new Message(Messages.JMX_CREATE_OR_LOCATE_SHOULD_BE_SET), this);
    }
    if (mBeanServer == null && locateServer) {
        List l = MBeanServerFactory.findMBeanServer(null);
        if (l != null && l.size() > 0) {
            mBeanServer = (MBeanServer) l.get(0);
        }
    }
    if (mBeanServer == null && createServer) {
        mBeanServer = MBeanServerFactory.createMBeanServer();
        serverCreated = true;
    }
    if (mBeanServer == null) {
        throw new InitialisationException(new Message(Messages.JMX_CANT_LOCATE_CREATE_SERVER), this);
    }
    if (connectorServerUrl != null) {
        try {
            JMXServiceURL url = new JMXServiceURL(connectorServerUrl);

            if (!credentials.isEmpty()) {
                JMXAuthenticator jmxAuthenticator = new SimplePasswordJmxAuthenticator();
                ((SimplePasswordJmxAuthenticator) jmxAuthenticator).setCredentials(credentials);
                connectorServerProperties.put(JMXConnectorServer.AUTHENTICATOR, jmxAuthenticator);
            }

            connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, connectorServerProperties,
                    mBeanServer);
        } catch (Exception e) {
            throw new InitialisationException(new Message(Messages.FAILED_TO_CREATE_X, "Jmx Connector"), e,
                    this);
        }
    }

    // We need to register all the services once the server has initialised
    MuleManager.getInstance().registerListener(new ModelEventListener() {
        public void onEvent(UMOServerEvent event) {
            if (event.getAction() == ModelEvent.MODEL_STARTED) {
                try {
                    registerStatisticsService();
                    registerMuleService();
                    registerConfigurationService();
                    registerModelService();

                    registerComponentServices();
                    registerEndpointServices();
                    registerConnectorServices();
                } catch (Exception e) {
                    throw new MuleRuntimeException(new Message(Messages.X_FAILED_TO_INITIALISE, "MBeans"), e);
                }
            }
        }
    });
    initialized = true;
}

From source file:org.mule.module.management.agent.AbstractJmxAgent.java

/**
 * {@inheritDoc}/*from   w  w w. j  a  v  a2 s  . c o m*/
 */
public void initialise() throws InitialisationException {
    if (initialized.get()) {
        return;
    }

    this.containerMode = muleContext.getConfiguration().isContainerMode();

    try {
        Object agent = muleContext.getRegistry().lookupObject(this.getClass());
        // if we find ourselves, but not initialized yet - proceed with init, otherwise return
        if (agent == this && this.initialized.get()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Found an existing JMX agent in the registry, we're done here.");
            }
            return;
        }
    } catch (Exception e) {
        throw new InitialisationException(e, this);
    }

    if (mBeanServer == null && createServer) {
        // here we create a new mbean server, not using a platform one
        mBeanServer = MBeanServerFactory.createMBeanServer();
        serverCreated.set(true);
    }

    if (mBeanServer == null && locateServer) {
        mBeanServer = ManagementFactory.getPlatformMBeanServer();
    }

    if (mBeanServer == null) {
        throw new InitialisationException(ManagementMessages.cannotLocateOrCreateServer(), this);
    }

    if (StringUtils.isBlank(muleContext.getConfiguration().getId())) {
        // TODO i18n the message properly
        throw new IllegalArgumentException(
                "Manager ID is mandatory when running with JmxAgent. Give your Mule configuration a valid ID.");
    }

    try {
        // We need to register all the services once the server has initialised
        muleContextStartedListener = new MuleContextStartedListener();
        muleContext.registerListener(muleContextStartedListener);
        // and unregister once context stopped
        muleContextStoppedListener = new MuleContextStoppedListener();
        muleContext.registerListener(muleContextStoppedListener);
    } catch (NotificationException e) {
        throw new InitialisationException(e, this);
    }
    initialized.compareAndSet(false, true);
}

From source file:org.nuxeo.runtime.management.ServerLocatorService.java

protected MBeanServer doCreateServer(final ServerLocatorDescriptor descriptor) {
    MBeanServer server = MBeanServerFactory.createMBeanServer();
    JMXServiceURL url = doFormatServerURL(descriptor);
    if (!descriptor.remote) {
        return server;
    }/*from   ww  w .  j av  a 2 s .  co m*/
    final RMIConnectorServer connector;
    try {
        connector = new RMIConnectorServer(url, null, server);
    } catch (IOException e) {
        throw new ManagementRuntimeException("Cannot start connector for " + descriptor.domainName, e);
    }
    try {
        connector.start();
    } catch (IOException e) {
        try {
            LocateRegistry.createRegistry(descriptor.rmiPort);
        } catch (RemoteException e2) {
            throw new ManagementRuntimeException("Cannot start RMI connector for " + descriptor.domainName, e);
        }
        try {
            connector.start();
        } catch (IOException e2) {
            throw new ManagementRuntimeException("Cannot start RMI connector for " + descriptor.domainName, e2);
        }
    }
    assert connector.isActive();
    log.info("Started a mbean server : " + url);
    return server;
}

From source file:org.nuxeo.runtime.remoting.transporter.TransporterClient.java

/**
 * Will set up network registry and detector for clustering (to identify
 * other remoting servers running on network).
 *///from  w w  w.j a  va 2  s  . c  o  m
private static void setupDetector() throws Exception {
    server = MBeanServerFactory.createMBeanServer();

    // the registry will house all remoting servers discovered
    registry = NetworkRegistry.getInstance();
    server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));

    // multicast detector will detect new network registries that come
    // online
    detector = new MulticastDetector();
    server.registerMBean(detector, new ObjectName("remoting:type=MulticastDetector"));
    detector.start();
}

From source file:org.openehealth.coala.lifecycle.FacesAppListener.java

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {

    /*/*from  w ww.j ava  2 s. c o  m*/
     * STARTUP PHASE - nothing yet
     */
    if (event instanceof PostConstructApplicationEvent) {
        LOG.info("PostConstructApplicationEvent is called");
    }

    /*
     * SHUTDOWN PHASE - cleaning up PDQ/XDS ipf routes (mwiesner)
     */

    if (event instanceof PreDestroyApplicationEvent) {
        LOG.info("PreDestroyApplicationEvent is called");
        MBeanServer mbserver = null;

        ArrayList<MBeanServer> mbservers = MBeanServerFactory.findMBeanServer(null);

        if (mbservers.size() > 0) {
            mbserver = (MBeanServer) mbservers.get(0);
        }

        if (mbserver != null) {
            LOG.info("Found our MBean server instance...");
        } else {
            mbserver = MBeanServerFactory.createMBeanServer();
        }

        try {
            Set<ObjectInstance> mbeans = mbserver.queryMBeans(null, null);
            for (ObjectInstance mb : mbeans) {
                if (mb.getObjectName().getCanonicalName().contains("camelContext-pdq")) {
                    LOG.info("Successfully removed MBean: " + mb.getObjectName().getCanonicalName());
                    mbserver.unregisterMBean(mb.getObjectName());
                } else if (mb.getObjectName().getCanonicalName().contains("camelContext-xds")) {
                    LOG.info("Successfully removed MBean: " + mb.getObjectName().getCanonicalName());
                    mbserver.unregisterMBean(mb.getObjectName());
                }
            }
        } catch (InstanceNotFoundException infe) {
            LOG.warn("Ignoring to unregister pdq/xds camelContext, as it was not found!?");
        } catch (Throwable e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }
}

From source file:org.wso2.carbon.caching.impl.CacheImpl.java

private MBeanServer getMBeanServer() {
    MBeanServer mserver;/*from w w  w . j a  va  2 s .c  o  m*/
    if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
        mserver = MBeanServerFactory.findMBeanServer(null).get(0);
    } else {
        mserver = MBeanServerFactory.createMBeanServer();
    }
    return mserver;
}