下面列出了怎么用javax.management.openmbean.InvalidKeyException的API类实例代码及写法,或者点击链接到github查看源代码。
private Set<StreamState> parseCompositeData(Set<CompositeData> payload) {
Set<StreamState> result = Sets.newHashSet();
for (CompositeData compositeData : payload) {
try {
// start by trying to parse with classes coming from Reaper's C* dependency
StreamState streamState = StreamStateCompositeData.fromCompositeData(compositeData);
result.add(streamState);
} catch (AssertionError | InvalidKeyException e) {
try {
// if that fails, try the old version
StreamState olderStreamState = parseStreamStatePre2_1(compositeData);
result.add(olderStreamState);
} catch (InvalidKeyException ie) {
// and if even that fails, try the new version
// if that still fails, exception goes up
StreamState newerStreamState = parseStreamState4_0_0(compositeData);
result.add(newerStreamState);
}
}
}
return result;
}
/**
* 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 = gcBean.getName().equals("ConcurrentMarkSweep") || gcBean.getName().equals("MarkSweepCompact") //$NON-NLS-1$ //$NON-NLS-2$
? 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();
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); //$NON-NLS-1$
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);
}
}