下面列出了javax.management.MBeanServerFactory#releaseMBeanServer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void withLocateExistingAndExistingServer() {
MBeanServer server = MBeanServerFactory.createMBeanServer();
try {
MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
bean.setLocateExistingServerIfPossible(true);
bean.afterPropertiesSet();
try {
MBeanServer otherServer = bean.getObject();
assertSame("Existing MBeanServer not located", server, otherServer);
}
finally {
bean.destroy();
}
}
finally {
MBeanServerFactory.releaseMBeanServer(server);
}
}
@Test
public void withLocateExistingAndExistingServer() {
MBeanServer server = MBeanServerFactory.createMBeanServer();
try {
MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
bean.setLocateExistingServerIfPossible(true);
bean.afterPropertiesSet();
try {
MBeanServer otherServer = bean.getObject();
assertSame("Existing MBeanServer not located", server, otherServer);
}
finally {
bean.destroy();
}
}
finally {
MBeanServerFactory.releaseMBeanServer(server);
}
}
@Override
@After
public void tearDown() throws Exception {
MBeanServerFactory.releaseMBeanServer(mbeanServer);
mbeanServer = null;
server.stop();
super.tearDown();
}
/**
* Unregisters the {@code MBeanServer} instance, if necessary.
*/
@Override
public void destroy() {
if (this.newlyRegistered) {
MBeanServerFactory.releaseMBeanServer(this.server);
}
}
/**
* Resets MBeanServerFactory and ManagementFactory to a known consistent state.
* This involves releasing all currently registered MBeanServers and resetting
* the platformMBeanServer to null.
*/
public static void resetMBeanServers() throws Exception {
for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
MBeanServerFactory.releaseMBeanServer(server);
}
Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
field.setAccessible(true);
field.set(null, null);
}
/**
* Unregisters all GemFire MBeans and then releases the MBeanServer for
* garbage collection.
*/
static void releaseMBeanServer() {
try {
// unregister all GemFire mbeans...
Iterator iter = mbeanServer.queryNames(null, null).iterator();
while (iter.hasNext()) {
ObjectName name = (ObjectName)iter.next();
if (name.getDomain().startsWith(DEFAULT_DOMAIN)) {
unregisterMBean(name);
}
}
// last, release the mbean server...
MBeanServerFactory.releaseMBeanServer(mbeanServer);
mbeanServer = null;
} catch (JMRuntimeException e) {
logStackTrace(LogWriterImpl.WARNING_LEVEL, e);
}
/* See #42391. Cleaning up the static maps which might be still holding
* references to ManagedResources */
synchronized (MBeanUtil.managedResources) {
MBeanUtil.managedResources.clear();
}
synchronized (refreshClients) {
refreshClients.clear();
}
/* See #42391. Cleaning up the static maps which might be still holding
* references to ManagedResources */
synchronized (MBeanUtil.managedResources) {
MBeanUtil.managedResources.clear();
}
synchronized (refreshClients) {
refreshClients.clear();
}
}
@Override
@After
public void tearDown() throws Exception {
MBeanServerFactory.releaseMBeanServer(mbeanServer);
super.tearDown();
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
try {
mlet.getMBeansFromURL("bogus://whatever");
System.out.println("TEST FAILED: Expected " +
ServiceNotFoundException.class +
" exception not thrown.");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got null cause in " +
ServiceNotFoundException.class +
" exception.");
error = true;
} else {
System.out.println("TEST PASSED: Got non-null cause in " +
ServiceNotFoundException.class +
" exception.");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
try {
mlet.getMBeansFromURL("bogus://whatever");
System.out.println("TEST FAILED: Expected " +
ServiceNotFoundException.class +
" exception not thrown.");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got null cause in " +
ServiceNotFoundException.class +
" exception.");
error = true;
} else {
System.out.println("TEST PASSED: Got non-null cause in " +
ServiceNotFoundException.class +
" exception.");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
@Test(expected = NoSuchElementException.class)
public void mxServerNotFound()
{
MBeanServerFactory.releaseMBeanServer(mxServer);
cut.mxServer();
}
/**
* Run test
*/
public int runTest(boolean setBeforeStart) throws Exception {
echo("=-=-= MBSFPreStartPostStartTest: Set MBSF " +
(setBeforeStart ? "before" : "after") +
" starting the connector server =-=-=");
JMXConnectorServer server = null;
JMXConnector client = null;
// Create a new MBeanServer
//
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
try {
// Create the JMXServiceURL
//
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
// Create a JMXConnectorServer
//
server = JMXConnectorServerFactory.newJMXConnectorServer(url,
null,
mbs);
// Create MBeanServerForwarder
//
MBeanServerForwarder mbsf =
MBSFInvocationHandler.newProxyInstance();
// Set MBeanServerForwarder before start()
//
if (setBeforeStart)
server.setMBeanServerForwarder(mbsf);
// Start the JMXConnectorServer
//
server.start();
// Set MBeanServerForwarder after start()
//
if (!setBeforeStart)
server.setMBeanServerForwarder(mbsf);
// Create a JMXConnector
//
client = server.toJMXConnector(null);
// Connect to the connector server
//
client.connect(null);
// Get non-secure MBeanServerConnection
//
final MBeanServerConnection mbsc =
client.getMBeanServerConnection();
// Run method
//
mbsc.getDefaultDomain();
// Check flag in MBeanServerForwarder
//
MBSFInvocationHandler mbsfih =
(MBSFInvocationHandler) Proxy.getInvocationHandler(mbsf);
if (mbsfih.getFlag() == true) {
echo("OK: Did go into MBeanServerForwarder!");
} else {
echo("KO: Didn't go into MBeanServerForwarder!");
return 1;
}
} catch (Exception e) {
echo("Failed to perform operation: " + e);
return 1;
} finally {
// Close the connection
//
if (client != null)
client.close();
// Stop the connector server
//
if (server != null)
server.stop();
// Release the MBeanServer
//
if (mbs != null)
MBeanServerFactory.releaseMBeanServer(mbs);
}
return 0;
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
try {
mlet.getMBeansFromURL("bogus://whatever");
System.out.println("TEST FAILED: Expected " +
ServiceNotFoundException.class +
" exception not thrown.");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got null cause in " +
ServiceNotFoundException.class +
" exception.");
error = true;
} else {
System.out.println("TEST PASSED: Got non-null cause in " +
ServiceNotFoundException.class +
" exception.");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
String testSrc = System.getProperty("test.src");
System.out.println("test.src = " + testSrc);
String urlCodebase;
if (testSrc.startsWith("/")) {
urlCodebase =
"file:" + testSrc.replace(File.separatorChar, '/') + "/";
} else {
urlCodebase =
"file:/" + testSrc.replace(File.separatorChar, '/') + "/";
}
String mletFile = urlCodebase + args[0];
System.out.println("MLet File = " + mletFile);
try {
mlet.getMBeansFromURL(mletFile);
System.out.println(
"TEST FAILED: Expected ServiceNotFoundException not thrown");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got unexpected null cause " +
"in ServiceNotFoundException");
error = true;
} else if (!(e.getCause() instanceof IOException)) {
System.out.println("TEST FAILED: Got unexpected non-null " +
"cause in ServiceNotFoundException");
error = true;
} else {
System.out.println("TEST PASSED: Got expected non-null " +
"cause in ServiceNotFoundException");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
String testSrc = System.getProperty("test.src");
System.out.println("test.src = " + testSrc);
String urlCodebase;
if (testSrc.startsWith("/")) {
urlCodebase =
"file:" + testSrc.replace(File.separatorChar, '/') + "/";
} else {
urlCodebase =
"file:/" + testSrc.replace(File.separatorChar, '/') + "/";
}
String mletFile = urlCodebase + args[0];
System.out.println("MLet File = " + mletFile);
try {
mlet.getMBeansFromURL(mletFile);
System.out.println(
"TEST FAILED: Expected ServiceNotFoundException not thrown");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got unexpected null cause " +
"in ServiceNotFoundException");
error = true;
} else if (!(e.getCause() instanceof IOException)) {
System.out.println("TEST FAILED: Got unexpected non-null " +
"cause in ServiceNotFoundException");
error = true;
} else {
System.out.println("TEST PASSED: Got expected non-null " +
"cause in ServiceNotFoundException");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
try {
mlet.getMBeansFromURL("bogus://whatever");
System.out.println("TEST FAILED: Expected " +
ServiceNotFoundException.class +
" exception not thrown.");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got null cause in " +
ServiceNotFoundException.class +
" exception.");
error = true;
} else {
System.out.println("TEST PASSED: Got non-null cause in " +
ServiceNotFoundException.class +
" exception.");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
try {
mlet.getMBeansFromURL("bogus://whatever");
System.out.println("TEST FAILED: Expected " +
ServiceNotFoundException.class +
" exception not thrown.");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got null cause in " +
ServiceNotFoundException.class +
" exception.");
error = true;
} else {
System.out.println("TEST PASSED: Got non-null cause in " +
ServiceNotFoundException.class +
" exception.");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
private static void test(String proto) throws Exception {
System.out.println("Unexpected notifications test for protocol " +
proto);
MBeanServer mbs = null;
try {
// Create a MBeanServer
//
mbs = MBeanServerFactory.createMBeanServer();
// Create a NotificationEmitter MBean
//
mbean = new ObjectName ("Default:name=NotificationEmitter");
mbs.registerMBean(new NotificationEmitter(), mbean);
// Create a connector server
//
url = new JMXServiceURL("service:jmx:" + proto + "://");
server = JMXConnectorServerFactory.newJMXConnectorServer(url,
null,
mbs);
mbs.registerMBean(
server,
new ObjectName("Default:name=ConnectorServer"));
server.start();
url = server.getAddress();
for (int j = 0; j < 2; j++) {
test();
}
} finally {
// Stop server
//
server.stop();
// Release the MBeanServer
//
MBeanServerFactory.releaseMBeanServer(mbs);
}
}
public static void main(String[] args) throws Exception {
boolean error = false;
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Instantiate an MLet
//
System.out.println("Create the MLet");
MLet mlet = new MLet();
// Register the MLet MBean with the MBeanServer
//
System.out.println("Register the MLet MBean");
ObjectName mletObjectName = new ObjectName("Test:type=MLet");
mbs.registerMBean(mlet, mletObjectName);
// Call getMBeansFromURL
//
System.out.println("Call mlet.getMBeansFromURL(<url>)");
String testSrc = System.getProperty("test.src");
System.out.println("test.src = " + testSrc);
String urlCodebase;
if (testSrc.startsWith("/")) {
urlCodebase =
"file:" + testSrc.replace(File.separatorChar, '/') + "/";
} else {
urlCodebase =
"file:/" + testSrc.replace(File.separatorChar, '/') + "/";
}
String mletFile = urlCodebase + args[0];
System.out.println("MLet File = " + mletFile);
try {
mlet.getMBeansFromURL(mletFile);
System.out.println(
"TEST FAILED: Expected ServiceNotFoundException not thrown");
error = true;
} catch (ServiceNotFoundException e) {
if (e.getCause() == null) {
System.out.println("TEST FAILED: Got unexpected null cause " +
"in ServiceNotFoundException");
error = true;
} else if (!(e.getCause() instanceof IOException)) {
System.out.println("TEST FAILED: Got unexpected non-null " +
"cause in ServiceNotFoundException");
error = true;
} else {
System.out.println("TEST PASSED: Got expected non-null " +
"cause in ServiceNotFoundException");
error = false;
}
e.printStackTrace(System.out);
}
// Unregister the MLet MBean
//
System.out.println("Unregister the MLet MBean");
mbs.unregisterMBean(mletObjectName);
// Release MBean server
//
System.out.println("Release the MBean server");
MBeanServerFactory.releaseMBeanServer(mbs);
// End Test
//
System.out.println("Bye! Bye!");
if (error) System.exit(1);
}
@Override
@After
public void tearDown() throws Exception {
MBeanServerFactory.releaseMBeanServer(mbeanServer_1);
super.tearDown();
}
@AfterEach
void tearDown() {
if (mbeanServer != null) {
MBeanServerFactory.releaseMBeanServer(mbeanServer);
}
}