本文整理汇总了Java中javax.management.MBeanServer.addNotificationListener方法的典型用法代码示例。如果您正苦于以下问题:Java MBeanServer.addNotificationListener方法的具体用法?Java MBeanServer.addNotificationListener怎么用?Java MBeanServer.addNotificationListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.management.MBeanServer
的用法示例。
在下文中一共展示了MBeanServer.addNotificationListener方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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");
}
示例2: run
import javax.management.MBeanServer; //导入方法依赖的package包/类
void run() throws Exception {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final ObjectName observedName = new ObjectName("a:b=c");
final ObjectName monitorName = new ObjectName("a:type=Monitor");
mbs.registerMBean(new StringMonitor(), monitorName);
final StringMonitorMBean monitorProxy =
JMX.newMBeanProxy(mbs, monitorName, StringMonitorMBean.class);
final TestMBean observedProxy =
JMX.newMBeanProxy(mbs, observedName, TestMBean.class);
final Runnable sensitiveThing = new Runnable() {
public void run() {
doSensitiveThing(monitorProxy, observedName);
}
};
final Runnable nothing = new Runnable() {
public void run() {}
};
final Runnable withinGetAttribute =
(when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;
mbs.registerMBean(new Test(withinGetAttribute), observedName);
monitorProxy.addObservedObject(observedName);
monitorProxy.setObservedAttribute("Thing");
monitorProxy.setStringToCompare("old");
monitorProxy.setGranularityPeriod(10L); // 10 ms
monitorProxy.setNotifyDiffer(true);
final int initGetCount = observedProxy.getGetCount();
monitorProxy.start();
int getCount = initGetCount;
for (int i = 0; i < 500; i++) { // 500 * 10 = 5 seconds
getCount = observedProxy.getGetCount();
if (getCount != initGetCount)
break;
Thread.sleep(10);
}
if (getCount <= initGetCount)
throw new Exception("Test failed: presumable deadlock");
// This won't show up as a deadlock in CTRL-\ or in
// ThreadMXBean.findDeadlockedThreads(), because they don't
// see that thread A is waiting for thread B (B.join()), and
// thread B is waiting for a lock held by thread A
// Now we know the monitor has observed the initial value,
// so if we want to test notify behaviour we can trigger by
// exceeding the threshold.
if (when == When.IN_NOTIFY) {
final AtomicInteger notifCount = new AtomicInteger();
final NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification n, Object h) {
Thread t = new Thread(sensitiveThing);
t.start();
try {
t.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
notifCount.incrementAndGet();
}
};
mbs.addNotificationListener(monitorName, listener, null, null);
observedProxy.setThing("new");
for (int i = 0; i < 500 && notifCount.get() == 0; i++)
Thread.sleep(10);
if (notifCount.get() == 0)
throw new Exception("Test failed: presumable deadlock");
}
}
示例3: run
import javax.management.MBeanServer; //导入方法依赖的package包/类
void run() throws Exception {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final ObjectName observedName = new ObjectName("a:b=c");
final ObjectName monitorName = new ObjectName("a:type=Monitor");
mbs.registerMBean(new CounterMonitor(), monitorName);
final CounterMonitorMBean monitorProxy =
JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
final TestMBean observedProxy =
JMX.newMBeanProxy(mbs, observedName, TestMBean.class);
final Runnable sensitiveThing = new Runnable() {
public void run() {
doSensitiveThing(monitorProxy, observedName);
}
};
final Runnable nothing = new Runnable() {
public void run() {}
};
final Runnable withinGetAttribute =
(when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;
mbs.registerMBean(new Test(withinGetAttribute), observedName);
monitorProxy.addObservedObject(observedName);
monitorProxy.setObservedAttribute("Thing");
monitorProxy.setInitThreshold(100);
monitorProxy.setGranularityPeriod(10L); // 10 ms
monitorProxy.setNotify(true);
final int initGetCount = observedProxy.getGetCount();
monitorProxy.start();
System.out.println("Checking GetCount, possible deadlock if timeout.");
do { // 8038322. Until timeout of testing harness
Thread.sleep(200);
} while ((observedProxy.getGetCount()) == initGetCount);
System.out.println("Done!");
// This won't show up as a deadlock in CTRL-\ or in
// ThreadMXBean.findDeadlockedThreads(), because they don't
// see that thread A is waiting for thread B (B.join()), and
// thread B is waiting for a lock held by thread A
// Now we know the monitor has observed the initial value,
// so if we want to test notify behaviour we can trigger by
// exceeding the threshold.
if (when == When.IN_NOTIFY) {
final AtomicInteger notifCount = new AtomicInteger();
final NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification n, Object h) {
Thread t = new Thread(sensitiveThing);
t.start();
try {
t.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
notifCount.incrementAndGet();
}
};
mbs.addNotificationListener(monitorName, listener, null, null);
observedProxy.setThing(1000);
System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
do {
Thread.sleep(200);
} while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
System.out.println("Done");
}
}