001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management;
010:
011: import java.util.ArrayList;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Set;
015: import javax.management.InstanceNotFoundException;
016: import javax.management.MBeanServerConnection;
017: import javax.management.MBeanServerDelegate;
018: import javax.management.Notification;
019: import javax.management.NotificationListener;
020: import javax.management.ObjectInstance;
021: import javax.management.ObjectName;
022: import javax.management.loading.MLet;
023: import javax.management.timer.Timer;
024:
025: import test.MX4JTestCase;
026: import test.MultiThreadTestRunner;
027:
028: /**
029: * @version $Revision: 1.4 $
030: */
031: public class MultiThreadMBeanServerTest extends MX4JTestCase {
032: protected MBeanServerConnection server;
033:
034: public MultiThreadMBeanServerTest(String s) {
035: super (s);
036: }
037:
038: protected void setUp() throws Exception {
039: server = newMBeanServer();
040: }
041:
042: protected void tearDown() throws Exception {
043: server = null;
044: }
045:
046: public void testAddRemoveNotifyListeners() throws Exception {
047: final ObjectName delegateName = ObjectName
048: .getInstance("JMImplementation:type=MBeanServerDelegate");
049:
050: MultiThreadTestRunner.Test test = new MultiThreadTestRunner.Test() {
051: public void test() throws Exception {
052: final List notifications = new ArrayList();
053: NotificationListener listener = new NotificationListener() {
054: public void handleNotification(
055: Notification notification, Object handback) {
056: synchronized (notifications) {
057: notifications.add(notification);
058: }
059: }
060: };
061:
062: // Add the listener
063: server.addNotificationListener(delegateName, listener,
064: null, null);
065:
066: // Emit a notification
067: ObjectName mletName = ObjectName.getInstance(":name="
068: + new Object().hashCode());
069: server
070: .createMBean(MLet.class.getName(), mletName,
071: null);
072:
073: // Emit another notification
074: server.unregisterMBean(mletName);
075:
076: // Remove the listener
077: server.removeNotificationListener(delegateName,
078: listener, null, null);
079: }
080: };
081:
082: MultiThreadTestRunner runner = new MultiThreadTestRunner(50, 10);
083: runner.run(test);
084: }
085:
086: public void testRegisterUnregisterQueryMBeans() throws Exception {
087: MultiThreadTestRunner.Test test = new MultiThreadTestRunner.Test() {
088: public void test() throws Exception {
089: Set names = server.queryNames(null, null);
090:
091: for (Iterator i = names.iterator(); i.hasNext();) {
092: ObjectName name = (ObjectName) i.next();
093: try {
094: if (server.isInstanceOf(name,
095: MBeanServerDelegate.class.getName())) {
096: server.getAttribute(name,
097: "ImplementationVendor");
098: }
099: } catch (InstanceNotFoundException ignored) {
100: // The Timer may be unregistered by another thread
101: }
102: }
103:
104: ObjectName timerName = ObjectName.getInstance(":timer="
105: + new Object().hashCode());
106: server.createMBean(Timer.class.getName(), timerName,
107: null);
108:
109: Set mbeans = server.queryMBeans(new ObjectName(
110: "JMImplementation:*"), null);
111:
112: for (Iterator i = mbeans.iterator(); i.hasNext();) {
113: ObjectInstance instance = (ObjectInstance) i.next();
114: try {
115: if (server.isInstanceOf(instance
116: .getObjectName(),
117: MBeanServerDelegate.class.getName())) {
118: server.getAttribute(instance
119: .getObjectName(),
120: "ImplementationVendor");
121: }
122: } catch (InstanceNotFoundException ignored) {
123: // The Timer may be unregistered by another thread
124: }
125: }
126:
127: server.unregisterMBean(timerName);
128: }
129: };
130:
131: MultiThreadTestRunner runner = new MultiThreadTestRunner(50, 10);
132: runner.run(test);
133: }
134: }
|