Search in sources :

Example 6 with ObjectInstance

use of javax.management.ObjectInstance in project jmxtrans by jmxtrans.

the class JmxResultProcessorTest method canReadTabularData.

@Test
public void canReadTabularData() throws MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
    ObjectInstance runtime = getRuntime();
    AttributeList attr = ManagementFactory.getPlatformMBeanServer().getAttributes(runtime.getObjectName(), new String[] { "SystemProperties" });
    List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), runtime, attr.asList(), runtime.getClassName(), TEST_DOMAIN_NAME).getResults();
    assertThat(results.size()).isGreaterThan(2);
    Optional<Result> result = from(results).firstMatch(new ByAttributeName("SystemProperties.java.version"));
    assertThat(result.isPresent()).isTrue();
    assertThat(result.get().getAttributeName()).isEqualTo("SystemProperties.java.version");
    Map<String, Object> values = result.get().getValues();
    assertThat(values).hasSize(2);
    Object objectValue = result.get().getValues().get("key");
    assertThat(objectValue).isInstanceOf(String.class);
    String key = (String) objectValue;
    assertThat(key).isEqualTo("java.version");
}
Also used : AttributeList(javax.management.AttributeList) ObjectInstance(javax.management.ObjectInstance) JmxResultProcessor(com.googlecode.jmxtrans.model.JmxResultProcessor) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Example 7 with ObjectInstance

use of javax.management.ObjectInstance in project rest.li by linkedin.

the class LoadBalancerClientCli method resetTogglingStores.

public static void resetTogglingStores(String host, boolean enabled) throws Exception {
    MonitoredHost _host = MonitoredHost.getMonitoredHost(new HostIdentifier(host));
    for (Object pidObj : _host.activeVms()) {
        int pid = (Integer) pidObj;
        System.out.println("checking pid: " + pid);
        JMXServiceURL jmxUrl = null;
        com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(pid + "");
        try {
            // get the connector address
            String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
            // establish connection to connector server
            if (connectorAddress != null) {
                jmxUrl = new JMXServiceURL(connectorAddress);
            }
        } finally {
            vm.detach();
        }
        if (jmxUrl != null) {
            System.out.println("got jmx url: " + jmxUrl);
            // connect to jmx
            JMXConnector connector = JMXConnectorFactory.connect(jmxUrl);
            connector.connect();
            MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
            // look for all beans in the d2 name space
            Set<ObjectInstance> objectInstances = mbeanServer.queryMBeans(new ObjectName("com.linkedin.d2:*"), null);
            for (ObjectInstance objectInstance : objectInstances) {
                System.err.println("checking object: " + objectInstance.getObjectName());
                // if we've found a toggling store, then toggle it
                if (objectInstance.getObjectName().toString().endsWith("TogglingStore")) {
                    System.out.println("found toggling zk store, so toggling to: " + enabled);
                    mbeanServer.invoke(objectInstance.getObjectName(), "setEnabled", new Object[] { enabled }, new String[] { "boolean" });
                }
            }
        } else {
            System.out.println("pid is not a jmx process: " + pid);
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HostIdentifier(sun.jvmstat.monitor.HostIdentifier) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MonitoredHost(sun.jvmstat.monitor.MonitoredHost) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 8 with ObjectInstance

use of javax.management.ObjectInstance in project pinpoint by naver.

the class PinpointMBeanServerTest method test.

@Test
public void test() throws Exception {
    PinpointMBeanServer mBeanServer = new PinpointMBeanServer();
    ATest aTest = new ATest();
    mBeanServer.registerMBean(aTest);
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    String mBeanObjectName = "com.navercorp.pinpoint.collector.mbean:type=PinpointMBeanServerTest$ATest";
    ObjectName objectName = new ObjectName(mBeanObjectName);
    String packageName = this.getClass().getPackage().getName();
    ObjectInstance instance = server.getObjectInstance(objectName);
    Assert.assertEquals(packageName + ".PinpointMBeanServerTest$ATest", instance.getClassName());
    mBeanServer.unregisterMBean(aTest);
}
Also used : ObjectInstance(javax.management.ObjectInstance) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 9 with ObjectInstance

use of javax.management.ObjectInstance in project graphdb by neo4j-attic.

the class KernelProxy method allBeans.

protected List<Object> allBeans() {
    List<Object> beans = new ArrayList<Object>();
    Iterable<ObjectInstance> mbeans;
    try {
        mbeans = server.queryMBeans(mbeanQuery(), null);
    } catch (IOException handled) {
        return beans;
    }
    for (ObjectInstance instance : mbeans) {
        String className = instance.getClassName();
        Class<?> beanType = null;
        try {
            if (className != null)
                beanType = Class.forName(className);
        } catch (Exception ignored) {
        // fall through
        } catch (LinkageError ignored) {
        // fall through
        }
        if (beanType != null) {
            try {
                beans.add(BeanProxy.load(server, beanType, instance.getObjectName()));
            } catch (Exception ignored) {
            // fall through
            }
        }
    }
    return beans;
}
Also used : ArrayList(java.util.ArrayList) ObjectInstance(javax.management.ObjectInstance) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with ObjectInstance

use of javax.management.ObjectInstance in project presto by prestodb.

the class RebindSafeMBeanServer method registerMBean.

/**
     * Delegates to the wrapped mbean server, but if a mbean is already registered
     * with the specified name, the existing instance is returned.
     */
@Override
public ObjectInstance registerMBean(Object object, ObjectName name) throws MBeanRegistrationException, NotCompliantMBeanException {
    while (true) {
        try {
            // try to register the mbean
            return mbeanServer.registerMBean(object, name);
        } catch (InstanceAlreadyExistsException ignored) {
        }
        try {
            // a mbean is already installed, try to return the already registered instance
            ObjectInstance objectInstance = mbeanServer.getObjectInstance(name);
            log.debug("%s already bound to %s", name, objectInstance);
            return objectInstance;
        } catch (InstanceNotFoundException ignored) {
        // the mbean was removed before we could get the reference
        // start the whole process over again
        }
    }
}
Also used : InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance)

Aggregations

ObjectInstance (javax.management.ObjectInstance)75 ObjectName (javax.management.ObjectName)36 Test (org.junit.Test)27 InstanceNotFoundException (javax.management.InstanceNotFoundException)15 MBeanServer (javax.management.MBeanServer)12 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)10 JmxResultProcessor (com.googlecode.jmxtrans.model.JmxResultProcessor)9 Result (com.googlecode.jmxtrans.model.Result)9 HashSet (java.util.HashSet)7 Attribute (javax.management.Attribute)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 AttributeNotFoundException (javax.management.AttributeNotFoundException)5 MBeanRegistrationException (javax.management.MBeanRegistrationException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 HashMap (java.util.HashMap)4 AttributeList (javax.management.AttributeList)4 MBeanException (javax.management.MBeanException)4 MBeanServerConnection (javax.management.MBeanServerConnection)4 ReflectionException (javax.management.ReflectionException)4