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));
}
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));
}
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();
}
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);
}
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();
}
Aggregations