Java Code Examples for org.springframework.jmx.support.ObjectNameManager#getInstance()
The following examples show how to use
org.springframework.jmx.support.ObjectNameManager#getInstance() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MBeanExporterOperationsTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testRegisterManagedResourceWithGeneratedObjectName() throws Exception { final ObjectName objectNameTemplate = ObjectNameManager.getInstance("spring:type=Test"); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.setNamingStrategy(new ObjectNamingStrategy() { @Override public ObjectName getObjectName(Object managedBean, String beanKey) { return objectNameTemplate; } }); JmxTestBean bean1 = new JmxTestBean(); JmxTestBean bean2 = new JmxTestBean(); ObjectName reg1 = exporter.registerManagedResource(bean1); ObjectName reg2 = exporter.registerManagedResource(bean2); assertIsRegistered("Bean 1 not registered with MBeanServer", reg1); assertIsRegistered("Bean 2 not registered with MBeanServer", reg2); assertObjectNameMatchesTemplate(objectNameTemplate, reg1); assertObjectNameMatchesTemplate(objectNameTemplate, reg2); }
Example 2
Source File: MBeanExporterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testSelfNaming() throws Exception { ObjectName objectName = ObjectNameManager.getInstance(OBJECT_NAME); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); Map<String, Object> beans = new HashMap<>(); beans.put("foo", testBean); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); start(exporter); ObjectInstance instance = server.getObjectInstance(objectName); assertNotNull(instance); }
Example 3
Source File: MBeanExporterTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testAutodetectLazyMBeans() throws Exception { ConfigurableApplicationContext ctx = load("autodetectLazyMBeans.xml"); try { ctx.getBean("exporter"); MBeanServer server = ctx.getBean("server", MBeanServer.class); ObjectName oname = ObjectNameManager.getInstance("spring:mbean=true"); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "Rob Harrop", name); oname = ObjectNameManager.getInstance("spring:mbean=another"); assertNotNull(server.getObjectInstance(oname)); name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "Juergen Hoeller", name); } finally { ctx.close(); } }
Example 4
Source File: MBeanExporterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testSelfNaming() throws Exception { ObjectName objectName = ObjectNameManager.getInstance(OBJECT_NAME); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); Map<String, Object> beans = new HashMap<>(); beans.put("foo", testBean); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); start(exporter); ObjectInstance instance = server.getObjectInstance(objectName); assertNotNull(instance); }
Example 5
Source File: MBeanExporterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testAutodetectLazyMBeans() throws Exception { ConfigurableApplicationContext ctx = load("autodetectLazyMBeans.xml"); try { ctx.getBean("exporter"); MBeanServer server = ctx.getBean("server", MBeanServer.class); ObjectName oname = ObjectNameManager.getInstance("spring:mbean=true"); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "Rob Harrop", name); oname = ObjectNameManager.getInstance("spring:mbean=another"); assertNotNull(server.getObjectInstance(oname)); name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "Juergen Hoeller", name); } finally { ctx.close(); } }
Example 6
Source File: AnnotationLazyInitMBeanTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void lazyAssembling() throws Exception { System.setProperty("domain", "bean"); ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/annotation/lazyAssembling.xml"); try { MBeanServer server = (MBeanServer) ctx.getBean("server"); ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4"); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "TEST", name); oname = ObjectNameManager.getInstance("bean:name=testBean5"); assertNotNull(server.getObjectInstance(oname)); name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "FACTORY", name); oname = ObjectNameManager.getInstance("spring:mbean=true"); assertNotNull(server.getObjectInstance(oname)); name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "Rob Harrop", name); oname = ObjectNameManager.getInstance("spring:mbean=another"); assertNotNull(server.getObjectInstance(oname)); name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "Juergen Hoeller", name); } finally { System.clearProperty("domain"); ctx.close(); } }
Example 7
Source File: AnnotationLazyInitMBeanTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void componentScan() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/annotation/componentScan.xml"); try { MBeanServer server = (MBeanServer) ctx.getBean("server"); ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4"); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertNull(name); } finally { ctx.close(); } }
Example 8
Source File: AnnotationLazyInitMBeanTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void lazyNaming() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/annotation/lazyNaming.xml"); try { MBeanServer server = (MBeanServer) ctx.getBean("server"); ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4"); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "TEST", name); } finally { ctx.close(); } }
Example 9
Source File: MetadataNamingStrategy.java From spring-analysis-note with MIT License | 5 votes |
/** * Reads the {@code ObjectName} from the source-level metadata associated * with the managed resource's {@code Class}. */ @Override public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException { Assert.state(this.attributeSource != null, "No JmxAttributeSource set"); Class<?> managedClass = AopUtils.getTargetClass(managedBean); ManagedResource mr = this.attributeSource.getManagedResource(managedClass); // Check that an object name has been specified. if (mr != null && StringUtils.hasText(mr.getObjectName())) { return ObjectNameManager.getInstance(mr.getObjectName()); } else { Assert.state(beanKey != null, "No ManagedResource attribute and no bean key specified"); try { return ObjectNameManager.getInstance(beanKey); } catch (MalformedObjectNameException ex) { String domain = this.defaultDomain; if (domain == null) { domain = ClassUtils.getPackageName(managedClass); } Hashtable<String, String> properties = new Hashtable<>(); properties.put("type", ClassUtils.getShortName(managedClass)); properties.put("name", beanKey); return ObjectNameManager.getInstance(domain, properties); } } }
Example 10
Source File: MetadataNamingStrategy.java From java-technology-stack with MIT License | 5 votes |
/** * Reads the {@code ObjectName} from the source-level metadata associated * with the managed resource's {@code Class}. */ @Override public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException { Assert.state(this.attributeSource != null, "No JmxAttributeSource set"); Class<?> managedClass = AopUtils.getTargetClass(managedBean); ManagedResource mr = this.attributeSource.getManagedResource(managedClass); // Check that an object name has been specified. if (mr != null && StringUtils.hasText(mr.getObjectName())) { return ObjectNameManager.getInstance(mr.getObjectName()); } else { Assert.state(beanKey != null, "No ManagedResource attribute and no bean key specified"); try { return ObjectNameManager.getInstance(beanKey); } catch (MalformedObjectNameException ex) { String domain = this.defaultDomain; if (domain == null) { domain = ClassUtils.getPackageName(managedClass); } Hashtable<String, String> properties = new Hashtable<>(); properties.put("type", ClassUtils.getShortName(managedClass)); properties.put("name", beanKey); return ObjectNameManager.getInstance(domain, properties); } } }
Example 11
Source File: MBeanExporterTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testRegisterReplaceExisting() throws Exception { ObjectName objectName = ObjectNameManager.getInstance(OBJECT_NAME); Person preRegistered = new Person(); preRegistered.setName("Rob Harrop"); server.registerMBean(preRegistered, objectName); Person springRegistered = new Person(); springRegistered.setName("Sally Greenwood"); Map<String, Object> beans = new HashMap<>(); beans.put(objectName.toString(), springRegistered); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setRegistrationPolicy(RegistrationPolicy.REPLACE_EXISTING); start(exporter); ObjectInstance instance = server.getObjectInstance(objectName); assertNotNull(instance); // should still be the new bean with name Sally Greenwood assertEquals("Sally Greenwood", server.getAttribute(objectName, "Name")); }
Example 12
Source File: MBeanExporterTests.java From spring-analysis-note with MIT License | 5 votes |
@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()); }
Example 13
Source File: KeyNamingStrategy.java From spring-analysis-note with MIT License | 5 votes |
/** * Attempts to retrieve the {@code ObjectName} via the given key, trying to * find a mapped value in the mappings first. */ @Override public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException { Assert.notNull(beanKey, "KeyNamingStrategy requires bean key"); String objectName = null; if (this.mergedMappings != null) { objectName = this.mergedMappings.getProperty(beanKey); } if (objectName == null) { objectName = beanKey; } return ObjectNameManager.getInstance(objectName); }
Example 14
Source File: AbstractJmxAssemblerTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testOperationInvocation() throws Exception{ ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); Object result = getServer().invoke(objectName, "add", new Object[] {new Integer(20), new Integer(30)}, new String[] {"int", "int"}); assertEquals("Incorrect result", new Integer(50), result); }
Example 15
Source File: MBeanExporterOperationsTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception { ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo"); ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null); RequiredModelMBean bean = new RequiredModelMBean(info); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.registerManagedResource(bean, objectName); MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName); assertEquals(info, infoFromServer); }
Example 16
Source File: AbstractJmxAssemblerTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testOperationInvocation() throws Exception{ ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); Object result = getServer().invoke(objectName, "add", new Object[] {new Integer(20), new Integer(30)}, new String[] {"int", "int"}); assertEquals("Incorrect result", new Integer(50), result); }
Example 17
Source File: LazyInitMBeanTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void invokeOnLazyInitBean() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/lazyInit.xml"); assertFalse(ctx.getBeanFactory().containsSingleton("testBean")); assertFalse(ctx.getBeanFactory().containsSingleton("testBean2")); try { MBeanServer server = (MBeanServer) ctx.getBean("server"); ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean2"); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "foo", name); } finally { ctx.close(); } }
Example 18
Source File: MBeanExporterOperationsTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testRegisterManagedResourceWithGeneratedObjectNameWithoutUniqueness() throws Exception { final ObjectName objectNameTemplate = ObjectNameManager.getInstance("spring:type=Test"); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.setEnsureUniqueRuntimeObjectNames(false); exporter.setNamingStrategy(new ObjectNamingStrategy() { @Override public ObjectName getObjectName(Object managedBean, String beanKey) { return objectNameTemplate; } }); JmxTestBean bean1 = new JmxTestBean(); JmxTestBean bean2 = new JmxTestBean(); ObjectName reg1 = exporter.registerManagedResource(bean1); assertIsRegistered("Bean 1 not registered with MBeanServer", reg1); try { exporter.registerManagedResource(bean2); fail("Shouldn't be able to register a runtime MBean with a reused ObjectName."); } catch (MBeanExportException e) { assertEquals("Incorrect root cause", InstanceAlreadyExistsException.class, e.getCause().getClass()); } }
Example 19
Source File: EnableMBeanExportConfigurationTests.java From spring-analysis-note with MIT License | 4 votes |
private void validateMBeanAttribute(MBeanServer server, String objectName, String expected) throws Exception { ObjectName oname = ObjectNameManager.getInstance(objectName); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", expected, name); }
Example 20
Source File: MBeanClientInterceptor.java From java-technology-stack with MIT License | 2 votes |
/** * Set the {@code ObjectName} of the MBean which calls are routed to, * as {@code ObjectName} instance or as {@code String}. */ public void setObjectName(Object objectName) throws MalformedObjectNameException { this.objectName = ObjectNameManager.getInstance(objectName); }