本文整理汇总了Java中javax.management.MBeanServer.createMBean方法的典型用法代码示例。如果您正苦于以下问题:Java MBeanServer.createMBean方法的具体用法?Java MBeanServer.createMBean怎么用?Java MBeanServer.createMBean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.management.MBeanServer
的用法示例。
在下文中一共展示了MBeanServer.createMBean方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javax.management.MBeanServer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
if (System.getSecurityManager() == null)
throw new IllegalStateException("No security manager installed!");
System.out.println("java.security.policy=" +
System.getProperty("java.security.policy"));
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Register the MLetMBean
//
System.out.println("Create MLet MBean");
ObjectName mlet = new ObjectName("MLetTest:name=MLetMBean");
mbs.createMBean("javax.management.loading.MLet", mlet);
// Test OK!
//
System.out.println("Bye! Bye!");
}
示例2: mbeanCreation
import javax.management.MBeanServer; //导入方法依赖的package包/类
private static int mbeanCreation(MBeanServer mbs, String name)
throws Exception {
int error = 0;
try {
System.out.println("Test: createMBean(" + name + ")");
mbs.createMBean(classname, ObjectName.getInstance(name));
error++;
System.out.println("Didn't get expected exception!");
System.out.println("Test failed!");
} catch (RuntimeOperationsException e) {
System.out.println("Got expected exception = " +
e.getCause().toString());
System.out.println("Test passed!");
}
return error;
}
示例3: main
import javax.management.MBeanServer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
// Instantiate the MBean server
//
System.out.println("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
ClassLoader mbsClassLoader = mbs.getClass().getClassLoader();
String testClassName = Test.class.getName();
// Check that the MBeanServer class loader does not know our test class
try {
Class.forName(testClassName, true, mbsClassLoader);
System.out.println("TEST IS INVALID: MBEANSERVER'S CLASS LOADER " +
"KNOWS OUR TEST CLASS");
System.exit(1);
} catch (ClassNotFoundException e) {
// As required
}
// Register the MBean
//
System.out.println("Create MBean from this class");
ObjectName objectName = new ObjectName("whatever:type=whatever");
mbs.createMBean(testClassName, objectName);
// Test OK!
//
System.out.println("Bye! Bye!");
}
示例4: createServerSide
import javax.management.MBeanServer; //导入方法依赖的package包/类
private JMXServiceURL createServerSide(Map<String, Object> serverMap)
throws Exception {
final int NINETY_SECONDS = 90;
System.out.println("AuthorizationTest::createServerSide: Start") ;
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
// Creates connection environment from server side params
HashMap<String, Object> env = new HashMap<>();
String value = null;
if ((value = (String)serverMap.get("-mapType")) != null) {
if (value.contains("x.access.file")) {
String accessFileStr = System.getProperty("test.src") +
File.separator + "access.properties";
env.put("jmx.remote.x.access.file", accessFileStr);
System.out.println("Added " + accessFileStr + " file as jmx.remote.x.access.file");
}
if (value.contains("x.password.file")) {
String passwordFileStr = System.getProperty("test.src") +
File.separator + "password.properties";
env.put("jmx.remote.x.password.file", passwordFileStr);
System.out.println("Added " + passwordFileStr + " file as jmx.remote.x.password.file");
}
}
if (serverMap.containsKey("-populate")) {
String populateClassName = "Simple";
ObjectName on =
new ObjectName("defaultDomain:class=Simple");
Utils.debug(Utils.DEBUG_STANDARD, "create and register Simple MBean") ;
mbs.createMBean(populateClassName, on);
}
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
cs.start();
Utils.waitReady(cs, NINETY_SECONDS);
JMXServiceURL addr = cs.getAddress();
System.out.println("AuthorizationTest::createServerSide: Done.") ;
return addr;
}
示例5: main
import javax.management.MBeanServer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName relSvcName = new ObjectName("a:type=relationService");
RelationServiceMBean relSvc =
JMX.newMBeanProxy(mbs, relSvcName, RelationServiceMBean.class);
mbs.createMBean("javax.management.relation.RelationService",
relSvcName,
new Object[] {Boolean.TRUE},
new String[] {"boolean"});
final BlockingQueue<Notification> q =
new ArrayBlockingQueue<Notification>(100);
NotificationListener qListener = new NotificationListener() {
public void handleNotification(Notification notification,
Object handback) {
q.add(notification);
}
};
mbs.addNotificationListener(relSvcName, qListener, null, null);
RoleInfo leftInfo =
new RoleInfo("left", "javax.management.timer.TimerMBean");
RoleInfo rightInfo =
new RoleInfo("right", "javax.management.timer.Timer");
relSvc.createRelationType("typeName", new RoleInfo[] {leftInfo, rightInfo});
ObjectName timer1 = new ObjectName("a:type=timer,number=1");
ObjectName timer2 = new ObjectName("a:type=timer,number=2");
mbs.createMBean("javax.management.timer.Timer", timer1);
mbs.createMBean("javax.management.timer.Timer", timer2);
Role leftRole =
new Role("left", Arrays.asList(new ObjectName[] {timer1}));
Role rightRole =
new Role("right", Arrays.asList(new ObjectName[] {timer2}));
RoleList roles =
new RoleList(Arrays.asList(new Role[] {leftRole, rightRole}));
final int NREPEAT = 10;
for (int i = 0; i < NREPEAT; i++) {
relSvc.createRelation("relationName", "typeName", roles);
relSvc.removeRelation("relationName");
}
Notification firstNotif = q.remove();
long seqNo = firstNotif.getSequenceNumber();
for (int i = 0; i < NREPEAT * 2 - 1; i++) {
Notification n = q.remove();
long nSeqNo = n.getSequenceNumber();
if (nSeqNo != seqNo + 1) {
throw new Exception(
"TEST FAILED: expected seqNo " + (seqNo + 1) + "; got " +
nSeqNo);
}
seqNo++;
}
System.out.println("TEST PASSED: got " + (NREPEAT * 2) + " notifications " +
"with contiguous sequence numbers");
}
示例6: runTest
import javax.management.MBeanServer; //导入方法依赖的package包/类
public static void runTest(int offset,
int counter[],
int derivedGauge[],
int threshold[]) throws Exception {
// Retrieve the platform MBean server
//
System.out.println("\nRetrieve the platform MBean server");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
String domain = mbs.getDefaultDomain();
// Create and register TestMBean
//
ObjectName name =
new ObjectName(domain +
":type=" + Test.class.getName() +
",offset=" + offset);
mbs.createMBean(Test.class.getName(), name);
TestMBean mbean = (TestMBean)
MBeanServerInvocationHandler.newProxyInstance(
mbs, name, TestMBean.class, false);
// Create and register CounterMonitorMBean
//
ObjectName cmn =
new ObjectName(domain +
":type=" + CounterMonitor.class.getName() +
",offset=" + offset);
CounterMonitor m = new CounterMonitor();
mbs.registerMBean(m, cmn);
CounterMonitorMBean cm = (CounterMonitorMBean)
MBeanServerInvocationHandler.newProxyInstance(
mbs, cmn, CounterMonitorMBean.class, true);
((NotificationEmitter) cm).addNotificationListener(
new Listener(), null, null);
cm.addObservedObject(name);
cm.setObservedAttribute("Counter");
cm.setGranularityPeriod(100);
cm.setInitThreshold(1);
cm.setOffset(offset);
cm.setModulus(5);
cm.setNotify(true);
// Start the monitor
//
System.out.println("\nStart monitoring...");
cm.start();
// Play with counter
//
for (int i = 0; i < counter.length; i++) {
mbean.setCounter(counter[i]);
System.out.println("\nCounter = " + mbean.getCounter());
Integer derivedGaugeValue;
// either pass or test timeout (killed by test harness)
// see 8025207
do {
Thread.sleep(150);
derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
} while (derivedGaugeValue.intValue() != derivedGauge[i]);
Number thresholdValue = cm.getThreshold(name);
System.out.println("Threshold = " + thresholdValue);
if (thresholdValue.intValue() != threshold[i]) {
System.out.println("Wrong threshold! Current value = " +
thresholdValue + " Expected value = " + threshold[i]);
System.out.println("\nStop monitoring...");
cm.stop();
throw new IllegalArgumentException("wrong threshold");
}
}
// Stop the monitor
//
System.out.println("\nStop monitoring...");
cm.stop();
}
示例7: main
import javax.management.MBeanServer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("Test that if an MBean throws RuntimeException " +
"it is wrapped in RuntimeMBeanException,");
System.out.println("and if a Standard MBean throws Exception " +
"it is wrapped in MBeanException");
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
Object standard = new Except();
ObjectName standardName = new ObjectName(":name=Standard MBean");
Object standardMBean =
new StandardMBean(new Except(), ExceptMBean.class);
ObjectName standardMBeanName =
new ObjectName(":name=Instance of StandardMBean");
Object dynamic = new DynamicExcept();
ObjectName dynamicName = new ObjectName(":name=Dynamic MBean");
mbs.registerMBean(standard, standardName);
mbs.registerMBean(standardMBean, standardMBeanName);
mbs.registerMBean(dynamic, dynamicName);
int failures = 0;
failures += test(mbs, standardName, true);
failures += test(mbs, standardMBeanName, true);
failures += test(mbs, dynamicName, false);
final boolean[] booleans = {false, true};
for (boolean runtimeX : booleans) {
Class<? extends Exception> excC =
runtimeX ? RuntimeMBeanException.class : MBeanException.class;
String excS =
runtimeX ? "a RuntimeMBeanException" : "an MBeanException";
String mbsS = "a plain MBeanServer";
System.out.println(
"Test that, with " + mbsS + ", " + excS + " is wrapped " +
"in " + excS);
// E.g. "Test that, with a plain MBeanServer, an MBeanException
// is wrapped in an MBeanException".
try {
mbs.createMBean(
Except.class.getName(), new ObjectName(":name=Oops"),
new Object[] {runtimeX},
new String[] {boolean.class.getName()});
System.out.println(
"FAIL: createMBean succeeded but should not have");
failures++;
} catch (Exception e) {
if (!excC.isInstance(e)) {
System.out.println(
"FAIL: expected " + excC.getName() + " from " +
"createMBean, got " + e);
failures++;
} else {
Throwable cause = e.getCause();
if (!excC.isInstance(cause)) {
System.out.println(
"FAIL: expected " + excC.getName() +
" as cause of " + excC.getName() +
", got " + e);
failures++;
} else
System.out.println("...ok");
}
}
}
if (failures == 0)
System.out.println("Test passed");
else {
System.out.println("TEST FAILED: " + failures + " failure(s)");
System.exit(1);
}
}