@Override public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { mbeanServer.setAttribute(name, attribute); }
@Override public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { mbeanServer.setAttribute(name, attribute); }
@Override public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { mbeanServer.setAttribute(name, attribute); }
@Override public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { mbeanServer.setAttribute(name, attribute); }
/** {@inheritDoc} */ @Override public void setAttribute(ObjectName name, Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { origin.setAttribute(name, attribute); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testRegisterNotificationListenerForMBean() 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(); Map notificationListeners = new HashMap(); notificationListeners.put(objectName, listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(notificationListeners); start(exporter); // update the attribute String attributeName = "Name"; server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop")); assertEquals("Listener not notified", 1, listener.getCount(attributeName)); }
@Test public void testSetAttribute() throws Exception { ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); getServer().setAttribute(objectName, new Attribute(NAME_ATTRIBUTE, "Rob Harrop")); IJmxTestBean bean = (IJmxTestBean) getContext().getBean("testBean"); assertEquals("Rob Harrop", bean.getName()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithWildcard() 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(); Map notificationListeners = new HashMap(); notificationListeners.put("*", listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(notificationListeners); start(exporter); // update the attribute String attributeName = "Name"; server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop")); assertEquals("Listener not notified", 1, listener.getCount(attributeName)); }
@Test public void testWithExposeClassLoader() throws Exception { String name = "Rob Harrop"; String otherName = "Juergen Hoeller"; JmxTestBean bean = new JmxTestBean(); bean.setName(name); ObjectName objectName = ObjectNameManager.getInstance("spring:type=Test"); Map<String, Object> beans = new HashMap<>(); beans.put(objectName.toString(), bean); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.setBeans(beans); exporter.setExposeManagedResourceClassLoader(true); start(exporter); assertIsRegistered("Bean instance not registered", objectName); Object result = server.invoke(objectName, "add", new Object[] {new Integer(2), new Integer(3)}, new String[] { int.class.getName(), int.class.getName()}); assertEquals("Incorrect result return from add", result, new Integer(5)); assertEquals("Incorrect attribute value", name, server.getAttribute(objectName, "Name")); server.setAttribute(objectName, new Attribute("Name", otherName)); assertEquals("Incorrect updated name.", otherName, bean.getName()); }
String ageAttribute = "Age"; server.setAttribute(objectName, new Attribute(nameAttribute, "Rob Harrop")); server.setAttribute(objectName, new Attribute(ageAttribute, new Integer(90)));
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedToSameBeanInstance() throws Exception { String beanName = "testBean"; ObjectName objectName = ObjectName.getInstance("spring:name=Test"); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerSingleton(beanName, testBean); Map<String, Object> beans = new HashMap<>(); beans.put(beanName, testBean); Map listenerMappings = new HashMap(); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); listenerMappings.put(beanName, listener); listenerMappings.put(objectName, listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(listenerMappings); exporter.setBeanFactory(factory); start(exporter); assertIsRegistered("Should have registered MBean", objectName); server.setAttribute(objectName, new Attribute("Age", new Integer(77))); assertEquals("Listener should have been notified exactly once", 1, listener.getCount("Age")); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedToSameBeanInstance() throws Exception { String beanName = "testBean"; ObjectName objectName = ObjectName.getInstance("spring:name=Test"); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerSingleton(beanName, testBean); Map<String, Object> beans = new HashMap<>(); beans.put(beanName, testBean); Map listenerMappings = new HashMap(); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); listenerMappings.put(objectName, listener); listenerMappings.put(beanName, listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(listenerMappings); exporter.setBeanFactory(factory); start(exporter); assertIsRegistered("Should have registered MBean", objectName); server.setAttribute(objectName, new Attribute("Age", new Integer(77))); assertEquals("Listener should have been notified exactly once", 1, listener.getCount("Age")); }
@Test public void testRegisterNotificationListenerForAllMBeans() 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); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean }); start(exporter); // update the attribute String attributeName = "Name"; server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop")); assertEquals("Listener not notified", 1, listener.getCount(attributeName)); }
@Test public void testSetAttribute() throws Exception { try { mBeanServer.setAttribute(objectName(countMetricName), new Attribute("anything", 1)); fail("setAttribute should have failed"); } catch (RuntimeMBeanException e) { assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class)); } }
@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("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)); }
assertIsRegistered("Should have registered MBean", objectName2); server.setAttribute(ObjectNameManager.getInstance(objectName1), new Attribute("Age", new Integer(77))); assertEquals("Listener not notified for testBean1", 1, listener.getCount("Age")); server.setAttribute(ObjectNameManager.getInstance(objectName2), new Attribute("Age", new Integer(33))); assertEquals("Listener not notified for testBean2", 2, listener.getCount("Age"));
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap() throws Exception { String beanName = "testBean"; ObjectName objectName = ObjectName.getInstance("spring:name=Test"); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerSingleton(beanName, testBean); Map<String, Object> beans = new HashMap<>(); beans.put(beanName, beanName); Map listenerMappings = new HashMap(); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); listenerMappings.put(beanName, listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(listenerMappings); exporter.setBeanFactory(factory); start(exporter); assertIsRegistered("Should have registered MBean", objectName); server.setAttribute(objectName, new Attribute("Age", new Integer(77))); assertEquals("Listener not notified", 1, listener.getCount("Age")); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMap() throws Exception { String beanName = "testBean"; ObjectName objectName = ObjectName.getInstance("spring:name=Test"); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerSingleton(beanName, testBean); Map<String, Object> beans = new HashMap<>(); beans.put(beanName, testBean); Map listenerMappings = new HashMap(); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); listenerMappings.put(beanName, listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(listenerMappings); exporter.setBeanFactory(factory); start(exporter); assertIsRegistered("Should have registered MBean", objectName); server.setAttribute(objectName, new Attribute("Age", new Integer(77))); assertEquals("Listener not notified", 1, listener.getCount("Age")); }
@Test public void testNotificationListenerRegistrar() throws Exception { ObjectName objectName = ObjectName.getInstance("spring:name=Test"); JmxTestBean bean = new JmxTestBean(); Map<String, Object> beans = new HashMap<>(); beans.put(objectName.getCanonicalName(), bean); 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.setMappedObjectName(objectName); 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)); }
@Test public void testRegisterNotificationListenerWithHandback() throws Exception { String objectName = "spring:name=Test"; JmxTestBean bean = new JmxTestBean(); Map<String, Object> beans = new HashMap<>(); beans.put(objectName, bean); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); Object handback = new Object(); NotificationListenerBean listenerBean = new NotificationListenerBean(); listenerBean.setNotificationListener(listener); listenerBean.setMappedObjectName("spring:name=Test"); listenerBean.setHandback(handback); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean }); start(exporter); // update the attribute String attributeName = "Name"; server.setAttribute(ObjectNameManager.getInstance("spring:name=Test"), new Attribute(attributeName, "Rob Harrop")); assertEquals("Listener not notified", 1, listener.getCount(attributeName)); assertEquals("Handback object not transmitted correctly", handback, listener.getLastHandback(attributeName)); }