List of usage examples for javax.management NotificationEmitter addNotificationListener
public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws java.lang.IllegalArgumentException;
From source file:net.centro.rtb.monitoringcenter.metrics.system.jvm.GarbageCollectorMetricSet.java
GarbageCollectorMetricSet() { this.garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans(); this.minorGcTimer = new Timer(); this.majorGcTimer = new Timer(); // Determine the location of the gc log file (note that there's not support for rolling gc logs) String gcLogFilePath = null;/*from w ww. ja v a 2 s. c o m*/ RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); List<String> inputArguments = runtimeMXBean.getInputArguments(); for (String argument : inputArguments) { if (argument.startsWith(LOG_GC_JVM_PARAM)) { gcLogFilePath = argument.substring(LOG_GC_JVM_PARAM.length()); break; } } if (gcLogFilePath != null && !gcLogFilePath.trim().isEmpty()) { final File gcLogFile = new File(gcLogFilePath); if (gcLogFile.exists()) { this.fullCollectionsCounter = new AtomicLong(); this.gcLogTailer = Tailer.create(gcLogFile, new TailerListenerAdapter() { @Override public void handle(String line) { if (line != null && line.contains(FULL_GC_LOG_STRING)) { fullCollectionsCounter.incrementAndGet(); } } }, GC_LOG_FILE_TAIL_DELAY_IN_MILLIS); } } // Attach a listener to the GarbageCollectorMXBeans this.gcEventListener = new NotificationListener() { @Override public void handleNotification(Notification notification, Object handback) { String notificationType = notification.getType(); if (notificationType.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { CompositeData compositeData = CompositeData.class.cast(notification.getUserData()); GarbageCollectionNotificationInfo gcNotificationInfo = GarbageCollectionNotificationInfo .from(compositeData); if (GC_NOTIFICATION_MINOR_GC_ACTION_STRING.equals(gcNotificationInfo.getGcAction())) { minorGcTimer.update(gcNotificationInfo.getGcInfo().getDuration(), TimeUnit.MILLISECONDS); } else if (GC_NOTIFICATION_MAJOR_GC_ACTION_STRING.equals(gcNotificationInfo.getGcAction())) { majorGcTimer.update(gcNotificationInfo.getGcInfo().getDuration(), TimeUnit.MILLISECONDS); } } } }; for (final GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) { if (NotificationEmitter.class.isInstance(garbageCollectorMXBean)) { NotificationEmitter emitter = NotificationEmitter.class.cast(garbageCollectorMXBean); emitter.addNotificationListener(gcEventListener, null, null); } } // Set up metrics Map<String, Metric> metricsByNames = new HashMap<>(); if (fullCollectionsCounter != null) { this.fullCollectionsGauge = new Gauge<Long>() { @Override public Long getValue() { return fullCollectionsCounter.get(); } }; metricsByNames.put("fullCollections", fullCollectionsGauge); } metricsByNames.put("majorGcTimer", majorGcTimer); metricsByNames.put("minorGcTimer", minorGcTimer); List<GarbageCollectorStatus> garbageCollectorStatuses = new ArrayList<>(); for (final GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) { final String garbageCollectorName = garbageCollectorMXBean.getName(); final String garbageCollectorNamespace = MetricNamingUtil.join("collectors", MetricNamingUtil.sanitize(garbageCollectorName)); final Gauge<Long> collectionsGauge; if (garbageCollectorMXBean.getCollectionCount() >= 0) { collectionsGauge = new Gauge<Long>() { @Override public Long getValue() { return garbageCollectorMXBean.getCollectionCount(); } }; metricsByNames.put(MetricNamingUtil.join(garbageCollectorNamespace, "collections"), collectionsGauge); } else { collectionsGauge = null; } final Gauge<Long> totalCollectionDurationInMillisGauge; if (garbageCollectorMXBean.getCollectionTime() >= 0) { totalCollectionDurationInMillisGauge = new Gauge<Long>() { @Override public Long getValue() { return garbageCollectorMXBean.getCollectionTime(); } }; metricsByNames.put( MetricNamingUtil.join(garbageCollectorNamespace, "totalCollectionDurationInMillis"), totalCollectionDurationInMillisGauge); } else { totalCollectionDurationInMillisGauge = null; } garbageCollectorStatuses.add(new GarbageCollectorStatus() { @Override public String getName() { return garbageCollectorName; } @Override public Gauge<Long> getCollectionsGauge() { return collectionsGauge; } @Override public Gauge<Long> getTotalCollectionDurationInMillisGauge() { return totalCollectionDurationInMillisGauge; } }); } this.garbageCollectorStatuses = garbageCollectorStatuses; this.metricsByNames = metricsByNames; }
From source file:net.sbbi.upnp.jmx.upnp.UPNPConnectorServer.java
public void start() throws IOException { MBeanServer server = getMBeanServer(); if (exposeMBeansAsUPNP.booleanValue()) { try {//from w w w . j a v a 2 s. c om ObjectName delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate"); NotificationEmitter emmiter = (NotificationEmitter) MBeanServerInvocationHandler .newProxyInstance(server, delegate, NotificationEmitter.class, false); // register for MBeans registration emmiter.addNotificationListener(this, null, this); } catch (Exception ex) { IOException ioEx = new IOException("UPNPConnector start error"); ioEx.initCause(ex); throw ioEx; } } if (exposeUPNPAsMBeans.booleanValue()) { int timeout = 2500; if (env.containsKey(EXPOSE_UPNP_DEVICES_AS_MBEANS_TIMEOUT)) { timeout = ((Integer) env.get(EXPOSE_UPNP_DEVICES_AS_MBEANS_TIMEOUT)).intValue(); } try { discoveryBeanName = new ObjectName("UPNPLib discovery:name=Discovery MBean_" + this.hashCode()); UPNPDiscoveryMBean bean = new UPNPDiscovery(timeout, handleSSDPMessages.booleanValue(), true); server.registerMBean(bean, discoveryBeanName); } catch (Exception ex) { IOException ioEx = new IOException("Error occured during MBeans discovery"); ioEx.initCause(ex); throw ioEx; } } if (exposeExistingMBeansAsUPNP.booleanValue()) { int c = 0; Set objectInstances = super.getMBeanServer().queryNames(null, null); for (Iterator i = objectInstances.iterator(); i.hasNext();) { ObjectName name = (ObjectName) i.next(); MBeanServerNotification not = new MBeanServerNotification( MBeanServerNotification.REGISTRATION_NOTIFICATION, this, c++, name); handleNotification(not, this); } } }
From source file:org.apache.giraph.graph.GraphTaskManager.java
/** * Install GC monitoring. This method intercepts all GC, log the gc, and * notifies an out-of-core engine (if any is used) about the GC. */// www . ja v a 2 s .c o m private void installGCMonitoring() { List<GarbageCollectorMXBean> mxBeans = ManagementFactory.getGarbageCollectorMXBeans(); final OutOfCoreEngine oocEngine = serviceWorker.getServerData().getOocEngine(); for (GarbageCollectorMXBean gcBean : mxBeans) { NotificationEmitter emitter = (NotificationEmitter) gcBean; NotificationListener listener = new NotificationListener() { @Override public void handleNotification(Notification notification, Object handle) { if (notification.getType() .equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo .from((CompositeData) notification.getUserData()); if (LOG.isInfoEnabled()) { LOG.info("installGCMonitoring: name = " + info.getGcName() + ", action = " + info.getGcAction() + ", cause = " + info.getGcCause() + ", duration = " + info.getGcInfo().getDuration() + "ms"); } gcTimeMetric.inc(info.getGcInfo().getDuration()); if (oocEngine != null) { oocEngine.gcCompleted(info); } } } }; //Add the listener emitter.addNotificationListener(listener, null, null); } }
From source file:org.jboss.resteasy.test.response.ResponseStreamPrematurelyClosedTest.java
@Test public void testStream() throws Exception { Builder builder = client.target(generateURL("/test/document/abc/content")).request(); try (MyByteArrayOutputStream baos = new MyByteArrayOutputStream()) { if (!TestUtil.isIbmJdk()) { //builder.get().readEntity explicitly on the same line below and not saved in any temp variable //to let the JVM try finalizing the ClientResponse object InputStream ins = builder.get().readEntity(InputStream.class); //suggest jvm to do gc and wait the gc notification final CountDownLatch coutDown = new CountDownLatch(1); List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans(); NotificationListener listener = new NotificationListener() { public void handleNotification(Notification notification, Object handback) { coutDown.countDown(); }// w w w . j a va 2 s.c om }; try { for (GarbageCollectorMXBean gcbean : gcbeans) { NotificationEmitter emitter = (NotificationEmitter) gcbean; emitter.addNotificationListener(listener, null, null); } System.gc(); coutDown.await(10, TimeUnit.SECONDS); IOUtils.copy(ins, baos); Assert.assertEquals("Received string: " + baos.toShortString(), 10000000, baos.size()); } finally { //remove the listener for (GarbageCollectorMXBean gcbean : gcbeans) { ((NotificationEmitter) gcbean).removeNotificationListener(listener); } } } else { // workaround for Ibm jdk - doesn't allow to use NotificationEmitter with GarbageCollectorMXBean //builder.get().readEntity explicitly on the same line below and not saved in any temp variable //to let the JVM try finalizing the ClientResponse object IOUtils.copy(builder.get().readEntity(InputStream.class), baos); Assert.assertEquals(100000000, baos.size()); } } }