org.springframework.jmx.support.ObjectNameManager Java Examples

The following examples show how to use org.springframework.jmx.support.ObjectNameManager. 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 vote down vote up
@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 spring-analysis-note with MIT License 6 votes vote down vote up
@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 spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutodetectWithExclude() throws Exception {
	ConfigurableApplicationContext ctx = load("autodetectMBeans.xml");
	try {
		ctx.getBean("exporter");
		MBeanServer server = ctx.getBean("server", MBeanServer.class);
		ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
		assertNotNull(instance);

		assertThatExceptionOfType(InstanceNotFoundException.class).isThrownBy(() ->
				server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=false")));
	}
	finally {
		ctx.close();
	}
}
 
Example #4
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #5
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutodetectMBeans() throws Exception {
	ConfigurableApplicationContext ctx = load("autodetectMBeans.xml");
	try {
		ctx.getBean("exporter");
		MBeanServer server = ctx.getBean("server", MBeanServer.class);
		ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
		assertNotNull(instance);
		instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean2=true"));
		assertNotNull(instance);
		instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean3=true"));
		assertNotNull(instance);
	}
	finally {
		ctx.close();
	}
}
 
Example #6
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testUserCreatedMBeanRegWithDynamicMBean() throws Exception {
	Map<String, Object> map = new HashMap<>();
	map.put("spring:name=dynBean", new TestDynamicMBean());

	InvokeDetectAssembler asm = new InvokeDetectAssembler();

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(server);
	exporter.setBeans(map);
	exporter.setAssembler(asm);

	try {
		start(exporter);
		Object name = server.getAttribute(ObjectNameManager.getInstance("spring:name=dynBean"), "Name");
		assertEquals("The name attribute is incorrect", "Rob Harrop", name);
		assertFalse("Assembler should not have been invoked", asm.invoked);
	}
	finally {
		exporter.destroy();
	}
}
 
Example #7
Source File: MBeanExporterOperationsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #8
Source File: NotificationPublisherTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLazyInit() throws Exception {
	// start the MBeanExporter
	ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherLazyTests.xml");
	assertFalse("Should not have instantiated the bean yet", ctx.getBeanFactory().containsSingleton("publisher"));

	// need to touch the MBean proxy
	server.getAttribute(ObjectNameManager.getInstance("spring:type=Publisher"), "Name");
	this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher"), listener, null,
			null);

	MyNotificationPublisher publisher = (MyNotificationPublisher) ctx.getBean("publisher");
	assertNotNull("NotificationPublisher should not be null", publisher.getNotificationPublisher());
	publisher.sendNotification();
	assertEquals("Notification not sent", 1, listener.count);
}
 
Example #9
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testUserCreatedMBeanRegWithDynamicMBean() throws Exception {
	Map<String, Object> map = new HashMap<>();
	map.put("spring:name=dynBean", new TestDynamicMBean());

	InvokeDetectAssembler asm = new InvokeDetectAssembler();

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(server);
	exporter.setBeans(map);
	exporter.setAssembler(asm);

	try {
		start(exporter);
		Object name = server.getAttribute(ObjectNameManager.getInstance("spring:name=dynBean"), "Name");
		assertEquals("The name attribute is incorrect", "Rob Harrop", name);
		assertFalse("Assembler should not have been invoked", asm.invoked);
	}
	finally {
		exporter.destroy();
	}
}
 
Example #10
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutodetectMBeans() throws Exception {
	ConfigurableApplicationContext ctx = load("autodetectMBeans.xml");
	try {
		ctx.getBean("exporter");
		MBeanServer server = ctx.getBean("server", MBeanServer.class);
		ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
		assertNotNull(instance);
		instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean2=true"));
		assertNotNull(instance);
		instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean3=true"));
		assertNotNull(instance);
	}
	finally {
		ctx.close();
	}
}
 
Example #11
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testOnlyBonaFideMBeanIsExportedWhenAutodetectIsMBeanOnly() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());
	String exportedBeanName = "spring:type=TestBean";
	factory.registerSingleton(exportedBeanName, new TestBean());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(exportedBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_MBEAN);
	start(exporter);

	assertIsRegistered("Bona fide MBean not autodetected in AUTODETECT_MBEAN mode",
			ObjectNameManager.getInstance(OBJECT_NAME));
	assertIsNotRegistered("Bean autodetected and (only) AUTODETECT_MBEAN mode is on",
			ObjectNameManager.getInstance(exportedBeanName));
}
 
Example #12
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBonaFideMBeanAndRegularBeanExporterWithAutodetectAll() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());
	String exportedBeanName = "spring:type=TestBean";
	factory.registerSingleton(exportedBeanName, new TestBean());
	String notToBeExportedBeanName = "spring:type=NotToBeExported";
	factory.registerSingleton(notToBeExportedBeanName, new TestBean());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(exportedBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL);
	start(exporter);
	assertIsRegistered("Bona fide MBean not autodetected in (AUTODETECT_ALL) mode",
			ObjectNameManager.getInstance(OBJECT_NAME));
	assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode",
			ObjectNameManager.getInstance(exportedBeanName));
	assertIsNotRegistered("Bean autodetected and did not satisfy the autodetect info assembler",
			ObjectNameManager.getInstance(notToBeExportedBeanName));
}
 
Example #13
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Want to ensure that said MBean is not exported twice.
 */
@Test
public void testBonaFideMBeanExplicitlyExportedAndAutodetectionIsOn() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	Map<String, Object> beansToExport = new HashMap<>();
	beansToExport.put(OBJECT_NAME, OBJECT_NAME);
	exporter.setBeans(beansToExport);
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(OBJECT_NAME));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ASSEMBLER);
	start(exporter);
	assertIsRegistered("Explicitly exported bona fide MBean obviously not exported.",
			ObjectNameManager.getInstance(OBJECT_NAME));
}
 
Example #14
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testIgnoreBeanName() throws MalformedObjectNameException {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	String firstBeanName = "spring:type=TestBean";
	factory.registerSingleton(firstBeanName, new TestBean("test"));
	String secondBeanName = "spring:type=TestBean2";
	factory.registerSingleton(secondBeanName, new TestBean("test2"));

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(firstBeanName, secondBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL);
	exporter.addExcludedBean(secondBeanName);

	start(exporter);
	assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode",
			ObjectNameManager.getInstance(firstBeanName));
	assertIsNotRegistered("Bean should have been excluded",
			ObjectNameManager.getInstance(secondBeanName));
}
 
Example #15
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #16
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testOnlyBonaFideMBeanIsExportedWhenAutodetectIsMBeanOnly() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());
	String exportedBeanName = "spring:type=TestBean";
	factory.registerSingleton(exportedBeanName, new TestBean());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(exportedBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_MBEAN);
	start(exporter);

	assertIsRegistered("Bona fide MBean not autodetected in AUTODETECT_MBEAN mode",
			ObjectNameManager.getInstance(OBJECT_NAME));
	assertIsNotRegistered("Bean autodetected and (only) AUTODETECT_MBEAN mode is on",
			ObjectNameManager.getInstance(exportedBeanName));
}
 
Example #17
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBonaFideMBeanAndRegularBeanExporterWithAutodetectAll() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());
	String exportedBeanName = "spring:type=TestBean";
	factory.registerSingleton(exportedBeanName, new TestBean());
	String notToBeExportedBeanName = "spring:type=NotToBeExported";
	factory.registerSingleton(notToBeExportedBeanName, new TestBean());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(exportedBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL);
	start(exporter);
	assertIsRegistered("Bona fide MBean not autodetected in (AUTODETECT_ALL) mode",
			ObjectNameManager.getInstance(OBJECT_NAME));
	assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode",
			ObjectNameManager.getInstance(exportedBeanName));
	assertIsNotRegistered("Bean autodetected and did not satisfy the autodetect info assembler",
			ObjectNameManager.getInstance(notToBeExportedBeanName));
}
 
Example #18
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #19
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutodetectWithExclude() throws Exception {
	ConfigurableApplicationContext ctx = load("autodetectMBeans.xml");
	try {
		ctx.getBean("exporter");
		MBeanServer server = ctx.getBean("server", MBeanServer.class);
		ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
		assertNotNull(instance);

		thrown.expect(InstanceNotFoundException.class);
		server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=false"));
	}
	finally {
		ctx.close();
	}
}
 
Example #20
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testIgnoreBeanName() throws MalformedObjectNameException {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	String firstBeanName = "spring:type=TestBean";
	factory.registerSingleton(firstBeanName, new TestBean("test"));
	String secondBeanName = "spring:type=TestBean2";
	factory.registerSingleton(secondBeanName, new TestBean("test2"));

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(firstBeanName, secondBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL);
	exporter.addExcludedBean(secondBeanName);

	start(exporter);
	assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode",
			ObjectNameManager.getInstance(firstBeanName));
	assertIsNotRegistered("Bean should have been excluded",
			ObjectNameManager.getInstance(secondBeanName));
}
 
Example #21
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-2158
public void testMBeanIsNotUnregisteredSpuriouslyIfSomeExternalProcessHasUnregisteredMBean() throws Exception {
	MBeanExporter exporter = new MBeanExporter();
	exporter.setBeans(getBeanMap());
	exporter.setServer(this.server);
	MockMBeanExporterListener listener = new MockMBeanExporterListener();
	exporter.setListeners(listener);
	start(exporter);
	assertIsRegistered("The bean was not registered with the MBeanServer",
			ObjectNameManager.getInstance(OBJECT_NAME));

	this.server.unregisterMBean(new ObjectName(OBJECT_NAME));
	exporter.destroy();
	assertEquals("Listener should not have been invoked (MBean previously unregistered by external agent)", 0,
			listener.getUnregistered().size());
}
 
Example #22
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-2158
public void testMBeanIsNotUnregisteredSpuriouslyIfSomeExternalProcessHasUnregisteredMBean() throws Exception {
	MBeanExporter exporter = new MBeanExporter();
	exporter.setBeans(getBeanMap());
	exporter.setServer(this.server);
	MockMBeanExporterListener listener = new MockMBeanExporterListener();
	exporter.setListeners(listener);
	start(exporter);
	assertIsRegistered("The bean was not registered with the MBeanServer",
			ObjectNameManager.getInstance(OBJECT_NAME));

	this.server.unregisterMBean(new ObjectName(OBJECT_NAME));
	exporter.destroy();
	assertEquals("Listener should not have been invoked (MBean previously unregistered by external agent)", 0,
			listener.getUnregistered().size());
}
 
Example #23
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBonaFideMBeanIsNotExportedWithAutodetectAssembler() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());
	String exportedBeanName = "spring:type=TestBean";
	factory.registerSingleton(exportedBeanName, new TestBean());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(exportedBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ASSEMBLER);
	start(exporter);
	assertIsNotRegistered("Bona fide MBean was autodetected in AUTODETECT_ASSEMBLER mode - must not have been",
			ObjectNameManager.getInstance(OBJECT_NAME));
	assertIsRegistered("Bean not autodetected in AUTODETECT_ASSEMBLER mode",
			ObjectNameManager.getInstance(exportedBeanName));
}
 
Example #24
Source File: NotificationPublisherTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSimpleBean() throws Exception {
	// start the MBeanExporter
	ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml");
	this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher"), listener, null,
			null);

	MyNotificationPublisher publisher = (MyNotificationPublisher) ctx.getBean("publisher");
	assertNotNull("NotificationPublisher should not be null", publisher.getNotificationPublisher());
	publisher.sendNotification();
	assertEquals("Notification not sent", 1, listener.count);
}
 
Example #25
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testRegisterIgnoreExisting() 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");

	String objectName2 = "spring:test=equalBean";

	Map<String, Object> beans = new HashMap<>();
	beans.put(objectName.toString(), springRegistered);
	beans.put(objectName2, springRegistered);

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(server);
	exporter.setBeans(beans);
	exporter.setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING);

	start(exporter);

	ObjectInstance instance = server.getObjectInstance(objectName);
	assertNotNull(instance);
	ObjectInstance instance2 = server.getObjectInstance(new ObjectName(objectName2));
	assertNotNull(instance2);

	// should still be the first bean with name Rob Harrop
	assertEquals("Rob Harrop", server.getAttribute(objectName, "Name"));
}
 
Example #26
Source File: KeyNamingStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 #27
Source File: MetadataNamingStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 #28
Source File: AnnotationLazyInitMBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #29
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testWithSuppliedMBeanServer() throws Exception {
	MBeanExporter exporter = new MBeanExporter();
	exporter.setBeans(getBeanMap());
	exporter.setServer(server);
	try {
		start(exporter);
		assertIsRegistered("The bean was not registered with the MBeanServer",
				ObjectNameManager.getInstance(OBJECT_NAME));
	}
	finally {
		exporter.destroy();
	}
}
 
Example #30
Source File: AnnotationLazyInitMBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
	}
}