use of javax.management.NotificationListener in project dsl-devkit by dsldevkit.
the class VirtualMachineTracer method start.
/**
* Installs a listener that will publish all full GC events as {@link FullGarbageCollectionEvent} objects.
*/
public void start() {
// This code only works with Oracle HotSpot JVM as there is no standard API to retrieve information about GC events
if (!isHotSpotVM()) {
return;
}
long vmStartTime = getApproximateNanoStartTime();
for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
Class<? extends TraceEvent> eventType = // $NON-NLS-1$ //$NON-NLS-2$
gcBean.getName().equals("ConcurrentMarkSweep") || gcBean.getName().equals("MarkSweepCompact") ? FullGarbageCollectionEvent.class : MinorGarbageCollectionEvent.class;
NotificationEmitter emitter = (NotificationEmitter) gcBean;
NotificationListener listener = new NotificationListener() {
@Override
public void handleNotification(final Notification notification, final Object handback) {
try {
// we only handle GARBAGE_COLLECTION_NOTIFICATION notifications here
if (notification.getType().equals("com.sun.management.gc.notification")) {
// $NON-NLS-1$
CompositeData cd = (CompositeData) notification.getUserData();
// $NON-NLS-1$
String gcAction = (String) cd.get("gcAction");
// $NON-NLS-1$
String gcCause = (String) cd.get("gcCause");
// $NON-NLS-1$
CompositeData gcInfo = (CompositeData) cd.get("gcInfo");
// $NON-NLS-1$
long startTime = TimeUnit.NANOSECONDS.convert((Long) gcInfo.get("startTime"), TimeUnit.MILLISECONDS);
// $NON-NLS-1$
long duration = TimeUnit.NANOSECONDS.convert((Long) gcInfo.get("duration"), TimeUnit.MILLISECONDS);
if (duration > 0) {
// "startTime" and "duration" are relative to VM start time
traceSet.started(eventType, vmStartTime + startTime, gcAction, gcCause);
traceSet.ended(eventType, vmStartTime + startTime + duration);
}
}
} catch (InvalidKeyException e) {
// ignore
}
}
};
emitter.addNotificationListener(listener, null, null);
gcListenerMap.put(emitter, listener);
}
}
use of javax.management.NotificationListener in project openj9 by eclipse.
the class GarbageCollectionNotificationTest method beforeTest.
@BeforeTest
public void beforeTest() {
// get all GarbageCollectorMXBeans
List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
// register every GarbageCollectorMXBean
for (GarbageCollectorMXBean gcBean : gcBeans) {
gcStates.put(gcBean.getName(), new GCState(gcBean, assumeGCIsPartiallyConcurrent(gcBean), assumeGCIsOldGen(gcBean)));
NotificationEmitter emitter = (NotificationEmitter) gcBean;
// new listener
NotificationListener listener = new NotificationListener() {
// record total gc time spend
long totalGcTimeSpend = 0;
@Override
public void handleNotification(Notification notification, Object handback) {
HandBack handBack = (HandBack) handback;
// get gc info
GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
// output
// get a glance of gc
StringBuilder gcGlance = new StringBuilder();
String gcName = info.getGcName();
GCState gcState = gcStates.get(gcName);
GcInfo gcInfo = info.getGcInfo();
long gcId = gcInfo.getId();
gcGlance.append(info.getGcAction()).append(" (").append(gcName).append(") : - ").append(gcId);
gcGlance.append(" (").append(info.getGcCause()).append(") ");
gcGlance.append("start: ").append(dateFormat.format(new Date(JVM_START_TIME + gcInfo.getStartTime())));
gcGlance.append(", end: ").append(dateFormat.format(new Date(JVM_START_TIME + gcInfo.getEndTime())));
logger.debug(gcGlance.toString());
AssertJUnit.assertTrue("startTime(" + gcInfo.getStartTime() + ") should be smaller than endTime(" + gcInfo.getEndTime() + ").", gcInfo.getStartTime() <= gcInfo.getEndTime());
if (0 != gcState.lastCollectionID) {
AssertJUnit.assertTrue("lastCollectionID+1(" + (gcState.lastCollectionID + 1) + ") should match gcId(" + gcId + ")", gcId == (gcState.lastCollectionID + 1));
}
gcState.lastCollectionID = gcId;
gcState.totalCollectionCount += 1;
gcState.totalCollectionDuration += gcInfo.getDuration();
// memory info
Map<String, MemoryUsage> beforeUsageMap = gcInfo.getMemoryUsageBeforeGc();
Map<String, MemoryUsage> afterUsageMap = gcInfo.getMemoryUsageAfterGc();
for (Map.Entry<String, MemoryUsage> entry : afterUsageMap.entrySet()) {
String name = entry.getKey();
MemoryUsage afterUsage = entry.getValue();
MemoryUsage beforeUsage = beforeUsageMap.get(name);
StringBuilder usage = new StringBuilder();
// $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
usage.append("\t[").append(String.format("%32s", name)).append("] \t");
// $NON-NLS-1$ //$NON-NLS-2$
usage.append("init:").append(afterUsage.getInit() / ONE_KBYTE).append("K;\t");
usage.append("used:").append(// $NON-NLS-1$
handBack.handUsage(beforeUsage.getUsed(), afterUsage.getUsed(), beforeUsage.getMax())).append(// $NON-NLS-1$
";\t");
usage.append("committed: ").append(// $NON-NLS-1$
handBack.handUsage(beforeUsage.getCommitted(), afterUsage.getCommitted(), beforeUsage.getMax()));
logger.debug(usage.toString());
}
totalGcTimeSpend += gcInfo.getDuration();
// summary
long percent = (gcInfo.getEndTime() - totalGcTimeSpend) * 1000L / gcInfo.getEndTime();
StringBuilder summary = new StringBuilder();
summary.append("duration:").append(gcInfo.getDuration()).append("ms");
// $NON-NLS-1$//$NON-NLS-2$
summary.append(", throughput:").append((percent / 10)).append(".").append(percent % 10).append('%');
logger.debug(summary.toString());
logger.debug("\n");
}
};
// add the listener
emitter.addNotificationListener(listener, new NotificationFilter() {
private static final long serialVersionUID = 3763793138186359389L;
@Override
public boolean isNotificationEnabled(Notification notification) {
// filter GC notification
return notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION);
}
}, HandBack.getInstance());
}
}
use of javax.management.NotificationListener in project tomcat70 by apache.
the class ConnectionPool method notify.
/**
* Return true if the notification was sent successfully, false otherwise.
* @param type
* @param message
* @return true if the notification succeeded
*/
public boolean notify(final String type, String message) {
try {
Notification n = new Notification(type, this, sequence.incrementAndGet(), System.currentTimeMillis(), "[" + type + "] " + message);
sendNotification(n);
for (NotificationListener listener : listeners) {
listener.handleNotification(n, this);
}
return true;
} catch (Exception x) {
if (log.isDebugEnabled()) {
log.debug("Notify failed. Type=" + type + "; Message=" + message, x);
}
return false;
}
}
use of javax.management.NotificationListener in project deltaspike by apache.
the class SimpleRegistrationTest method checkMBean.
@Test
public void checkMBean() throws Exception {
assertEquals(0, myMBean.getCounter());
myMBean.resetTo(2);
final ObjectName on = new ObjectName("org.apache.deltaspike:type=MBeans,name=" + MyMBean.class.getName());
assertTrue(server.isRegistered(on));
assertEquals(2, server.getAttribute(on, "counter"));
assertEquals(6, server.invoke(on, "multiply", new Object[] { 3 }, new String[0]));
myMBean.resetTo(5);
assertEquals(5, server.getAttribute(on, "counter"));
assertEquals(20, server.invoke(on, "multiply", new Object[] { 4 }, new String[0]));
server.setAttribute(on, new Attribute("counter", 10));
assertEquals(10, myMBean.getCounter());
final Collection<Notification> notifications = new ArrayList<Notification>();
server.addNotificationListener(on, new NotificationListener() {
@Override
public void handleNotification(final Notification notification, final Object handback) {
notifications.add(notification);
}
}, null, null);
myMBean.broadcast();
assertEquals(1, notifications.size());
assertEquals(10L, notifications.iterator().next().getSequenceNumber());
MBeanInfo mBeanInfo = server.getMBeanInfo(on);
Assert.assertNotNull(mBeanInfo);
MBeanOperationInfo[] operations = mBeanInfo.getOperations();
Assert.assertNotNull(operations);
Assert.assertTrue(operations.length > 0);
Assert.assertTrue("Empty Signature on operation: " + operations[1], operations[1].getSignature().length > 0);
MBeanParameterInfo parameterInfo = operations[1].getSignature()[0];
assertEquals("multiplier", parameterInfo.getName());
assertEquals("the multiplier", parameterInfo.getDescription());
{
// table support - through map
Object table = server.getAttribute(on, "table");
assertTrue(TabularData.class.isInstance(table));
final TabularData data = TabularData.class.cast(table);
assertEquals(1, data.size());
final CompositeData compositeData = CompositeData.class.cast(data.values().iterator().next());
assertEquals(2, compositeData.values().size());
assertEquals("value1", compositeData.get("key1"));
assertEquals("value2", compositeData.get("key2"));
}
{
// table support - through Table
Object table = server.getAttribute(on, "table2");
assertTrue(TabularData.class.isInstance(table));
final TabularData data = TabularData.class.cast(table);
assertEquals(2, data.size());
final Iterator<?> iterator = data.values().iterator();
{
final CompositeData compositeData = CompositeData.class.cast(iterator.next());
assertEquals(3, compositeData.values().size());
assertEquals("1", compositeData.get("a"));
assertEquals("2", compositeData.get("b"));
assertEquals("3", compositeData.get("c"));
}
{
final CompositeData compositeData = CompositeData.class.cast(iterator.next());
assertEquals(3, compositeData.values().size());
assertEquals("alpha", compositeData.get("a"));
assertEquals("beta", compositeData.get("b"));
assertEquals("gamma", compositeData.get("c"));
}
}
}
use of javax.management.NotificationListener in project hive by apache.
the class HeapMemoryMonitor method start.
public void start() {
// unsupported if null
if (tenuredGenPool == null) {
return;
}
MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
NotificationEmitter emitter = (NotificationEmitter) mxBean;
notificationListener = (n, hb) -> {
if (n.getType().equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
long maxMemory = tenuredGenPool.getUsage().getMax();
long usedMemory = tenuredGenPool.getUsage().getUsed();
for (Listener listener : listeners) {
listener.memoryUsageAboveThreshold(usedMemory, maxMemory);
}
}
};
emitter.addNotificationListener(notificationListener, null, null);
}
Aggregations