Search in sources :

Example 41 with ObjectInstance

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

the class JmxResultProcessorTest method canReadCompositeData.

@Test
public void canReadCompositeData() throws MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
    ObjectInstance memory = getMemory();
    AttributeList attr = ManagementFactory.getPlatformMBeanServer().getAttributes(memory.getObjectName(), new String[] { "HeapMemoryUsage" });
    List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), memory, attr.asList(), memory.getClassName(), TEST_DOMAIN_NAME).getResults();
    assertThat(results).hasSize(1);
    Result result = results.get(0);
    assertThat(result.getAttributeName()).isEqualTo("HeapMemoryUsage");
    assertThat(result.getTypeName()).isEqualTo("type=Memory");
    Map<String, Object> values = result.getValues();
    assertThat(values).hasSize(4);
    Object objectValue = result.getValues().get("init");
    assertThat(objectValue).isInstanceOf(Long.class);
}
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 42 with ObjectInstance

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

the class MBeansTest method checkResourceMBean.

private void checkResourceMBean(String name) throws MalformedObjectNameException {
    final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    final ObjectName objectName = new ObjectName("org.glassfish.jersey:type=myApplication,subType=Uris,resource=\"" + name + "\"");
    ObjectInstance mbean = null;
    try {
        mbean = mBeanServer.getObjectInstance(objectName);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Resource MBean name '" + name + "' not found.");
    }
    assertNotNull(mbean);
}
Also used : InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 43 with ObjectInstance

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

the class Query method fetchResults.

public Iterable<Result> fetchResults(MBeanServerConnection mbeanServer, ObjectName queryName) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    MBeanInfo info = mbeanServer.getMBeanInfo(queryName);
    ObjectInstance oi = mbeanServer.getObjectInstance(queryName);
    List<String> attributes;
    if (attr.isEmpty()) {
        attributes = new ArrayList<>();
        for (MBeanAttributeInfo attrInfo : info.getAttributes()) {
            attributes.add(attrInfo.getName());
        }
    } else {
        attributes = attr;
    }
    try {
        if (!attributes.isEmpty()) {
            logger.debug("Executing queryName [{}] from query [{}]", queryName.getCanonicalName(), this);
            AttributeList al = mbeanServer.getAttributes(queryName, attributes.toArray(new String[attributes.size()]));
            return new JmxResultProcessor(this, oi, al.asList(), info.getClassName(), queryName.getDomain()).getResults();
        }
    } catch (UnmarshalException ue) {
        if ((ue.getCause() != null) && (ue.getCause() instanceof ClassNotFoundException)) {
            logger.debug("Bad unmarshall, continuing. This is probably ok and due to something like this: " + "http://ehcache.org/xref/net/sf/ehcache/distribution/RMICacheManagerPeerListener.html#52", ue.getMessage());
        } else {
            throw ue;
        }
    }
    return ImmutableList.of();
}
Also used : MBeanInfo(javax.management.MBeanInfo) AttributeList(javax.management.AttributeList) UnmarshalException(java.rmi.UnmarshalException) ObjectInstance(javax.management.ObjectInstance) ToString(lombok.ToString) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 44 with ObjectInstance

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

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 45 with ObjectInstance

use of javax.management.ObjectInstance in project camel by apache.

the class DefaultManagementAgent method registerMBeanWithServer.

private void registerMBeanWithServer(Object obj, ObjectName name, boolean forceRegistration) throws JMException {
    // have we already registered the bean, there can be shared instances in the camel routes
    boolean exists = isRegistered(name);
    if (exists) {
        if (forceRegistration) {
            LOG.info("ForceRegistration enabled, unregistering existing MBean with ObjectName: {}", name);
            server.unregisterMBean(name);
        } else {
            // okay ignore we do not want to force it and it could be a shared instance
            LOG.debug("MBean already registered with ObjectName: {}", name);
        }
    }
    // register bean if by force or not exists
    ObjectInstance instance = null;
    if (forceRegistration || !exists) {
        LOG.trace("Registering MBean with ObjectName: {}", name);
        instance = server.registerMBean(obj, name);
    }
    // need to use the name returned from the server as some JEE servers may modify the name
    if (instance != null) {
        ObjectName registeredName = instance.getObjectName();
        LOG.debug("Registered MBean with ObjectName: {}", registeredName);
        mbeansRegistered.put(name, registeredName);
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName)

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