use of javax.management.NotificationEmitter in project jdk8u_jdk by JetBrains.
the class ScanManagerTest method doTestOperation.
/**
* Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
*/
private void doTestOperation(ScanManagerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
System.out.println("doTestOperation: " + testName);
final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
try {
queue.put(notification);
} catch (Exception x) {
System.err.println("Failed to queue notif: " + x);
}
}
};
NotificationFilter filter = null;
Object handback = null;
final ScanState before;
final NotificationEmitter emitter = (NotificationEmitter) proxy;
emitter.addNotificationListener(listener, filter, handback);
before = proxy.getState();
op.call();
try {
final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
assertEquals(AttributeChangeNotification.class, notification.getClass());
assertEquals(ScanManager.SCAN_MANAGER_NAME, notification.getSource());
AttributeChangeNotification acn = (AttributeChangeNotification) notification;
assertEquals("State", acn.getAttributeName());
assertEquals(ScanState.class.getName(), acn.getAttributeType());
assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
emitter.removeNotificationListener(listener, filter, handback);
} finally {
try {
op.cancel();
} catch (Exception x) {
System.err.println("Failed to cleanup: " + x);
}
}
}
use of javax.management.NotificationEmitter in project jdk8u_jdk by JetBrains.
the class TestUtils method makeNotificationEmitter.
/**
* Transfroms a proxy implementing T in a proxy implementing T plus
* NotificationEmitter
*
**/
public static <T> T makeNotificationEmitter(T proxy, Class<T> mbeanInterface) {
if (proxy instanceof NotificationEmitter)
return proxy;
if (proxy == null)
return null;
if (!(proxy instanceof Proxy))
throw new IllegalArgumentException("not a " + Proxy.class.getName());
final Proxy p = (Proxy) proxy;
final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
if (!(handler instanceof MBeanServerInvocationHandler))
throw new IllegalArgumentException("not a JMX Proxy");
final MBeanServerInvocationHandler h = (MBeanServerInvocationHandler) handler;
final ObjectName name = h.getObjectName();
final MBeanServerConnection mbs = h.getMBeanServerConnection();
final boolean isMXBean = h.isMXBean();
final T newProxy;
if (isMXBean)
newProxy = JMX.newMXBeanProxy(mbs, name, mbeanInterface, true);
else
newProxy = JMX.newMBeanProxy(mbs, name, mbeanInterface, true);
return newProxy;
}
use of javax.management.NotificationEmitter in project jdk8u_jdk by JetBrains.
the class DirectoryScannerTest method doTestOperation.
private void doTestOperation(DirectoryScannerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
System.out.println("doTestOperation: " + testName);
final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
try {
queue.put(notification);
} catch (Exception x) {
System.err.println("Failed to queue notif: " + x);
}
}
};
NotificationFilter filter = null;
Object handback = null;
final ScanState before;
final NotificationEmitter emitter = (NotificationEmitter) makeNotificationEmitter(proxy, DirectoryScannerMXBean.class);
emitter.addNotificationListener(listener, filter, handback);
before = proxy.getState();
op.call();
try {
final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
assertEquals(AttributeChangeNotification.class, notification.getClass());
assertEquals(getObjectName(proxy), notification.getSource());
AttributeChangeNotification acn = (AttributeChangeNotification) notification;
assertEquals("State", acn.getAttributeName());
assertEquals(ScanState.class.getName(), acn.getAttributeType());
assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
emitter.removeNotificationListener(listener, filter, handback);
} finally {
try {
op.cancel();
} catch (Exception x) {
System.err.println("Failed to cleanup: " + x);
}
}
}
use of javax.management.NotificationEmitter in project geode by apache.
the class HeapMemoryMonitor method stopMonitoring.
/**
* Stops all three mechanisms from monitoring heap usage.
*/
@Override
public void stopMonitoring() {
synchronized (this) {
if (!this.started) {
return;
}
// Stop the poller
this.resourceManager.stopExecutor(this.pollerExecutor);
// Stop the JVM threshold listener
NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
try {
emitter.removeNotificationListener(this, null, null);
this.cache.getLoggerI18n().fine("Removed Memory MXBean notification listener" + this);
} catch (ListenerNotFoundException ignore) {
logger.debug("This instance '{}' was not registered as a Memory MXBean listener", this);
}
// Stop the stats listener
final GemFireStatSampler sampler = this.cache.getInternalDistributedSystem().getStatSampler();
if (sampler != null) {
sampler.removeLocalStatListener(this.statListener);
}
this.started = false;
}
}
use of javax.management.NotificationEmitter in project geode by apache.
the class MXMemoryPoolListenerExample method main.
public static void main(String[] args) {
final MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
final double threshold;
{
double t = 0.8;
if (args.length > 0) {
try {
t = Integer.parseInt(args[0]) / 100;
} catch (NumberFormatException useDefault) {
}
}
if (t < 0.0 || t > 1.0) {
throw new IllegalArgumentException("Theshold must be >= 0 and <= 100");
}
threshold = t;
}
final int percentTenured;
{
int p = 100;
if (args.length > 1) {
try {
p = Integer.parseInt(args[1]);
} catch (NumberFormatException useDefault) {
}
}
if (p > 100 || p < 0) {
throw new IllegalArgumentException("Percent Tenured must be >= 0 and <= 100");
}
percentTenured = p;
}
Properties dsProps = new Properties();
// Loner
dsProps.setProperty(MCAST_PORT, "0");
dsProps.setProperty(ConfigurationProperties.LOG_LEVEL, "info");
dsProps.setProperty(ConfigurationProperties.STATISTIC_SAMPLE_RATE, "200");
dsProps.setProperty(ConfigurationProperties.ENABLE_TIME_STATISTICS, "true");
dsProps.setProperty(ConfigurationProperties.STATISTIC_SAMPLING_ENABLED, "true");
DistributedSystem ds = DistributedSystem.connect(dsProps);
final LogWriter logger = ds.getLogWriter();
logger.info("Usage threshold: " + threshold + "; percent tenured: " + percentTenured + "; Runtime Maximum memory: " + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + "Mb" + "; Heap Maximum memory: " + (mbean.getHeapMemoryUsage().getMax() / (1024 * 1024)) + "Mb");
MXMemoryPoolListenerExample me = new MXMemoryPoolListenerExample(ds);
// Register this listener to NotificationEmitter
NotificationEmitter emitter = (NotificationEmitter) mbean;
emitter.addNotificationListener(me, null, null);
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean p : pools) {
if (p.isCollectionUsageThresholdSupported()) {
// p.setCollectionUsageThreshold(0);
logger.info("Pool which supports collection usage threshold: " + p.getName() + "; " + p.getCollectionUsage());
}
// On JRockit do not set the usage threshold on the Nursery pool
if (p.getType().equals(MemoryType.HEAP) && p.isUsageThresholdSupported() && !p.getName().startsWith("Nursery")) {
int byteThreshold = (int) Math.ceil(threshold * p.getUsage().getMax());
logger.info("Setting threshold " + (byteThreshold / (1024 * 1024)) + "Mb on: " + p.getName() + "; " + p.getCollectionUsage());
p.setUsageThreshold(byteThreshold);
}
}
final Cache c = CacheFactory.create(ds);
new MemoryHog("hog_1", c, me.critical).consumeMemory(percentTenured).printTenuredSize();
ds.disconnect();
}
Aggregations