类javax.management.NotificationEmitter源码实例Demo

下面列出了怎么用javax.management.NotificationEmitter的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: dragonwell8_jdk   文件: TestUtils.java
/**
 * 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;
}
 
源代码2 项目: TencentKona-8   文件: TestUtils.java
/**
 * 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;
}
 
源代码3 项目: jdk8u_jdk   文件: TestUtils.java
/**
 * 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;
}
 
源代码4 项目: jdk8u60   文件: TestUtils.java
/**
 * 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;
}
 
源代码5 项目: babar   文件: JVMUtils.java
/**
 * Register to all notifications from the GC MXBeans.
 * @param listener          A java NotificationListener
 */
public static void registerGCListener(GCListener listener) {

    // takes the GC listener and makes javax NotificationListener that filters GC notifications
    NotificationListener notificationListener = new NotificationListener() {
        @Override
        public void handleNotification(Notification notification, Object handback) {
            if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) return;

            // get the GC info object
            GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData)notification.getUserData());
            String gcAction = info.getGcAction();
            if (MINOR_GC_ACTION.equals(gcAction)) {
                listener.onMinorGc(info.getGcInfo().getDuration());
            }
            else if (MAJOR_GC_ACTION.equals(gcAction)) {
                listener.onMajorGc(info.getGcInfo().getDuration());
            }
        }
    };

    // register to the GC beans
    for (GarbageCollectorMXBean bean: ManagementFactory.getGarbageCollectorMXBeans()) {
        ((NotificationEmitter)bean).addNotificationListener(notificationListener, null, null);
    }
}
 
源代码6 项目: tsml   文件: MemoryWatcher.java
/**
 * emitters are used to listen to each memory pool (usually young / old gen). This function sets up the emitters
 * if not already
 */
private synchronized void setupEmitters() {
    if(emitters == null) {
        emitters = new ArrayList<>();
        // garbage collector for old and young gen
        List<GarbageCollectorMXBean> garbageCollectorBeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans();
        logger.finest("Setting up listeners for garbage collection ");
        for (GarbageCollectorMXBean garbageCollectorBean : garbageCollectorBeans) {
            // to log
            // listen to notification from the emitter
            NotificationEmitter emitter = (NotificationEmitter) garbageCollectorBean;
            emitters.add(emitter);
            emitter.addNotificationListener(listener, null, null);
        }
    }
}
 
源代码7 项目: andesite-node   文件: Init.java
public static void preInit(Config config) {
    ((Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(
            Level.valueOf(config.getString("log-level").toUpperCase())
    );
    if(config.getBoolean("prometheus.enabled")) {
        PrometheusUtils.setup();
        var listener = new GCListener();
        for(var gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
            if(gcBean instanceof NotificationEmitter) {
                ((NotificationEmitter) gcBean)
                        .addNotificationListener(listener, null, gcBean);
            }
        }
    }
    if(config.getBoolean("sentry.enabled")) {
        SentryUtils.setup(config);
    }
}
 
源代码8 项目: openjdk-jdk8u-backup   文件: TestUtils.java
/**
 * 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;
}
 
源代码9 项目: openjdk-jdk9   文件: ThresholdNotificationsTest.java
protected void runTest() {
    int iterationsCount
            = Integer.getInteger("jdk.test.lib.iterations", 1);
    MemoryPoolMXBean bean = btype.getMemoryPool();
    ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
            addNotificationListener(this, null, null);
    for (int i = 0; i < iterationsCount; i++) {
        CodeCacheUtils.hitUsageThreshold(bean, btype);
    }
    Asserts.assertTrue(
            Utils.waitForCondition(
                    () -> (CodeCacheUtils.isCodeHeapPredictable(btype) ?
                            (counter == iterationsCount) : (counter >= iterationsCount)),
                    WAIT_TIME),
            "Couldn't receive expected notifications count");
    try {
        ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).
                removeNotificationListener(this);
    } catch (ListenerNotFoundException ex) {
        throw new AssertionError("Can't remove notification listener", ex);
    }
    System.out.printf("INFO: Scenario finished successfully for %s%n",
            bean.getName());
}
 
源代码10 项目: Lavalink   文件: PrometheusMetrics.java
public PrometheusMetrics() {

        InstrumentedAppender prometheusAppender = new InstrumentedAppender();
        //log metrics
        final LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory();
        final ch.qos.logback.classic.Logger root = factory.getLogger(Logger.ROOT_LOGGER_NAME);
        prometheusAppender.setContext(root.getLoggerContext());
        prometheusAppender.start();
        root.addAppender(prometheusAppender);

        //jvm (hotspot) metrics
        DefaultExports.initialize();

        //gc pause buckets
        final GcNotificationListener gcNotificationListener = new GcNotificationListener();
        for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
            if (gcBean instanceof NotificationEmitter) {
                ((NotificationEmitter) gcBean).addNotificationListener(gcNotificationListener, null, gcBean);
            }
        }

        log.info("Prometheus metrics set up");
    }
 
源代码11 项目: dremio-oss   文件: HeapMonitorThread.java
private void registerForNotifications() {
  // Register the listener with MemoryMXBean
  NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
  emitter.addNotificationListener(listener, null, null);

  // set collection usage threshold.
  for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
    if (pool.getType() == MemoryType.HEAP &&
      pool.isUsageThresholdSupported() &&
      pool.isCollectionUsageThresholdSupported()) {

      long threshold = (pool.getUsage().getMax() * thresholdPercentage) / 100;
      logger.info("setting collection threshold for " + pool.getName() +
        " with max " + pool.getUsage().getMax() +
        " to " + threshold);

      pool.setCollectionUsageThreshold(threshold);
      monitoredPools.put(pool.getName(), pool.getCollectionUsageThresholdCount());
    } else {
      logger.info("skip monitoring for pool " + pool.getName());
    }
  }
}
 
源代码12 项目: hottub   文件: TestUtils.java
/**
 * 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;
}
 
源代码13 项目: openjdk-8-source   文件: TestUtils.java
/**
 * 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;
}
 
源代码14 项目: dragonwell8_jdk   文件: ScanDirAgent.java
/**
 * 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);
}
 
源代码15 项目: dragonwell8_jdk   文件: ScanDirAgent.java
/**
 * 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);
    }
}
 
源代码16 项目: dragonwell8_jdk   文件: OldMBeanServerTest.java
public void removeNotificationListener(
        ObjectName name, NotificationListener listener,
        NotificationFilter filter, Object handback)
        throws InstanceNotFoundException, ListenerNotFoundException {
    NotificationEmitter userMBean =
            (NotificationEmitter) getMBean(name);
    NotificationListener wrappedListener =
          wrappedListener(name, userMBean, listener);
    userMBean.removeNotificationListener(wrappedListener, filter, handback);
}
 
源代码17 项目: TencentKona-8   文件: ScanDirAgent.java
/**
 * 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);
}
 
源代码18 项目: TencentKona-8   文件: ScanDirAgent.java
/**
 * 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);
    }
}
 
源代码19 项目: dsl-devkit   文件: VirtualMachineTracer.java
/**
 * Uninstalls any attached GC listeners.
 *
 * @see #start()
 */
public void stop() {
  for (Map.Entry<NotificationEmitter, NotificationListener> entry : gcListenerMap.entrySet()) {
    try {
      entry.getKey().removeNotificationListener(entry.getValue());
    } catch (ListenerNotFoundException e) {
      // should never happen, ignore
    }
  }
}
 
源代码20 项目: TencentKona-8   文件: OldMBeanServerTest.java
public void removeNotificationListener(
        ObjectName name, NotificationListener listener,
        NotificationFilter filter, Object handback)
        throws InstanceNotFoundException, ListenerNotFoundException {
    NotificationEmitter userMBean =
            (NotificationEmitter) getMBean(name);
    NotificationListener wrappedListener =
          wrappedListener(name, userMBean, listener);
    userMBean.removeNotificationListener(wrappedListener, filter, handback);
}
 
源代码21 项目: joyqueue   文件: GarbageCollectorMonitor.java
/**
 * add listener to gc Garbage collector gc event
 **/
@Override
public void addGCEventListener(NotificationListener listener){
    List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
    for (GarbageCollectorMXBean gcbean : gcbeans) {
        NotificationEmitter emitter = (NotificationEmitter) gcbean;
        emitter.addNotificationListener(listener, null, null);
    }
}
 
源代码22 项目: garmadon   文件: GCNotifications.java
public void unsubscribe() {
    for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
        NotificationEmitter emitter = (NotificationEmitter) bean;
        try {
            emitter.removeNotificationListener(listener);
        } catch (ListenerNotFoundException e) {
            LOGGER.debug("Error during unsuscribing GC listener", e);
        }
    }
}
 
源代码23 项目: kanela   文件: OldGarbageCollectorListener.java
private void startListening() {
    val notificationListener = new GcNotificationListener();
    for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) {
        if (mbean instanceof NotificationEmitter) {
            val emitter = (NotificationEmitter) mbean;
            emitter.addNotificationListener(notificationListener, null, null);
        }
    }
}
 
源代码24 项目: jdk8u60   文件: ScanDirAgent.java
/**
 * 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);
    }
}
 
源代码25 项目: jdk8u60   文件: OldMBeanServerTest.java
public void removeNotificationListener(
        ObjectName name, NotificationListener listener,
        NotificationFilter filter, Object handback)
        throws InstanceNotFoundException, ListenerNotFoundException {
    NotificationEmitter userMBean =
            (NotificationEmitter) getMBean(name);
    NotificationListener wrappedListener =
          wrappedListener(name, userMBean, listener);
    userMBean.removeNotificationListener(wrappedListener, filter, handback);
}
 
源代码26 项目: spark   文件: GarbageCollectionMonitor.java
public GarbageCollectionMonitor() {
    List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans();
    for (GarbageCollectorMXBean bean : beans) {
        if (!(bean instanceof NotificationEmitter)) {
            continue;
        }

        NotificationEmitter notificationEmitter = (NotificationEmitter) bean;
        notificationEmitter.addNotificationListener(this, null, null);
        this.emitters.add(notificationEmitter);
    }
}
 
源代码27 项目: tsml   文件: MemoryWatcher.java
/**
 * tear down emitters
 */
private synchronized void tearDownEmitters() {
    if(emitters != null) {
        logger.finest("tearing down listeners for garbage collection");
        for(NotificationEmitter emitter : emitters) {
            try {
                emitter.removeNotificationListener(listener);
            } catch (ListenerNotFoundException e) {
                throw new IllegalStateException("already removed somehow...");
            }
        }
    }
}
 
源代码28 项目: openjdk-jdk8u   文件: ScanDirAgent.java
/**
 * 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);
}
 
源代码29 项目: openjdk-jdk8u   文件: ScanDirAgent.java
/**
 * 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);
    }
}
 
源代码30 项目: gemfirexd-oss   文件: HeapMemoryMonitor.java
/**
   * 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;
    }
  }
 
 类所在包
 同包方法