下面列出了javax.management.MBeanServerConnection#createMBean ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doTest(JMXConnector connector) throws IOException,
MalformedObjectNameException, ReflectionException,
InstanceAlreadyExistsException, MBeanRegistrationException,
MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException {
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean");
System.out.println("DEBUG: Calling createMBean");
mbsc.createMBean(Name.class.getName(), objName);
System.out.println("DEBUG: Calling setAttributes");
AttributeList attList = new AttributeList();
attList.add(new Attribute("FirstName", ANY_NAME));
attList.add(new Attribute("LastName", ANY_NAME));
mbsc.setAttributes(objName, attList);
}
protected int doCreateRequest(MBeanServerConnection mbsc,
ObjectName on,
boolean expectedException) {
int errorCount = 0;
try {
Utils.debug(Utils.DEBUG_STANDARD,
"ClientSide::doCreateRequest: Create and register the MBean") ;
mbsc.createMBean("Simple", on) ;
if (expectedException) {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create did not fail with expected SecurityException");
errorCount++;
} else {
System.out.println("ClientSide::doCreateRequest: (OK) Create succeed") ;
}
} catch(Exception e) {
Utils.printThrowable(e, true) ;
if (expectedException) {
if (e instanceof java.lang.SecurityException) {
System.out.println("ClientSide::doCreateRequest: " +
"(OK) Create failed with expected SecurityException") ;
} else {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create failed with " +
e.getClass() + " instead of expected SecurityException");
errorCount++;
}
} else {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create failed");
errorCount++;
}
}
return errorCount;
}
private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)
throws Exception {
if (adding) {
mserver.createMBean("javax.management.timer.Timer", name);
} else {
mserver.unregisterMBean(name);
}
}
private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)
throws Exception {
if (adding) {
mserver.createMBean("javax.management.timer.Timer", name);
} else {
mserver.unregisterMBean(name);
}
}
protected int doCreateRequest(MBeanServerConnection mbsc,
ObjectName on,
boolean expectedException) {
int errorCount = 0;
try {
Utils.debug(Utils.DEBUG_STANDARD,
"ClientSide::doCreateRequest: Create and register the MBean") ;
mbsc.createMBean("Simple", on) ;
if (expectedException) {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create did not fail with expected SecurityException");
errorCount++;
} else {
System.out.println("ClientSide::doCreateRequest: (OK) Create succeed") ;
}
} catch(Exception e) {
Utils.printThrowable(e, true) ;
if (expectedException) {
if (e instanceof java.lang.SecurityException) {
System.out.println("ClientSide::doCreateRequest: " +
"(OK) Create failed with expected SecurityException") ;
} else {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create failed with " +
e.getClass() + " instead of expected SecurityException");
errorCount++;
}
} else {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create failed");
errorCount++;
}
}
return errorCount;
}
private static void mbeanOp(MBeanServerConnection mserver, ObjectName name, boolean adding)
throws Exception {
if (adding) {
mserver.createMBean("javax.management.timer.Timer", name);
} else {
mserver.unregisterMBean(name);
}
}
public static void main(String[] args) throws Exception
{
// The address of the connector server
JMXServiceURL url = new JMXServiceURL("rmi", "localhost", 0, "/jndi/jmx");
// Create and connect the connector client
JMXConnector cntor = JMXConnectorFactory.connect(url, null);
// The connection represent, on client-side, the remote MBeanServer
MBeanServerConnection connection = cntor.getMBeanServerConnection();
// The listener that will receive notifications from a remote MBean
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
System.out.println(notification);
}
};
// The MBeanServerDelegate emits notifications about registration/unregistration of MBeans
ObjectName delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
connection.addNotificationListener(delegateName, listener, null, null);
// Give chance to the notification machinery to setup
Thread.sleep(1000);
// Now register a remote MBean, for example an MLet, so that the MBeanServerDelegate
// will emit notifications for its registration
ObjectName name = ObjectName.getInstance("examples:mbean=mlet");
// First notification
connection.createMBean(MLet.class.getName(), name, null);
// Second notification
connection.unregisterMBean(name);
}
protected int doCreateRequest(MBeanServerConnection mbsc,
ObjectName on,
boolean expectedException) {
int errorCount = 0;
try {
Utils.debug(Utils.DEBUG_STANDARD,
"ClientSide::doCreateRequest: Create and register the MBean") ;
mbsc.createMBean("Simple", on) ;
if (expectedException) {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create did not fail with expected SecurityException");
errorCount++;
} else {
System.out.println("ClientSide::doCreateRequest: (OK) Create succeed") ;
}
} catch(Exception e) {
Utils.printThrowable(e, true) ;
if (expectedException) {
if (e instanceof java.lang.SecurityException) {
System.out.println("ClientSide::doCreateRequest: " +
"(OK) Create failed with expected SecurityException") ;
} else {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create failed with " +
e.getClass() + " instead of expected SecurityException");
errorCount++;
}
} else {
System.out.println("ClientSide::doCreateRequest: " +
"(ERROR) Create failed");
errorCount++;
}
}
return errorCount;
}
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
final String[] command, final boolean oneBeanOnly) throws Exception {
Object[] result = null;
Set beans = mbsc.queryMBeans(objName, null);
if (beans.isEmpty()) {
// No bean found. Check if we are to create a bean?
if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
String className = command[0].substring(CREATE_CMD_PREFIX.length());
mbsc.createMBean(className, objName);
} else {
// TODO: Is there a better JMX exception that RE for this
// scenario?
throw new RuntimeException(objName.getCanonicalName() + " not registered.");
}
} else if (beans.size() == 1) {
result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
} else {
if (oneBeanOnly) {
throw new RuntimeException("Only supposed to be one bean " + "query result");
}
// This is case of multiple beans in query results.
// Print name of each into a StringBuffer. Return as one
// result.
StringBuffer buffer = new StringBuffer();
for (Iterator i = beans.iterator(); i.hasNext();) {
Object obj = i.next();
if (obj instanceof ObjectName) {
buffer.append((((ObjectName) obj).getCanonicalName()));
} else if (obj instanceof ObjectInstance) {
buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
} else {
throw new RuntimeException("Unexpected object type: " + obj);
}
buffer.append("\n");
}
result = new String[] { buffer.toString() };
}
return result;
}
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
final String[] command, final boolean oneBeanOnly) throws Exception {
Object[] result = null;
Set beans = mbsc.queryMBeans(objName, null);
if (beans.size() == 0) {
// No bean found. Check if we are to create a bean?
if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
String className = command[0].substring(CREATE_CMD_PREFIX.length());
mbsc.createMBean(className, objName);
} else {
// TODO: Is there a better JMX exception that RE for this
// scenario?
throw new RuntimeException(objName.getCanonicalName() + " not registered.");
}
} else if (beans.size() == 1) {
result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
} else {
if (oneBeanOnly) {
throw new RuntimeException("Only supposed to be one bean " + "query result");
}
// This is case of multiple beans in query results.
// Print name of each into a StringBuffer. Return as one
// result.
StringBuffer buffer = new StringBuffer();
for (Iterator i = beans.iterator(); i.hasNext();) {
Object obj = i.next();
if (obj instanceof ObjectName) {
buffer.append((((ObjectName) obj).getCanonicalName()));
} else if (obj instanceof ObjectInstance) {
buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
} else {
throw new RuntimeException("Unexpected object type: " + obj);
}
buffer.append("\n");
}
result = new String[] { buffer.toString() };
}
return result;
}
public static void main(String args[]) throws Exception {
int errorCount = 0 ;
String msgTag = "ClientSide::main: ";
try {
// Get a connection to remote mbean server
JMXServiceURL addr = new JMXServiceURL(args[0]);
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println(msgTag + "Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
System.out.println(msgTag +"(OK) Got result of class "
+ result.getClass().getName());
System.out.println(msgTag +"Received CompositeData is " + result);
System.out.println(msgTag +"---- OK\n") ;
// ----
// We use the value returned by getAttribute to perform the invoke.
System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
mbsc.invoke(objName, "doWeird",
new Object[]{result},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
// We build the CompositeData ourselves that time.
System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
String typeName = "SqeParameter";
String[] itemNames = new String[] {"glop"};
OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
CompositeType rowType = new CompositeType(typeName, typeName,
itemNames, itemNames, openTypes);
Object[] itemValues = {"HECTOR"};
CompositeData data =
new CompositeDataSupport(rowType, itemNames, itemValues);
TabularType tabType = new TabularType(typeName, typeName,
rowType, new String[]{"glop"});
TabularDataSupport tds = new TabularDataSupport(tabType);
tds.put(data);
System.out.println(msgTag +"Source CompositeData is " + data);
mbsc.invoke(objName, "doWeird",
new Object[]{data},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Unregister the MBean");
mbsc.unregisterMBean(objName);
System.out.println(msgTag +"---- OK\n") ;
// Terminate the JMX Client
cc.close();
} catch(Exception e) {
Utils.printThrowable(e, true) ;
errorCount++;
throw new RuntimeException(e);
} finally {
System.exit(errorCount);
}
}
public static void main(String args[]) throws Exception {
int errorCount = 0 ;
String msgTag = "ClientSide::main: ";
try {
// Get a connection to remote mbean server
JMXServiceURL addr = new JMXServiceURL(args[0]);
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println(msgTag + "Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
System.out.println(msgTag +"(OK) Got result of class "
+ result.getClass().getName());
System.out.println(msgTag +"Received CompositeData is " + result);
System.out.println(msgTag +"---- OK\n") ;
// ----
// We use the value returned by getAttribute to perform the invoke.
System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
mbsc.invoke(objName, "doWeird",
new Object[]{result},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
// We build the CompositeData ourselves that time.
System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
String typeName = "SqeParameter";
String[] itemNames = new String[] {"glop"};
OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
CompositeType rowType = new CompositeType(typeName, typeName,
itemNames, itemNames, openTypes);
Object[] itemValues = {"HECTOR"};
CompositeData data =
new CompositeDataSupport(rowType, itemNames, itemValues);
TabularType tabType = new TabularType(typeName, typeName,
rowType, new String[]{"glop"});
TabularDataSupport tds = new TabularDataSupport(tabType);
tds.put(data);
System.out.println(msgTag +"Source CompositeData is " + data);
mbsc.invoke(objName, "doWeird",
new Object[]{data},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Unregister the MBean");
mbsc.unregisterMBean(objName);
System.out.println(msgTag +"---- OK\n") ;
// Terminate the JMX Client
cc.close();
} catch(Exception e) {
Utils.printThrowable(e, true) ;
errorCount++;
throw new RuntimeException(e);
} finally {
System.exit(errorCount);
}
}
public static void main(String args[]) throws Exception {
int errorCount = 0 ;
String msgTag = "ClientSide::main: ";
try {
// Get a connection to remote mbean server
JMXServiceURL addr = new JMXServiceURL(args[0]);
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println(msgTag + "Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
System.out.println(msgTag +"(OK) Got result of class "
+ result.getClass().getName());
System.out.println(msgTag +"Received CompositeData is " + result);
System.out.println(msgTag +"---- OK\n") ;
// ----
// We use the value returned by getAttribute to perform the invoke.
System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
mbsc.invoke(objName, "doWeird",
new Object[]{result},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
// We build the CompositeData ourselves that time.
System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
String typeName = "SqeParameter";
String[] itemNames = new String[] {"glop"};
OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
CompositeType rowType = new CompositeType(typeName, typeName,
itemNames, itemNames, openTypes);
Object[] itemValues = {"HECTOR"};
CompositeData data =
new CompositeDataSupport(rowType, itemNames, itemValues);
TabularType tabType = new TabularType(typeName, typeName,
rowType, new String[]{"glop"});
TabularDataSupport tds = new TabularDataSupport(tabType);
tds.put(data);
System.out.println(msgTag +"Source CompositeData is " + data);
mbsc.invoke(objName, "doWeird",
new Object[]{data},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Unregister the MBean");
mbsc.unregisterMBean(objName);
System.out.println(msgTag +"---- OK\n") ;
// Terminate the JMX Client
cc.close();
} catch(Exception e) {
Utils.printThrowable(e, true) ;
errorCount++;
throw new RuntimeException(e);
} finally {
System.exit(errorCount);
}
}
/**
* create new Mbean and when set from ClassLoader Objectname
* @param jmxServerConnection
* @param name
* @return The value of the given named attribute
* @throws Exception
*/
protected String jmxCreate(MBeanServerConnection jmxServerConnection,
String name) throws Exception {
String error = null;
Object argsA[] = null;
String sigA[] = null;
if (args != null) {
argsA = new Object[ args.size()];
sigA = new String[args.size()];
for( int i=0; i<args.size(); i++ ) {
Arg arg=args.get(i);
if (arg.getType() == null) {
arg.setType("java.lang.String");
sigA[i]=arg.getType();
argsA[i]=arg.getValue();
} else {
sigA[i]=arg.getType();
argsA[i]=convertStringToType(arg.getValue(),arg.getType());
}
}
}
if (classLoader != null && !"".equals(classLoader)) {
if (isEcho()) {
handleOutput("create MBean " + name + " from class "
+ className + " with classLoader " + classLoader);
}
if(args == null)
jmxServerConnection.createMBean(className, new ObjectName(name), new ObjectName(classLoader));
else
jmxServerConnection.createMBean(className, new ObjectName(name), new ObjectName(classLoader),argsA,sigA);
} else {
if (isEcho()) {
handleOutput("create MBean " + name + " from class "
+ className);
}
if(args == null)
jmxServerConnection.createMBean(className, new ObjectName(name));
else
jmxServerConnection.createMBean(className, new ObjectName(name),argsA,sigA);
}
return error;
}
public static void main(String args[]) throws Exception {
int errorCount = 0 ;
String msgTag = "ClientSide::main: ";
try {
// Get a connection to remote mbean server
JMXServiceURL addr = new JMXServiceURL(args[0]);
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println(msgTag + "Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Get attribute SqeParameterAtt on our MXBean");
Object result = mbsc.getAttribute(objName, "SqeParameterAtt");
System.out.println(msgTag +"(OK) Got result of class "
+ result.getClass().getName());
System.out.println(msgTag +"Received CompositeData is " + result);
System.out.println(msgTag +"---- OK\n") ;
// ----
// We use the value returned by getAttribute to perform the invoke.
System.out.println(msgTag +"Call operation doWeird on our MXBean [1]");
mbsc.invoke(objName, "doWeird",
new Object[]{result},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
// We build the CompositeData ourselves that time.
System.out.println(msgTag +"Call operation doWeird on our MXBean [2]");
String typeName = "SqeParameter";
String[] itemNames = new String[] {"glop"};
OpenType<?>[] openTypes = new OpenType<?>[] {SimpleType.STRING};
CompositeType rowType = new CompositeType(typeName, typeName,
itemNames, itemNames, openTypes);
Object[] itemValues = {"HECTOR"};
CompositeData data =
new CompositeDataSupport(rowType, itemNames, itemValues);
TabularType tabType = new TabularType(typeName, typeName,
rowType, new String[]{"glop"});
TabularDataSupport tds = new TabularDataSupport(tabType);
tds.put(data);
System.out.println(msgTag +"Source CompositeData is " + data);
mbsc.invoke(objName, "doWeird",
new Object[]{data},
new String[]{"javax.management.openmbean.CompositeData"});
System.out.println(msgTag +"---- OK\n") ;
// ----
System.out.println(msgTag +"Unregister the MBean");
mbsc.unregisterMBean(objName);
System.out.println(msgTag +"---- OK\n") ;
// Terminate the JMX Client
cc.close();
} catch(Exception e) {
Utils.printThrowable(e, true) ;
errorCount++;
throw new RuntimeException(e);
} finally {
System.exit(errorCount);
}
}
public static void main(String[] args) throws Exception {
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
server.start();
JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
final MBeanServerConnection mbsc = client.getMBeanServerConnection();
final ObjectName mbean = ObjectName.getInstance(":type=Simple");
mbsc.createMBean(Simple.class.getName(), mbean);
System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
final Listener li = new Listener();
mbsc.addNotificationListener(mbean, li, null, null);
System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
mbsc.invoke(mbean, "emitNotification", null, null);
System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
final long stopTime = System.currentTimeMillis() + 2000;
synchronized(li) {
long toWait = stopTime - System.currentTimeMillis();
while (li.received < 1 && toWait > 0) {
li.wait(toWait);
toWait = stopTime - System.currentTimeMillis();
}
}
if (li.received < 1) {
throw new RuntimeException("No notif received!");
} else if (li.received > 1) {
throw new RuntimeException("Wait one notif but got: "+li.received);
}
System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
mbsc.removeNotificationListener(mbean, li);
// clean
client.close();
server.stop();
System.out.println("EmptyDomainNotificationTest-main: Bye.");
}
public static void main(String[] args) throws Exception {
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
server.start();
JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
final MBeanServerConnection mbsc = client.getMBeanServerConnection();
final ObjectName mbean = ObjectName.getInstance(":type=Simple");
mbsc.createMBean(Simple.class.getName(), mbean);
System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
final Listener li = new Listener();
mbsc.addNotificationListener(mbean, li, null, null);
System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
mbsc.invoke(mbean, "emitNotification", null, null);
System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
final long stopTime = System.currentTimeMillis() + 2000;
synchronized(li) {
long toWait = stopTime - System.currentTimeMillis();
while (li.received < 1 && toWait > 0) {
li.wait(toWait);
toWait = stopTime - System.currentTimeMillis();
}
}
if (li.received < 1) {
throw new RuntimeException("No notif received!");
} else if (li.received > 1) {
throw new RuntimeException("Wait one notif but got: "+li.received);
}
System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
mbsc.removeNotificationListener(mbean, li);
// clean
client.close();
server.stop();
System.out.println("EmptyDomainNotificationTest-main: Bye.");
}
public static void main(String[] args) throws Exception {
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
server.start();
JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
final MBeanServerConnection mbsc = client.getMBeanServerConnection();
final ObjectName mbean = ObjectName.getInstance(":type=Simple");
mbsc.createMBean(Simple.class.getName(), mbean);
System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
final Listener li = new Listener();
mbsc.addNotificationListener(mbean, li, null, null);
System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
mbsc.invoke(mbean, "emitNotification", null, null);
System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
final long stopTime = System.currentTimeMillis() + 2000;
synchronized(li) {
long toWait = stopTime - System.currentTimeMillis();
while (li.received < 1 && toWait > 0) {
li.wait(toWait);
toWait = stopTime - System.currentTimeMillis();
}
}
if (li.received < 1) {
throw new RuntimeException("No notif received!");
} else if (li.received > 1) {
throw new RuntimeException("Wait one notif but got: "+li.received);
}
System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
mbsc.removeNotificationListener(mbean, li);
// clean
client.close();
server.stop();
System.out.println("EmptyDomainNotificationTest-main: Bye.");
}
/**
* Create and register a new singleton instance of the ScanManager
* MBean in the given {@link MBeanServerConnection}.
* @param mbs The MBeanServer in which the new singleton instance
* should be created.
* @throws JMException The MBeanServer connection raised an exception
* while trying to instantiate and register the singleton MBean
* instance.
* @throws IOException There was a connection problem while trying to
* communicate with the underlying MBeanServer.
* @return A proxy for the registered MBean.
**/
public static ScanManagerMXBean register(MBeanServerConnection mbs)
throws IOException, JMException {
final ObjectInstance moi =
mbs.createMBean(ScanManager.class.getName(),SCAN_MANAGER_NAME);
final ScanManagerMXBean proxy =
JMX.newMXBeanProxy(mbs,moi.getObjectName(),
ScanManagerMXBean.class,true);
return proxy;
}
/**
* Create and register a new singleton instance of the ScanManager
* MBean in the given {@link MBeanServerConnection}.
* @param mbs The MBeanServer in which the new singleton instance
* should be created.
* @throws JMException The MBeanServer connection raised an exception
* while trying to instantiate and register the singleton MBean
* instance.
* @throws IOException There was a connection problem while trying to
* communicate with the underlying MBeanServer.
* @return A proxy for the registered MBean.
**/
public static ScanManagerMXBean register(MBeanServerConnection mbs)
throws IOException, JMException {
final ObjectInstance moi =
mbs.createMBean(ScanManager.class.getName(),SCAN_MANAGER_NAME);
final ScanManagerMXBean proxy =
JMX.newMXBeanProxy(mbs,moi.getObjectName(),
ScanManagerMXBean.class,true);
return proxy;
}