/** * Retrieve the {@code ObjectName} instance corresponding to the supplied name. * @param objectName the {@code ObjectName} in {@code String} format * @return the {@code ObjectName} instance * @throws MalformedObjectNameException in case of an invalid object name specification * @see ObjectName#ObjectName(String) * @see ObjectName#getInstance(String) */ public static ObjectName getInstance(String objectName) throws MalformedObjectNameException { return ObjectName.getInstance(objectName); }
/** * Retrieve an {@code ObjectName} instance with the specified domain name * and the supplied key/name properties. * @param domainName the domain name for the {@code ObjectName} * @param properties the properties for the {@code ObjectName} * @return the {@code ObjectName} instance * @throws MalformedObjectNameException in case of an invalid object name specification * @see ObjectName#ObjectName(String, java.util.Hashtable) * @see ObjectName#getInstance(String, java.util.Hashtable) */ public static ObjectName getInstance(String domainName, Hashtable<String, String> properties) throws MalformedObjectNameException { return ObjectName.getInstance(domainName, properties); }
/** * Retrieve an {@code ObjectName} instance for the specified domain and a * single property with the supplied key and value. * @param domainName the domain name for the {@code ObjectName} * @param key the key for the single property in the {@code ObjectName} * @param value the value for the single property in the {@code ObjectName} * @return the {@code ObjectName} instance * @throws MalformedObjectNameException in case of an invalid object name specification * @see ObjectName#ObjectName(String, String, String) * @see ObjectName#getInstance(String, String, String) */ public static ObjectName getInstance(String domainName, String key, String value) throws MalformedObjectNameException { return ObjectName.getInstance(domainName, key, value); }
/** * Retrieve the {@code ObjectName} instance corresponding to the supplied name. * @param objectName the {@code ObjectName} in {@code String} format * @return the {@code ObjectName} instance * @throws MalformedObjectNameException in case of an invalid object name specification * @see ObjectName#ObjectName(String) * @see ObjectName#getInstance(String) */ public static ObjectName getInstance(String objectName) throws MalformedObjectNameException { return ObjectName.getInstance(objectName); }
/** * Retrieve an {@code ObjectName} instance with the specified domain name * and the supplied key/name properties. * @param domainName the domain name for the {@code ObjectName} * @param properties the properties for the {@code ObjectName} * @return the {@code ObjectName} instance * @throws MalformedObjectNameException in case of an invalid object name specification * @see ObjectName#ObjectName(String, java.util.Hashtable) * @see ObjectName#getInstance(String, java.util.Hashtable) */ public static ObjectName getInstance(String domainName, Hashtable<String, String> properties) throws MalformedObjectNameException { return ObjectName.getInstance(domainName, properties); }
private static ObjectName createObjectName() throws MalformedObjectNameException { return ObjectName.getInstance("foo:type=bar"); }
private static OptionalLong getMaxFileDescriptorCount() { try { MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); Object maxFileDescriptorCount = mbeanServer.getAttribute(ObjectName.getInstance(OPERATING_SYSTEM_MXBEAN_NAME), "MaxFileDescriptorCount"); return OptionalLong.of(((Number) maxFileDescriptorCount).longValue()); } catch (Exception e) { return OptionalLong.empty(); } }
@Test public void mBeanExportAlsoWorks() throws Exception { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); assertEquals("done", server.invoke(ObjectName.getInstance("test:type=TestBean"), "doSomething", new Object[0], new String[0])); }
@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)); }
@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)); }
@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")); }
@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 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 testExportJdkProxy() throws Exception { JmxTestBean bean = new JmxTestBean(); bean.setName("Rob Harrop"); ProxyFactory factory = new ProxyFactory(); factory.setTarget(bean); factory.addAdvice(new NopInterceptor()); factory.setInterfaces(IJmxTestBean.class); IJmxTestBean proxy = (IJmxTestBean) factory.getProxy(); String name = "bean:mmm=whatever"; Map<String, Object> beans = new HashMap<>(); beans.put(name, proxy); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.registerBeans(); ObjectName oname = ObjectName.getInstance(name); Object nameValue = server.getAttribute(oname, "Name"); assertEquals("Rob Harrop", nameValue); }
@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)); }
@Test public void registerWithMBeanServer() throws Exception { //Added a brief snooze here - seems to fix occasional //java.net.BindException: Address already in use errors Thread.sleep(1); ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); bean.setObjectName(OBJECT_NAME); bean.afterPropertiesSet(); try { // Try to get the connector bean. ObjectInstance instance = getServer().getObjectInstance(ObjectName.getInstance(OBJECT_NAME)); assertNotNull("ObjectInstance should not be null", instance); } finally { bean.destroy(); } }
@Test public void noRegisterWithMBeanServer() throws Exception { ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean(); bean.afterPropertiesSet(); try { // Try to get the connector bean. getServer().getObjectInstance(ObjectName.getInstance(OBJECT_NAME)); fail("Instance should not be found"); } catch (InstanceNotFoundException ex) { // expected } finally { bean.destroy(); } }
@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)); }