org.springframework.jmx.JmxTestBean Java Examples

The following examples show how to use org.springframework.jmx.JmxTestBean. 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: NotificationListenerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
}
 
Example #2
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testWildcardCanBeUsedInNotificationListenersMap() throws Exception {
	String beanName = "charlesDexterWard";
	BeanDefinitionBuilder testBean = BeanDefinitionBuilder.rootBeanDefinition(JmxTestBean.class);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(beanName, testBean.getBeanDefinition());
	factory.preInstantiateSingletons();
	Object testBeanInstance = factory.getBean(beanName);

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	Map<String, Object> beansToExport = new HashMap<>();
	beansToExport.put("test:what=ever", testBeanInstance);
	exporter.setBeans(beansToExport);
	exporter.setBeanFactory(factory);
	StubNotificationListener listener = new StubNotificationListener();
	exporter.setNotificationListenerMappings(Collections.singletonMap("*", listener));

	start(exporter);
}
 
Example #3
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-3302
public void testBeanNameCanBeUsedInNotificationListenersMap() throws Exception {
	String beanName = "charlesDexterWard";
	BeanDefinitionBuilder testBean = BeanDefinitionBuilder.rootBeanDefinition(JmxTestBean.class);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(beanName, testBean.getBeanDefinition());
	factory.preInstantiateSingletons();
	Object testBeanInstance = factory.getBean(beanName);

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	Map<String, Object> beansToExport = new HashMap<>();
	beansToExport.put("test:what=ever", testBeanInstance);
	exporter.setBeans(beansToExport);
	exporter.setBeanFactory(factory);
	StubNotificationListener listener = new StubNotificationListener();
	exporter.setNotificationListenerMappings(Collections.singletonMap(beanName, listener));

	start(exporter);
}
 
Example #4
Source File: NotificationListenerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegisterNotificationListenerForAllMBeans() throws Exception {
	ObjectName objectName = ObjectName.getInstance("spring:name=Test");
	JmxTestBean bean = new JmxTestBean();

	Map<String, Object> beans = new HashMap<String, Object>();
	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));
}
 
Example #5
Source File: MBeanExporterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #6
Source File: MBeanExporterOperationsTests.java    From spring4-understanding with Apache License 2.0 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 #7
Source File: NotificationListenerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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<String, Object>();
	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));
}
 
Example #8
Source File: NotificationListenerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example #9
Source File: NotificationListenerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example #10
Source File: NotificationListenerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example #11
Source File: NotificationListenerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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<String, Object>();
	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));
}
 
Example #12
Source File: MBeanExporterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * SPR-3302
 */
@Test
public void testBeanNameCanBeUsedInNotificationListenersMap() throws Exception {
	String beanName = "charlesDexterWard";
	BeanDefinitionBuilder testBean = BeanDefinitionBuilder.rootBeanDefinition(JmxTestBean.class);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(beanName, testBean.getBeanDefinition());
	factory.preInstantiateSingletons();
	Object testBeanInstance = factory.getBean(beanName);

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	Map<String, Object> beansToExport = new HashMap<String, Object>();
	beansToExport.put("test:what=ever", testBeanInstance);
	exporter.setBeans(beansToExport);
	exporter.setBeanFactory(factory);
	StubNotificationListener listener = new StubNotificationListener();
	exporter.setNotificationListenerMappings(Collections.singletonMap(beanName, listener));

	start(exporter);
}
 
Example #13
Source File: MBeanExporterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testWildcardCanBeUsedInNotificationListenersMap() throws Exception {
	String beanName = "charlesDexterWard";
	BeanDefinitionBuilder testBean = BeanDefinitionBuilder.rootBeanDefinition(JmxTestBean.class);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(beanName, testBean.getBeanDefinition());
	factory.preInstantiateSingletons();
	Object testBeanInstance = factory.getBean(beanName);

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	Map<String, Object> beansToExport = new HashMap<String, Object>();
	beansToExport.put("test:what=ever", testBeanInstance);
	exporter.setBeans(beansToExport);
	exporter.setBeanFactory(factory);
	StubNotificationListener listener = new StubNotificationListener();
	exporter.setNotificationListenerMappings(Collections.singletonMap("*", listener));

	start(exporter);
}
 
Example #14
Source File: NotificationListenerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
}
 
Example #15
Source File: NotificationListenerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
}
 
Example #16
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 #17
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testWildcardCanBeUsedInNotificationListenersMap() throws Exception {
	String beanName = "charlesDexterWard";
	BeanDefinitionBuilder testBean = BeanDefinitionBuilder.rootBeanDefinition(JmxTestBean.class);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(beanName, testBean.getBeanDefinition());
	factory.preInstantiateSingletons();
	Object testBeanInstance = factory.getBean(beanName);

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	Map<String, Object> beansToExport = new HashMap<>();
	beansToExport.put("test:what=ever", testBeanInstance);
	exporter.setBeans(beansToExport);
	exporter.setBeanFactory(factory);
	StubNotificationListener listener = new StubNotificationListener();
	exporter.setNotificationListenerMappings(Collections.singletonMap("*", listener));

	start(exporter);
}
 
Example #18
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-3302
public void testBeanNameCanBeUsedInNotificationListenersMap() throws Exception {
	String beanName = "charlesDexterWard";
	BeanDefinitionBuilder testBean = BeanDefinitionBuilder.rootBeanDefinition(JmxTestBean.class);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(beanName, testBean.getBeanDefinition());
	factory.preInstantiateSingletons();
	Object testBeanInstance = factory.getBean(beanName);

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	Map<String, Object> beansToExport = new HashMap<>();
	beansToExport.put("test:what=ever", testBeanInstance);
	exporter.setBeans(beansToExport);
	exporter.setBeanFactory(factory);
	StubNotificationListener listener = new StubNotificationListener();
	exporter.setNotificationListenerMappings(Collections.singletonMap(beanName, listener));

	start(exporter);
}
 
Example #19
Source File: MBeanExporterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #20
Source File: MBeanExporterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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<String, Object>();
	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);
}
 
Example #21
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 #22
Source File: NotificationListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
}
 
Example #23
Source File: NotificationListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
}
 
Example #24
Source File: MBeanExporterOperationsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testRegisterManagedResourceWithUserSuppliedObjectName() throws Exception {
	ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");

	JmxTestBean bean = new JmxTestBean();
	bean.setName("Rob Harrop");

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.registerManagedResource(bean, objectName);

	String name = (String) getServer().getAttribute(objectName, "Name");
	assertEquals("Incorrect name on MBean", name, bean.getName());
}
 
Example #25
Source File: MBeanExporterOperationsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterManagedResourceWithUserSuppliedObjectName() throws Exception {
	ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");

	JmxTestBean bean = new JmxTestBean();
	bean.setName("Rob Harrop");

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.registerManagedResource(bean, objectName);

	String name = (String) getServer().getAttribute(objectName, "Name");
	assertEquals("Incorrect name on MBean", name, bean.getName());
}
 
Example #26
Source File: MBeanExporterOperationsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #27
Source File: MBeanExporterOperationsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #28
Source File: AbstractAutodetectTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void autodetect() throws Exception {
	JmxTestBean bean = new JmxTestBean();

	AutodetectCapableMBeanInfoAssembler assembler = getAssembler();
	assertTrue("The bean should be included", assembler.includeBean(bean.getClass(), "testBean"));
}
 
Example #29
Source File: NotificationListenerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegisterNotificationListenerWithHandback() throws Exception {
	String objectName = "spring:name=Test";
	JmxTestBean bean = new JmxTestBean();

	Map<String, Object> beans = new HashMap<String, Object>();
	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));
}
 
Example #30
Source File: MethodExclusionMBeanInfoAssemblerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testIsNotIgnoredDoesntIgnoreUnspecifiedBeanMethods() throws Exception {
	final String beanKey = "myTestBean";
	MethodExclusionMBeanInfoAssembler assembler = new MethodExclusionMBeanInfoAssembler();
	Properties ignored = new Properties();
	ignored.setProperty(beanKey, "dontExposeMe,setSuperman");
	assembler.setIgnoredMethodMappings(ignored);
	Method method = JmxTestBean.class.getMethod("dontExposeMe");
	assertFalse(assembler.isNotIgnored(method, beanKey));
	// this bean does not have any ignored methods on it, so must obviously not be ignored...
	assertTrue(assembler.isNotIgnored(method, "someOtherBeanKey"));
}