/**
* 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;
}
java类javax.management.NotificationEmitter的实例源码
TestUtils.java 文件源码
项目:WBSAirback
阅读 31
收藏 0
点赞 0
评论 0
LowMemoryDetector.java 文件源码
项目:scaleDOM
阅读 14
收藏 0
点赞 0
评论 0
/**
* Default constructor.
*
* @param memoryThresholdFactor amount of memory to be considered 'enough memory'.
* @throws IllegalArgumentException If memoryThresholdFactor is not strictly between 0.0 and 1.0.
*/
public LowMemoryDetector(final double memoryThresholdFactor) {
checkArgument(memoryThresholdFactor > 0.0 && memoryThresholdFactor < 1.0,
"Expected memoryThresholdFactor to be between 0.0 and 1.0, %s is not.", memoryThresholdFactor);
isLowMemory = false;
// Find the tenured heap
tenuredHeap = findTenuredHeap();
checkNotNull(tenuredHeap, "Expected tenuredHeap to be not null.");
tenuredHeapSize = tenuredHeap.getUsage().getMax();
log.debug("Determined the tenured heap as '{}' (size: {} B).", tenuredHeap.getName(), tenuredHeapSize);
// Monitor tenured heap
memoryThreshold = (long) (tenuredHeapSize * memoryThresholdFactor);
tenuredHeap.setCollectionUsageThreshold(memoryThreshold);
log.debug("Low memory threshold is {} B.", memoryThreshold);
// Add notification listener
final NotificationEmitter notificationEmitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
notificationEmitter.addNotificationListener(this, null, null);
}
G1Demo.java 文件源码
项目:java-perv
阅读 18
收藏 0
点赞 0
评论 0
private static void subscribeForGc() {
ManagementFactory.getGarbageCollectorMXBeans().stream().filter(bean -> bean instanceof NotificationEmitter).forEach(bean -> {
((NotificationEmitter) bean).addNotificationListener((notification, handback) -> {
// System.err.println("Notified");
if (GARBAGE_COLLECTION_NOTIFICATION.equals(notification.getType())) {
GarbageCollectionNotificationInfo info = from((CompositeData) notification.getUserData());
com.sun.management.GarbageCollectorMXBean mxBean = (com.sun.management.GarbageCollectorMXBean) handback;
// System.err.println("GC notification");
GcInfo gcInfo = info.getGcInfo();
if (gcInfo != null) {
System.out.println(info.getGcName() + ", " + info.getGcAction() + ", " + mxBean.getName() + ", " + info.getGcCause() + ", " + gcInfo.getMemoryUsageBeforeGc() + ", " + gcInfo.getMemoryUsageAfterGc());
}
}
}, null, bean);
});
}
MemoryMonitor.java 文件源码
项目:kuali_rice
阅读 23
收藏 0
点赞 0
评论 0
public MemoryMonitor() {
LOG.info("initializing");
this.springContextId = "Unknown";
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationEmitter emitter = (NotificationEmitter) mbean;
emitter.addNotificationListener(new NotificationListener() {
public void handleNotification(Notification n, Object hb) {
if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
long maxMemory = tenuredGenPool.getUsage().getMax();
long usedMemory = tenuredGenPool.getUsage().getUsed();
for (Listener listener : listeners) {
listener.memoryUsageLow(springContextId, usedMemory, maxMemory);
}
}
}
}, null, null);
}
PerformanceWatcher.java 文件源码
项目:consulo
阅读 18
收藏 0
点赞 0
评论 0
private void watchCodeCache(final MemoryPoolMXBean bean) {
final long threshold = bean.getUsage().getMax() - 5 * 1024 * 1024;
if (!bean.isUsageThresholdSupported() || threshold <= 0) return;
bean.setUsageThreshold(threshold);
final NotificationEmitter emitter = (NotificationEmitter)ManagementFactory.getMemoryMXBean();
emitter.addNotificationListener(new NotificationListener() {
@Override
public void handleNotification(Notification n, Object hb) {
if (bean.getUsage().getUsed() > threshold) {
LOG.info("Code Cache is almost full");
dumpThreads("codeCacheFull", true);
try {
emitter.removeNotificationListener(this);
}
catch (ListenerNotFoundException e) {
LOG.error(e);
}
}
}
}, null, null);
}
BTraceRuntime.java 文件源码
项目:btrace
阅读 13
收藏 0
点赞 0
评论 0
private synchronized void exitImpl(int exitCode) {
if (exitHandler != null) {
try {
exitHandler.invoke(null, exitCode);
} catch (Throwable ignored) {
}
}
disabled = true;
if (timer != null) {
timer.cancel();
}
if (memoryListener != null && memoryMBean != null) {
NotificationEmitter emitter = (NotificationEmitter) memoryMBean;
try {
emitter.removeNotificationListener(memoryListener);
} catch (ListenerNotFoundException lnfe) {}
}
if (threadPool != null) {
threadPool.shutdownNow();
}
send(new ExitCommand(exitCode));
}
HeapMemoryMonitor.java 文件源码
项目:monarch
阅读 24
收藏 0
点赞 0
评论 0
/**
* 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 e) {
this.cache.getLoggerI18n().fine(
"This instance '" + toString() + "' was not registered as a Memory MXBean listener");
}
// Stop the stats listener
final GemFireStatSampler sampler = this.cache.getDistributedSystem().getStatSampler();
if (sampler != null) {
sampler.removeLocalStatListener(this.statListener);
}
this.started = false;
}
}
CodeManager.java 文件源码
项目:ChronoBike
阅读 16
收藏 0
点赞 0
评论 0
public static CodeSizeLimitEventHandler createSizeLimitEventHandler()
{
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationEmitter emitter = (NotificationEmitter) mbean;
CodeSizeLimitEventHandler eventHandler = new CodeSizeLimitEventHandler();
return eventHandler;
}
ScanDirAgent.java 文件源码
项目:jdk8u-jdk
阅读 16
收藏 0
点赞 0
评论 0
/**
* Initialize the application by registering a ScanManagerMXBean in
* the platform MBeanServer
* @throws java.io.IOException Registration failed for communication-related reasons.
* @throws javax.management.JMException Registration failed for JMX-related reasons.
*/
public void init() throws IOException, JMException {
// Registers the ScanManagerMXBean singleton in the
// platform MBeanServer
//
proxy = ScanManager.register();
// Registers a NotificationListener with the ScanManagerMXBean in
// order to receive state changed notifications.
//
((NotificationEmitter)proxy).addNotificationListener(listener,null,null);
}
ScanDirAgent.java 文件源码
项目:jdk8u-jdk
阅读 14
收藏 0
点赞 0
评论 0
/**
* Cleanup after close: unregister the ScanManagerMXBean singleton.
* @throws java.io.IOException Cleanup failed for communication-related reasons.
* @throws javax.management.JMException Cleanup failed for JMX-related reasons.
*/
public void cleanup() throws IOException, JMException {
try {
((NotificationEmitter)proxy).
removeNotificationListener(listener,null,null);
} finally {
ManagementFactory.getPlatformMBeanServer().
unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
}
}