Search in sources :

Example 31 with Attribute

use of javax.management.Attribute in project spring-framework by spring-projects.

the class NotificationListenerTests method testRegisterNotificationListenerWithFilter.

@SuppressWarnings("serial")
@Test
public void testRegisterNotificationListenerWithFilter() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    JmxTestBean bean = new JmxTestBean();
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName.getCanonicalName(), bean);
    CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);
    listenerBean.setNotificationFilter(new NotificationFilter() {

        @Override
        public boolean isNotificationEnabled(Notification notification) {
            if (notification instanceof AttributeChangeNotification) {
                AttributeChangeNotification changeNotification = (AttributeChangeNotification) notification;
                return "Name".equals(changeNotification.getAttributeName());
            } else {
                return false;
            }
        }
    });
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
    start(exporter);
    // update the attributes
    String nameAttribute = "Name";
    String ageAttribute = "Age";
    server.setAttribute(objectName, new Attribute(nameAttribute, "Rob Harrop"));
    server.setAttribute(objectName, new Attribute(ageAttribute, new Integer(90)));
    assertEquals("Listener not notified for Name", 1, listener.getCount(nameAttribute));
    assertEquals("Listener incorrectly notified for Age", 0, listener.getCount(ageAttribute));
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) ObjectName(javax.management.ObjectName) JmxTestBean(org.springframework.jmx.JmxTestBean) NotificationFilter(javax.management.NotificationFilter) Test(org.junit.Test)

Example 32 with Attribute

use of javax.management.Attribute in project spring-framework by spring-projects.

the class NotificationListenerTests method testNotificationListenerRegistrarWithMultipleNames.

@Test
public void testNotificationListenerRegistrarWithMultipleNames() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    ObjectName objectName2 = ObjectName.getInstance("spring:name=Test2");
    JmxTestBean bean = new JmxTestBean();
    JmxTestBean bean2 = new JmxTestBean();
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName.getCanonicalName(), bean);
    beans.put(objectName2.getCanonicalName(), bean2);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    start(exporter);
    CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
    NotificationListenerRegistrar registrar = new NotificationListenerRegistrar();
    registrar.setServer(server);
    registrar.setNotificationListener(listener);
    //registrar.setMappedObjectNames(new Object[] {objectName, objectName2});
    registrar.setMappedObjectNames(new String[] { "spring:name=Test", "spring:name=Test2" });
    registrar.afterPropertiesSet();
    // update the attribute
    String attributeName = "Name";
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
    registrar.destroy();
    // try to update the attribute again
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener notified after destruction", 1, listener.getCount(attributeName));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) NotificationListenerRegistrar(org.springframework.jmx.access.NotificationListenerRegistrar) JmxTestBean(org.springframework.jmx.JmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 33 with Attribute

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

the class SpringManagedCustomProcessorTest method testManageCustomProcessor.

public void testManageCustomProcessor() throws Exception {
    MBeanServer mbeanServer = getMBeanServer();
    ObjectName on = ObjectName.getInstance("org.apache.camel:context=camel-1,type=processors,name=\"custom\"");
    getMockEndpoint("mock:result").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedHeaderReceived("foo", "hey");
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    String foo = (String) mbeanServer.getAttribute(on, "Foo");
    assertEquals("hey", foo);
    // change foo
    mbeanServer.setAttribute(on, new Attribute("Foo", "changed"));
    resetMocks();
    getMockEndpoint("mock:result").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedHeaderReceived("foo", "changed");
    template.sendBody("direct:start", "Bye World");
    assertMockEndpointsSatisfied();
}
Also used : Attribute(javax.management.Attribute) ManagedAttribute(org.springframework.jmx.export.annotation.ManagedAttribute) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 34 with Attribute

use of javax.management.Attribute in project midpoint by Evolveum.

the class SystemInfoPanel method fillCpuUsage.

private void fillCpuUsage(SystemInfoDto dto) throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
    AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });
    if (list.isEmpty()) {
        dto.cpuUsage = Double.NaN;
        return;
    }
    Attribute att = (Attribute) list.get(0);
    Double value = (Double) att.getValue();
    if (value == -1.0) {
        // usually takes a couple of seconds before we get real values
        dto.cpuUsage = Double.NaN;
        return;
    }
    dto.cpuUsage = ((int) (value * 1000) / 10.0);
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 35 with Attribute

use of javax.management.Attribute in project jdk8u_jdk by JetBrains.

the class RMIDownloadTest method testWithException.

private static void testWithException(boolean send) throws Exception {
    ClassLoader zoobyCL = new ZoobyClassLoader();
    Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);
    Object zooby = zoobyClass.newInstance();
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");
    JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();
    Object rzooby;
    if (send) {
        System.out.println("Sending object...");
        mbsc.setAttribute(getSetName, new Attribute("It", zooby));
        rzooby = getSetInstance.getIt();
    } else {
        System.out.println("Receiving object...");
        getSetInstance.setIt(zooby);
        rzooby = mbsc.getAttribute(getSetName, "It");
    }
    if (!rzooby.getClass().getName().equals("Zooby")) {
        throw new Exception("FAILED: remote object is not a Zooby");
    }
    if (rzooby.getClass().getClassLoader() == zooby.getClass().getClassLoader()) {
        throw new Exception("FAILED: same class loader: " + zooby.getClass().getClassLoader());
    }
    cc.close();
    cs.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Attribute(javax.management.Attribute) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Aggregations

Attribute (javax.management.Attribute)157 ObjectName (javax.management.ObjectName)94 MBeanServer (javax.management.MBeanServer)56 AttributeList (javax.management.AttributeList)46 Test (org.junit.Test)38 ReflectionException (javax.management.ReflectionException)29 MBeanException (javax.management.MBeanException)25 HashMap (java.util.HashMap)23 InstanceNotFoundException (javax.management.InstanceNotFoundException)22 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)21 AttributeNotFoundException (javax.management.AttributeNotFoundException)20 MBeanServerConnection (javax.management.MBeanServerConnection)15 MBeanInfo (javax.management.MBeanInfo)14 JMXConnector (javax.management.remote.JMXConnector)13 RuntimeOperationsException (javax.management.RuntimeOperationsException)12 List (java.util.List)10 BacklogTracerEventMessage (org.apache.camel.api.management.mbean.BacklogTracerEventMessage)10 IOException (java.io.IOException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ArrayList (java.util.ArrayList)9