Example usage for javax.management MBeanServerConnection queryNames

List of usage examples for javax.management MBeanServerConnection queryNames

Introduction

In this page you can find the example usage for javax.management MBeanServerConnection queryNames.

Prototype

public Set<ObjectName> queryNames(ObjectName name, QueryExp query) throws IOException;

Source Link

Document

Gets the names of MBeans controlled by the MBean server.

Usage

From source file:com.example.Client.java

public static void main(String[] args) throws Exception {
    // Create an RMI connector client and
    // connect it to the RMI connector server
    ////from w  w w .  ja va 2 s  . c o m
    echo("\nCreate an RMI connector client and " + "connect it to the RMI connector server");
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi");
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

    // Create listener
    //
    ClientListener listener = new ClientListener();

    // Get an MBeanServerConnection
    //
    echo("\nGet an MBeanServerConnection");
    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
    waitForEnterPressed();

    // Get domains from MBeanServer
    //
    echo("\nDomains:");
    String domains[] = mbsc.getDomains();
    Arrays.sort(domains);
    for (String domain : domains) {
        echo("\tDomain = " + domain);
    }
    waitForEnterPressed();

    // Get MBeanServer's default domain
    //
    echo("\nMBeanServer default domain = " + mbsc.getDefaultDomain());

    // Get MBean count
    //
    echo("\nMBean count = " + mbsc.getMBeanCount());

    // Query MBean names
    //
    echo("\nQuery MBeanServer MBeans:");
    Set<ObjectName> names = new TreeSet<ObjectName>(mbsc.queryNames(null, null));
    for (ObjectName name : names) {
        echo("\tObjectName = " + name);
    }
    waitForEnterPressed();

    // ----------------------
    // Manage the Hello MBean
    // ----------------------

    echo("\n>>> Perform operations on Hello MBean <<<");

    // Construct the ObjectName for the Hello MBean
    //
    ObjectName mbeanName = new ObjectName("com.example:type=Hello");

    // Create a dedicated proxy for the MBean instead of
    // going directly through the MBean server connection
    //
    HelloMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName, HelloMBean.class, true);

    // Add notification listener on Hello MBean
    //
    echo("\nAdd notification listener...");
    mbsc.addNotificationListener(mbeanName, listener, null, null);

    // Get CacheSize attribute in Hello MBean
    //
    echo("\nCacheSize = " + mbeanProxy.getCacheSize());

    // Set CacheSize attribute in Hello MBean
    // Calling "reset" makes the Hello MBean emit a
    // notification that will be received by the registered
    // ClientListener.
    //
    mbeanProxy.setCacheSize(150);

    // Sleep for 2 seconds to have time to receive the notification
    //
    echo("\nWaiting for notification...");
    sleep(2000);

    // Get CacheSize attribute in Hello MBean
    //
    echo("\nCacheSize = " + mbeanProxy.getCacheSize());

    // Invoke "sayHello" in Hello MBean
    //
    echo("\nInvoke sayHello() in Hello MBean...");
    mbeanProxy.sayHello();

    // Invoke "add" in Hello MBean
    //
    echo("\nInvoke add(2, 3) in Hello MBean...");
    echo("\nadd(2, 3) = " + mbeanProxy.add(2, 3));

    waitForEnterPressed();

    // ------------------------------
    // Manage the QueueSampler MXBean
    // ------------------------------

    echo("\n>>> Perform operations on QueueSampler MXBean <<<");

    // Construct the ObjectName for the QueueSampler MXBean
    //
    ObjectName mxbeanName = new ObjectName("com.example:type=QueueSampler");

    // Create a dedicated proxy for the MXBean instead of
    // going directly through the MBean server connection
    //
    QueueSamplerMXBean mxbeanProxy = JMX.newMXBeanProxy(mbsc, mxbeanName, QueueSamplerMXBean.class);

    // Get QueueSample attribute in QueueSampler MXBean
    //
    QueueSample queue1 = mxbeanProxy.getQueueSample();
    echo("\nQueueSample.Date = " + queue1.getDate());
    echo("QueueSample.Head = " + queue1.getHead());
    echo("QueueSample.Size = " + queue1.getSize());

    // Invoke "clearQueue" in QueueSampler MXBean
    //
    echo("\nInvoke clearQueue() in QueueSampler MXBean...");
    mxbeanProxy.clearQueue();

    // Get QueueSample attribute in QueueSampler MXBean
    //
    QueueSample queue2 = mxbeanProxy.getQueueSample();
    echo("\nQueueSample.Date = " + queue2.getDate());
    echo("QueueSample.Head = " + queue2.getHead());
    echo("QueueSample.Size = " + queue2.getSize());

    waitForEnterPressed();

    // Close MBeanServer connection
    //
    echo("\nClose the connection to the server");
    jmxc.close();
    echo("\nBye! Bye!");
}

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

@Nullable
protected static ObjectName getObjectName(final MBeanServerConnection connection, final Class objectClass)
        throws IOException {

    Set<ObjectName> names = connection.queryNames(null, null);
    return IterableUtils.find(names, o -> {
        MBeanInfo info;/*from w  ww. ja  v a2  s  . c  om*/
        try {
            info = connection.getMBeanInfo(o);
        } catch (InstanceNotFoundException | UnmarshalException e) {
            return false;
        } catch (Exception e) {
            throw new JmxControlException(e);
        }
        return Objects.equals(objectClass.getName(), info.getClassName());
    });
}

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

protected static Collection<ObjectName> getSuitableObjectNames(final MBeanServerConnection connection,
        final Class objectClass) throws IOException {
    Set<ObjectName> names = connection.queryNames(null, null);

    // find all suitable beans
    @SuppressWarnings("unchecked")
    Collection<ObjectName> suitableNames = CollectionUtils.select(names, o -> {
        MBeanInfo info;/*  ww  w  .  java2  s  .c om*/
        try {
            info = connection.getMBeanInfo(o);
        } catch (Exception e) {
            throw new JmxControlException(e);
        }
        return Objects.equals(objectClass.getName(), info.getClassName());
    });

    return suitableNames;
}

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

protected static ObjectName getObjectName(final MBeanServerConnection connection, final String remoteContext,
        final Class objectClass) throws IOException {
    Set<ObjectName> names = connection.queryNames(null, null);
    return IterableUtils.find(names, o -> {
        if (!Objects.equals(remoteContext, o.getDomain())) {
            return false;
        }//from   w  w  w. ja v  a2  s  .  c  o m

        MBeanInfo info;
        try {
            info = connection.getMBeanInfo(o);
        } catch (Exception e) {
            throw new JmxControlException(e);
        }
        return Objects.equals(objectClass.getName(), info.getClassName());
    });
}

From source file:gr.cslab.Metric_test.java

static public void list() {
    String host = hosts.remove(0);
    try {//from  w  ww. jav  a2 s  . c  o  m

        String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
        System.out.println("RMI URL:\t" + url + "");
        JMXServiceURL serviceUrl = new JMXServiceURL(url);
        try (JMXConnector jmxc = JMXConnectorFactory.connect(serviceUrl, null)) {
            MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

            System.out.println("List of available names");

            Set<ObjectName> names;
            names = new TreeSet<>(mbsc.queryNames(null, null));
            System.out.println("Available names:");
            for (ObjectName name : names) {
                System.out.println("\tObjectName = " + name);
            }
        }
    } catch (IOException ex) {
        System.err.println("ERROR: failed to query the server " + host);
    }

}

From source file:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps a local or remote MBeanServer's entire object tree for support
 * purposes. Nested arrays and CompositeData objects in MBean attribute
 * values are handled.//w w  w  .  j  a  va 2 s. co  m
 * 
 * @param connection
 *            the server connection (or server itself)
 * @param out
 *            PrintWriter to write the output to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void dumpConnection(MBeanServerConnection connection, PrintWriter out) throws IOException {
    JmxDumpUtil.showStartBanner(out);

    // Get all the object names
    Set<ObjectName> objectNames = connection.queryNames(null, null);

    // Sort the names (don't assume ObjectName implements Comparable in JDK
    // 1.5)
    Set<ObjectName> newObjectNames = new TreeSet<ObjectName>(new Comparator<ObjectName>() {
        public int compare(ObjectName o1, ObjectName o2) {
            return o1.toString().compareTo(o2.toString());
        }
    });
    newObjectNames.addAll(objectNames);
    objectNames = newObjectNames;

    // Dump each MBean
    for (ObjectName objectName : objectNames) {
        try {
            printMBeanInfo(connection, objectName, out, null);
        } catch (JMException e) {
            // Sometimes beans can disappear while we are examining them
        }
    }
}

From source file:com.athena.dolly.console.module.jmx.JmxClientManager.java

public static HashMap<String, Object> getObjectNameInfo(ObjectName objName, String nodeName) {
    JmxClient jmxClient = jmxClientMap.get(nodeName);

    try {//www  . ja va2s  .c o m
        MBeanServerConnection connection = jmxClient.getJmxConnector().getMBeanServerConnection();
        HashMap<String, Object> infoMap = new HashMap<String, Object>();
        Set<ObjectName> names = new TreeSet<ObjectName>(connection.queryNames(objName, null));

        for (ObjectName name : names) {
            logger.info("#######################");
            logger.info("\tObjectName = " + name);

            MBeanInfo info = connection.getMBeanInfo(name);
            MBeanAttributeInfo[] attributes = info.getAttributes();

            for (MBeanAttributeInfo attr : attributes) {
                logger.info("==========================");
                logger.info("attrName = " + attr.getName());
                logger.info("attrType = " + attr.getType());
                logger.info("connection.getAttribute = " + connection.getAttribute(name, attr.getName()));

                infoMap.put(attr.getName(), connection.getAttribute(name, attr.getName()));
            }
        }

        return infoMap;
    } catch (Exception e) {
        logger.error("unhandled exception has errored : ", e);
    }

    return null;
}

From source file:io.mapzone.controller.um.launcher.ArenaLauncher.java

protected void configureInstance(ProjectInstanceRecord instance) throws MalformedURLException {
    // config via JMX
    String url = "service:jmx:rmi:///jndi/rmi://" + instance.host.get().inetAddress.get() + ":"
            + instance.process.get().jmxPort.get() + "/jmxrmi";
    JMXServiceURL serviceUrl = new JMXServiceURL(url);
    try (JMXConnector connector = JMXConnectorFactory.connect(serviceUrl, null);) {
        MBeanServerConnection conn = connector.getMBeanServerConnection();

        log.info(url);//w ww.  j  a  v a  2  s  .c  o  m
        while (conn.queryNames(ArenaConfigMBean.NAME.get(), null).isEmpty()) {
            log.info("No such MBean: " + ArenaConfigMBean.NAME.get());
            Thread.sleep(100);
        }

        String localHttpPort = ControllerPlugin.instance().httpPort();

        ArenaConfigMBean arenaConfig = JMX.newMBeanProxy(conn, ArenaConfigMBean.NAME.get(),
                ArenaConfigMBean.class);
        checked(() -> arenaConfig.setAppTitle(instance.project.get()));
        checked(() -> arenaConfig.setServiceAuthToken(project().serviceAuthToken.get())); // send before URLs!!!
        checked(() -> arenaConfig.setCatalogServerUrl("http://localhost:" + localHttpPort + "/csw"));
        checked(() -> arenaConfig.setProxyUrl(ProxyServlet.projectUrlBase(project())));
        checked(() -> arenaConfig.setProjectCatalogId(project().catalogId.get()));

        log.info("Instance process configured.");
    } catch (Throwable e) {
        log.warn("Error while configuring instance.", e);
    }
}

From source file:org.hyperic.hq.product.jmx.MBeanDumper.java

public void dump(Properties config) throws Exception {
    MBeanServerConnection mServer = getMBeanServer(config);
    dump(mServer.queryNames(getQuery(), null));
}

From source file:com.boundary.plugin.sdk.jmx.MBeansTransformer.java

private void traverseMBeans() {
    MBeanServerConnection connection = this.client.getMBeanServerConnection();
    try {/*from www. j  a va  2  s  . c  o m*/
        Set<ObjectName> mbeans = connection.queryNames(null, null);
        List<ObjectName> mbeanList = new ArrayList<ObjectName>(mbeans);
        Collections.sort(mbeanList);
        for (ObjectName obj : mbeans) {
            transform.beginMBean(obj);
            traverseAttributes(obj);
            transform.endMBean();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}