use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.
the class RMINotifTest method main.
public static void main(String[] args) {
try {
// create a rmi registry
Registry reg = null;
int port = 6666;
final Random r = new Random();
while (port++ < 7000) {
try {
reg = LocateRegistry.createRegistry(++port);
System.out.println("Creation of rmi registry succeeded. Running on port " + port);
break;
} catch (RemoteException re) {
// no problem
}
}
if (reg == null) {
System.out.println("Failed to create a RMI registry, " + "the ports from 6666 to 6999 are all occupied.");
System.exit(1);
}
// create a MBeanServer
MBeanServer server = MBeanServerFactory.createMBeanServer();
// create a notif emitter mbean
ObjectName mbean = new ObjectName("Default:name=NotificationEmitter");
server.registerMBean(new NotificationEmitter(), mbean);
// create a rmi server
JMXServiceURL url = new JMXServiceURL("rmi", null, port, "/jndi/rmi://:" + port + "/server" + port);
System.out.println("RMIConnectorServer address " + url);
JMXConnectorServer sServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, null);
ObjectInstance ss = server.registerMBean(sServer, new ObjectName("Default:name=RmiConnectorServer"));
sServer.start();
// create a rmi client
JMXConnector rmiConnection = JMXConnectorFactory.newJMXConnector(url, null);
rmiConnection.connect(null);
MBeanServerConnection client = rmiConnection.getMBeanServerConnection();
// add listener at the client side
client.addNotificationListener(mbean, listener, null, null);
//ask to send notifs
Object[] params = new Object[1];
String[] signatures = new String[1];
params[0] = new Integer(nb);
signatures[0] = "java.lang.Integer";
client.invoke(mbean, "sendNotifications", params, signatures);
// waiting ...
synchronized (lock) {
if (receivedNotifs != nb) {
lock.wait(10000);
System.out.println(">>> Received notifications..." + receivedNotifs);
}
}
// check
if (receivedNotifs != nb) {
System.exit(1);
} else {
System.out.println("The client received all notifications.");
}
// remove listener
client.removeNotificationListener(mbean, listener);
// more test
NotificationFilterSupport filter = new NotificationFilterSupport();
Object o = new Object();
client.addNotificationListener(mbean, listener, filter, o);
client.removeNotificationListener(mbean, listener, filter, o);
sServer.stop();
// // clean
// client.unregisterMBean(mbean);
// rmiConnection.close();
// Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.
the class LibraryLoaderTest method main.
public static void main(String[] args) throws Exception {
String osName = System.getProperty("os.name");
System.out.println("os.name=" + osName);
String osArch = System.getProperty("os.arch");
System.out.println("os.name=" + osArch);
//
if ((!(osName.equals("SunOS") && osArch.equals("sparc"))) && (!(osName.startsWith("Windows") && osArch.equals("x86")))) {
System.out.println("This test runs only on Solaris/SPARC and Windows/x86 platforms");
System.out.println("Bye! Bye!");
return;
}
String libPath = System.getProperty("java.library.path");
System.out.println("java.library.path=" + libPath);
String testSrc = System.getProperty("test.src");
System.out.println("test.src=" + testSrc);
String workingDir = System.getProperty("user.dir");
System.out.println("user.dir=" + workingDir);
String urlCodebase;
if (testSrc.startsWith("/")) {
urlCodebase = "file:" + testSrc.replace(File.separatorChar, '/') + "/";
} else {
urlCodebase = "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
}
// Create MBeanServer
//
MBeanServer server = MBeanServerFactory.newMBeanServer();
//
for (int i = 0; i < mletInfo.length; i++) {
// Create ObjectName for MLet
//
ObjectName mlet = new ObjectName(mletInfo[i][0]);
server.createMBean("javax.management.loading.MLet", mlet);
System.out.println("MLet = " + mlet);
// Display old library directory and set it to test.classes
//
String libraryDirectory = (String) server.getAttribute(mlet, "LibraryDirectory");
System.out.println("Old Library Directory = " + libraryDirectory);
Attribute attribute = new Attribute("LibraryDirectory", workingDir);
server.setAttribute(mlet, attribute);
libraryDirectory = (String) server.getAttribute(mlet, "LibraryDirectory");
System.out.println("New Library Directory = " + libraryDirectory);
// Get MBeans from URL
//
String mletURL = urlCodebase + mletInfo[i][1];
System.out.println("MLet URL = " + mletURL);
Object[] params = new Object[] { mletURL };
String[] signature = new String[] { "java.lang.String" };
Object[] res = ((Set<?>) server.invoke(mlet, "getMBeansFromURL", params, signature)).toArray();
//
for (int j = 0; j < res.length; j++) {
//
if (res[j] instanceof Throwable) {
((Throwable) res[j]).printStackTrace(System.out);
throw new Exception("Failed to load the MBean #" + j, (Throwable) res[j]);
}
// On each of the loaded MBeans, try to invoke their
// native operation
//
Object result = null;
try {
ObjectName mbean = ((ObjectInstance) res[j]).getObjectName();
result = server.getAttribute(mbean, "Random");
System.out.println("MBean #" + j + " = " + mbean);
System.out.println("Random number = " + result);
} catch (ReflectionException e) {
e.getTargetException().printStackTrace(System.out);
throw new Exception("A ReflectionException " + "occured when attempting to invoke " + "a native library based operation.", e.getTargetException());
}
}
}
}
use of javax.management.ObjectInstance in project aries by apache.
the class IntegrationTestBase method assertRegistered.
protected void assertRegistered(final MBeanServer server, final ObjectName objectName) {
try {
ObjectInstance instance = server.getObjectInstance(objectName);
TestCase.assertNotNull(instance);
TestCase.assertEquals(objectName, instance.getObjectName());
} catch (InstanceNotFoundException nfe) {
TestCase.fail("Expected instance of " + objectName + " registered with MBeanServer");
}
}
use of javax.management.ObjectInstance in project geode by apache.
the class ManagementAdapter method handleManagerStop.
/**
* Handles all the clean up activities when a Manager is stopped It clears the distributed mbeans
* and underlying data structures
*/
public void handleManagerStop() throws ManagementException {
if (!isServiceInitialised("handleManagerStop")) {
return;
}
MBeanJMXAdapter jmxAdapter = service.getJMXAdapter();
Map<ObjectName, Object> registeredMBeans = jmxAdapter.getLocalGemFireMBean();
ObjectName aggregatemMBeanPattern;
try {
aggregatemMBeanPattern = new ObjectName(ManagementConstants.AGGREGATE_MBEAN_PATTERN);
} catch (MalformedObjectNameException | NullPointerException e1) {
throw new ManagementException(e1);
}
MemberMXBean localMember = service.getMemberMXBean();
ObjectName memberObjectName = MBeanJMXAdapter.getMemberMBeanName(InternalDistributedSystem.getConnectedInstance().getDistributedMember());
FederationComponent removedComp = service.getLocalManager().getFedComponents().get(memberObjectName);
service.afterRemoveProxy(memberObjectName, MemberMXBean.class, localMember, removedComp);
for (ObjectName objectName : registeredMBeans.keySet()) {
if (objectName.equals(memberObjectName)) {
continue;
}
if (aggregatemMBeanPattern.apply(objectName)) {
continue;
}
Object object = registeredMBeans.get(objectName);
ObjectInstance instance;
try {
instance = mbeanServer.getObjectInstance(objectName);
String className = instance.getClassName();
Class cls = ClassLoadUtil.classFromName(className);
Type[] intfTyps = cls.getGenericInterfaces();
FederationComponent oldObj = service.getLocalManager().getFedComponents().get(objectName);
for (Type intfTyp1 : intfTyps) {
Class intfTyp = (Class) intfTyp1;
service.afterRemoveProxy(objectName, intfTyp, object, oldObj);
}
} catch (InstanceNotFoundException | ClassNotFoundException e) {
logger.warn("Failed to invoke aggregator for {} with exception {}", objectName, e.getMessage(), e);
}
}
service.removeProxyListener(this.aggregator);
this.aggregator = null;
}
use of javax.management.ObjectInstance in project geode by apache.
the class MBeanServerConnectionRule method getProxyMBean.
/**
* Retrieve a new proxy MBean
*
* @return A new proxy MBean of the same type with which the class was constructed
*/
public <T> T getProxyMBean(Class<T> proxyClass, String beanQueryName) throws MalformedObjectNameException, IOException {
ObjectName name = null;
QueryExp query = null;
if (proxyClass != null) {
query = Query.isInstanceOf(Query.value(proxyClass.getName()));
}
if (beanQueryName != null) {
name = ObjectName.getInstance(beanQueryName);
}
Set<ObjectInstance> beans = con.queryMBeans(name, query);
assertEquals("failed to find only one instance of type " + proxyClass.getName() + " with name " + beanQueryName, 1, beans.size());
return JMX.newMXBeanProxy(con, ((ObjectInstance) beans.toArray()[0]).getObjectName(), proxyClass);
}
Aggregations