下面列出了怎么用javax.management.Notification的API类实例代码及写法,或者点击链接到github查看源代码。
public void handleNotification(Notification notification,
Object handback) {
if (notification != null) {
if (notification.getSource() == mbean)
notification.setSource(name);
}
/*
* Listeners are not supposed to throw exceptions. If
* this one does, we could remove it from the MBean. It
* might indicate that a connector has stopped working,
* for instance, and there is no point in sending future
* notifications over that connection. However, this
* seems rather drastic, so instead we propagate the
* exception and let the broadcaster handle it.
*/
listener.handleNotification(notification, handback);
}
/**
* Check that the received notifications are the epxected ones.
* Here we expect that:
* <ul>
* <li>the number of tasks is a multiple of the total number of nodes</li>
* <li>the tasks are evenly spread among the nodes</li>
* </ul>
* @param notifs the notifications that were received.
* @param nbTasks the total number of takss that were executed.
* @param expectedNodes the nodes from which notifications should have been received.
* @throws Exception if any error occurs.
*/
private static void checkNotifs(final List<Notification> notifs, final int nbTasks, final String...expectedNodes) throws Exception {
assertNotNull(expectedNodes);
assertTrue(expectedNodes.length > 0);
final Set<String> expectedNodesSet = CollectionUtils.set(expectedNodes);
assertNotNull(notifs);
final int nbNotifsPerNode = nbTasks / allNodes.size();
assertEquals(expectedNodes.length * nbNotifsPerNode, notifs.size());
final Map<String, AtomicInteger> notifCounts = new HashMap<>();
for (final String uuid: expectedNodes) notifCounts.put(uuid, new AtomicInteger(0));
for (final Notification notification: notifs) {
assertTrue(notification instanceof JPPFNodeForwardingNotification);
final JPPFNodeForwardingNotification outerNotif = (JPPFNodeForwardingNotification) notification;
assertEquals(NodeTestMBean.MBEAN_NAME, outerNotif.getMBeanName());
final Notification notif = outerNotif.getNotification();
assertTrue(notif.getUserData() instanceof UserObject);
final UserObject userObject = (UserObject) notif.getUserData();
assertNotNull(userObject.nodeUuid);
assertTrue(expectedNodesSet.contains(userObject.nodeUuid));
assertEquals(outerNotif.getNodeUuid(), userObject.nodeUuid);
notifCounts.get(userObject.nodeUuid).incrementAndGet();
}
for (final Map.Entry<String, AtomicInteger> entry: notifCounts.entrySet()) {
assertEquals(nbNotifsPerNode, entry.getValue().get());
}
}
@Override
public void handleNotification(Notification notification,
Object handback) {
notificationCount.incrementAndGet();
System.out.println("\nReceived notification:");
System.out.println("\tClassName: " + notification.getClass().getName());
System.out.println("\tSource: " + notification.getSource());
System.out.println("\tType: " + notification.getType());
System.out.println("\tMessage: " + notification.getMessage());
if (notification instanceof AttributeChangeNotification) {
AttributeChangeNotification acn =
(AttributeChangeNotification) notification;
System.out.println("\tAttributeName: " + acn.getAttributeName());
System.out.println("\tAttributeType: " + acn.getAttributeType());
System.out.println("\tNewValue: " + acn.getNewValue());
System.out.println("\tOldValue: " + acn.getOldValue());
}
}
public synchronized void handleNotification(Notification notification,
Object handback) {
Utils.debug(Utils.DEBUG_STANDARD,
"SimpleListener::handleNotification :" + notification);
try {
received = true;
type = notification.getType();
this.handback = handback;
handbacks.add(handback);
nbrec++;
notify();
} catch(Exception e) {
System.out.println("(ERROR) SimpleListener::handleNotification :"
+ " Caught exception "
+ e) ;
}
}
void createGCNotification(long timestamp,
String gcName,
String gcAction,
String gcCause,
GcInfo gcInfo) {
if (!hasListeners()) {
return;
}
Notification notif = new Notification(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION,
getObjectName(),
getNextSeqNumber(),
timestamp,
gcName);
GarbageCollectionNotificationInfo info =
new GarbageCollectionNotificationInfo(gcName,
gcAction,
gcCause,
gcInfo);
CompositeData cd =
GarbageCollectionNotifInfoCompositeData.toCompositeData(info);
notif.setUserData(cd);
sendNotification(notif);
}
public void handleNotification(Notification notification, Object handback) {
echo(">>> Received notification: " + notification);
if (notification instanceof MonitorNotification) {
String type = notification.getType();
if (type.equals(MonitorNotification.RUNTIME_ERROR)) {
MonitorNotification mn = (MonitorNotification) notification;
echo("\tType: " + mn.getType());
echo("\tTimeStamp: " + mn.getTimeStamp());
echo("\tObservedObject: " + mn.getObservedObject());
echo("\tObservedAttribute: " + mn.getObservedAttribute());
echo("\tDerivedGauge: " + mn.getDerivedGauge());
echo("\tTrigger: " + mn.getTrigger());
synchronized (this) {
messageReceived = true;
notifyAll();
}
}
}
}
/**
* This MBean emits three kind of notifications:
* <pre>
* <i>com.sun.jmx.examples.scandir.log.file.switched</i>
* <i>com.sun.jmx.examples.scandir.log.memory.full</i>
* <i>com.sun.jmx.examples.scandir.log.memory.cleared</i>
* </pre>
**/
public MBeanNotificationInfo[] getNotificationInfo() {
return new MBeanNotificationInfo[] {
new MBeanNotificationInfo(new String[] {
LOG_FILE_CHANGED},
Notification.class.getName(),
"Emitted when the log file is switched")
,
new MBeanNotificationInfo(new String[] {
MEMORY_LOG_MAX_CAPACITY},
Notification.class.getName(),
"Emitted when the memory log capacity is reached")
,
new MBeanNotificationInfo(new String[] {
MEMORY_LOG_CLEARED},
Notification.class.getName(),
"Emitted when the memory log is cleared")
};
}
private boolean notifyMatch(File file) {
try {
final Notification n =
new Notification(FILE_MATCHES_NOTIFICATION,this,
getNextSeqNumber(),
file.getAbsolutePath());
// This method *is not* called from any synchronized block, so
// we can happily call broadcaster.sendNotification() here.
// Note that verifying whether a method is called from within
// a synchronized block demends a thoroughful code reading,
// examining each of the 'parent' methods in turn.
//
broadcaster.sendNotification(n);
return true;
} catch (Exception x) {
LOG.fine("Failed to notify: "+file.getAbsolutePath());
}
return false;
}
public MBeanNotificationInfo[] getNotificationInfo() {
if (notifDescriptorAtt == null) {
initNotifDescriptorAtt();
}
return new MBeanNotificationInfo[]{
new MBeanNotificationInfo(new String[]{
NOTIF_TYPE_0
},
javax.management.Notification.class.getName(),
"Standard JMX Notification",
notifDescriptorAtt),
new MBeanNotificationInfo(new String[]{
NOTIF_TYPE_1
},
SqeNotification.class.getName(),
"SQE Notification",
notifDescriptorAtt)
};
}
public static void main(String[] args) throws Exception
{
// Create the service
LegacyService legacyService = new LegacyService();
// Create the JMX MBeanServer and register the service wrapper
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName serviceName = new ObjectName("examples", "mbean", "legacy");
DynamicLegacyService dynamicService = new DynamicLegacyService(legacyService);
server.registerMBean(dynamicService, serviceName);
// Now register a listener: we want to be able to know when the service starts and stops
server.addNotificationListener(serviceName, new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
System.out.println(notification);
}
}, null, null);
// Now start the service, using the new method name: 'start' instead of 'execute'
server.invoke(serviceName, "start", null, null);
}
private boolean notifyMatch(File file) {
try {
final Notification n =
new Notification(FILE_MATCHES_NOTIFICATION,this,
getNextSeqNumber(),
file.getAbsolutePath());
// This method *is not* called from any synchronized block, so
// we can happily call broadcaster.sendNotification() here.
// Note that verifying whether a method is called from within
// a synchronized block demends a thoroughful code reading,
// examining each of the 'parent' methods in turn.
//
broadcaster.sendNotification(n);
return true;
} catch (Exception x) {
LOG.fine("Failed to notify: "+file.getAbsolutePath());
}
return false;
}
private DistributedMember removeMemberArtifacts(DistributedMember member, boolean crashed) {
Region<String, Object> proxyRegion = repo.getEntryFromMonitoringRegionMap(member);
Region<NotificationKey, Notification> notifRegion = repo.getEntryFromNotifRegionMap(member);
if (proxyRegion == null && notifRegion == null) {
return member;
}
repo.romoveEntryFromMonitoringRegionMap(member);
repo.removeEntryFromNotifRegionMap(member);
// If cache is closed all the regions would have been
// destroyed implicitly
if (!cache.isClosed()) {
proxyFactory.removeAllProxies(member, proxyRegion);
proxyRegion.localDestroyRegion();
notifRegion.localDestroyRegion();
}
if (!cache.getDistributedSystem().getDistributedMember().equals(member)) {
service.memberDeparted((InternalDistributedMember) member, crashed);
}
return member;
}
public void fetchNotification(
String connectionId,
ObjectName name,
Notification notification,
Subject subject)
throws SecurityException {
echo("fetchNotification:");
echo("\tconnectionId: " + connectionId);
echo("\tname: " + name);
echo("\tnotification: " + notification);
echo("\tsubject: " +
(subject == null ? null : subject.getPrincipals()));
if (!throwException)
if (name.getCanonicalName().equals("domain:name=2,type=NB") &&
subject.getPrincipals().contains(new JMXPrincipal("role")))
throw new SecurityException();
}
public MBeanNotificationInfo[] getNotificationInfo() {
if (notifDescriptorAtt == null) {
initNotifDescriptorAtt();
}
return new MBeanNotificationInfo[]{
new MBeanNotificationInfo(new String[]{
NOTIF_TYPE_0
},
javax.management.Notification.class.getName(),
"Standard JMX Notification",
notifDescriptorAtt),
new MBeanNotificationInfo(new String[]{
NOTIF_TYPE_1
},
SqeNotification.class.getName(),
"SQE Notification",
notifDescriptorAtt)
};
}
public void unregisterMBean(ObjectName name)
throws InstanceNotFoundException, MBeanRegistrationException {
forbidJMImpl(name);
DynamicMBean mbean = getMBean(name);
if (mbean == null)
throw new InstanceNotFoundException(name.toString());
MBeanRegistration reg = mbeanRegistration(mbean);
try {
reg.preDeregister();
} catch (Exception e) {
throw new MBeanRegistrationException(e);
}
if (!mbeans.remove(name, mbean))
throw new InstanceNotFoundException(name.toString());
// This is incorrect because we've invoked preDeregister
Object userMBean = getUserMBean(mbean);
if (userMBean instanceof ClassLoader)
clr.removeLoader((ClassLoader) userMBean);
Notification n = new MBeanServerNotification(
MBeanServerNotification.REGISTRATION_NOTIFICATION,
MBeanServerDelegate.DELEGATE_NAME,
0,
name);
delegate.sendNotification(n);
reg.postDeregister();
}
/**
* Assumption is its a cache server instance. For Gateway receiver there will
* be a separate method
*
* @param server
* cache server instance
*/
public void handleCacheServerStop(CacheServer server) {
if (!isServiceInitialised("handleCacheServerStop")) {
return;
}
CacheServerMBean mbean = (CacheServerMBean) service
.getLocalCacheServerMXBean(server.getPort());
BridgeMembershipListener listener = mbean.getBridge()
.getBridgeMembershipListener();
if(listener != null){
BridgeMembership.unregisterBridgeMembershipListener(listener);
}
mbean.stopMonitor();
ObjectName cacheServerMBeanName = MBeanJMXAdapter
.getClientServiceMBeanName(server.getPort(), cacheImpl
.getDistributedSystem().getDistributedMember());
service.unregisterMBean(cacheServerMBeanName);
Notification notification = new Notification(
ResourceNotification.CACHE_SERVER_STOPPED, memberSource, SequenceNumber
.next(), System.currentTimeMillis(),
ResourceNotification.CACHE_SERVER_STOPPED_PREFIX);
memberLevelNotifEmitter.sendNotification(notification);
}
private void doTestMBeanServerNotification_UNREGISTRATION_NOTIFICATION(MBeanServerConnection connection, boolean mustReceiveNotification) throws Exception {
final ObjectName testObjectName = createObjectName(LEGACY_DOMAIN + ":subsystem=test");
final ObjectName childObjectName = createObjectName(LEGACY_DOMAIN + ":subsystem=test,single=only");
final CountDownLatch notificationEmitted = new CountDownLatch(1);
final AtomicReference<Notification> notification = new AtomicReference<>();
NotificationListener listener = new MbeanServerNotificationListener(notification, notificationEmitted, LEGACY_DOMAIN);
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
connection.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, listener, filter, null);
// add a management resource
connection.invoke(testObjectName, "addSingleOnly", new Object[]{123}, new String[]{String.class.getName()});
// and remove it
connection.invoke(childObjectName, "remove", new Object[0], new String[0]);
if (mustReceiveNotification) {
Assert.assertTrue("Did not receive expected notification", notificationEmitted.await(1, TimeUnit.SECONDS));
Notification notif = notification.get();
Assert.assertNotNull(notif);
Assert.assertTrue(notif instanceof MBeanServerNotification);
MBeanServerNotification mBeanServerNotification = (MBeanServerNotification) notif;
Assert.assertEquals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION, mBeanServerNotification.getType());
Assert.assertEquals(childObjectName, mBeanServerNotification.getMBeanName());
} else {
Assert.assertFalse("Did receive unexpected notification", notificationEmitted.await(500, TimeUnit.MILLISECONDS));
}
connection.removeNotificationListener(MBeanServerDelegate.DELEGATE_NAME, listener, filter, null);
}
public static MBeanNotificationInfo[] getDefaultNotificationInfo() {
String[] types = new String[] {NOTIFY_INIT, NOTIFY_CONNECT, NOTIFY_ABANDON, SLOW_QUERY_NOTIFICATION,
FAILED_QUERY_NOTIFICATION, SUSPECT_ABANDONED_NOTIFICATION, POOL_EMPTY, SUSPECT_RETURNED_NOTIFICATION};
String name = Notification.class.getName();
String description = "A connection pool error condition was met.";
MBeanNotificationInfo info = new MBeanNotificationInfo(types, name, description);
return new MBeanNotificationInfo[] {info};
}
public void handleNotification(Notification n, Object hb) {
if (!stopping) {
received++;
System.out.println(
">>> myListener-handleNotification: received " +
n.getSequenceNumber());
}
}
private void sendNotification(String type, String connectionId,
String message, Object userData) {
Notification notif =
new JMXConnectionNotification(type,
getNotificationSource(),
connectionId,
nextSequenceNumber(),
message,
userData);
sendNotification(notif);
}
public void handleNotification(Notification n, Object hb) {
if (!stopping) {
received++;
System.out.println(
">>> myListener-handleNotification: received " +
n.getSequenceNumber());
}
}
protected void doStart() throws IOException {
// Get RMIServer stub from directory or URL encoding if needed.
RMIServer stub;
try {
stub = (rmiServer!=null)?rmiServer:
findRMIServer(jmxServiceURL, env);
} catch (NamingException ne) {
throw new IOException("Failed to get a RMI stub: "+ne);
}
// Connect IIOP Stub if needed.
stub = connectStub(stub,env);
// Calling newClient on the RMIServer stub.
Object credentials = env.get(CREDENTIALS);
connection = stub.newClient(credentials);
// notif issues
final ClientListenerInfo[] old = rmiNotifClient.preReconnection();
reconnectNotificationListeners(old);
connectionId = getConnectionId();
Notification reconnectedNotif =
new JMXConnectionNotification(JMXConnectionNotification.OPENED,
this,
connectionId,
clientNotifSeqNo++,
"Reconnected to server",
null);
sendNotification(reconnectedNotif);
}
public void sendNotification(String ntfyText)
throws MBeanException, RuntimeOperationsException {
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
RequiredModelMBean.class.getName(),
"sendNotification(String)","Entry");
}
if (ntfyText == null)
throw new RuntimeOperationsException(new
IllegalArgumentException("notification message must not "+
"be null"),
"Exception occurred trying to send a text notification "+
"from a ModelMBean");
Notification myNtfyObj = new Notification("jmx.modelmbean.generic",
this, 1, ntfyText);
sendNotification(myNtfyObj);
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
RequiredModelMBean.class.getName(),
"sendNotification(String)","Notification sent");
MODELMBEAN_LOGGER.logp(Level.FINER,
RequiredModelMBean.class.getName(),
"sendNotification(String)","Exit");
}
}
public void handleNotification(Notification n, Object h) {
echo("handleNotification:");
echo("\tNotification = " + n);
echo("\tNotification.SeqNum = " + n.getSequenceNumber());
echo("\tHandback = " + h);
notifs.add(n);
}
public void handleNotification(Notification notif, Object handback) {
if(++receivedNotifs == nb) {
synchronized(lock) {
lock.notifyAll();
}
}
}
/**
* Create a new ScanManager MBean
**/
public ScanManager() {
broadcaster = new NotificationBroadcasterSupport();
pendingNotifs = new LinkedBlockingQueue<Notification>(100);
scanmap = newConcurrentHashMap();
configmap = newConcurrentHashMap();
log = new ResultLogManager();
}
public void apply(List<TargetedNotification> targetedNotifs,
ObjectName source, Notification notif) {
// We proceed in two stages here, to avoid holding the listenerMap
// lock while invoking the filters (which are user code).
final IdAndFilter[] candidates;
synchronized (listenerMap) {
final Set<IdAndFilter> set = listenerMap.get(source);
if (set == null) {
logger.debug("bufferFilter", "no listeners for this name");
return;
}
candidates = new IdAndFilter[set.size()];
set.toArray(candidates);
}
// We don't synchronize on targetedNotifs, because it is a local
// variable of our caller and no other thread can see it.
for (IdAndFilter idaf : candidates) {
final NotificationFilter nf = idaf.getFilter();
if (nf == null || nf.isNotificationEnabled(notif)) {
logger.debug("bufferFilter", "filter matches");
final TargetedNotification tn =
new TargetedNotification(notif, idaf.getId());
if (allowNotificationEmission(source, tn))
targetedNotifs.add(tn);
}
}
}
public void sendNotificationToMe(String name) {
long sequence = sequenceNumber++;
Notification n = new AttributeChangeNotification(this, sequenceNumber, System.currentTimeMillis(),
name, "A", "long", sequence, sequenceNumber);
n.setSource(RemoteTestModule.getMyVmid());
sendNotification(n);
}
public void handleNotification(Notification notif, Object handback) {
System.out.println("Received: " + notif + " (" +
notif.getSequenceNumber() + ")");
synchronized(lock) {
if(++receivedNotifs == nb) {
lock.notifyAll();
} else if (receivedNotifs > nb) {
System.out.println("The client expected to receive " +
nb + " notifs, but got at least " +
receivedNotifs);
System.exit(1);
}
}
}
/**
* Check whether a new log file should be created.
* If a new file needs to be created, creates it, renaming
* previously existing file by appending '~' to its name.
* Also reset the log count and file capacity.
* Sends a notification indicating that the log file was changed.
* Returns the new log stream;
* Creation of a new file can be forced by passing force=true.
**/
private OutputStream checkLogFile(String basename, long maxRecords,
boolean force)
throws IOException {
final OutputStream newStream;
synchronized(this) {
if ((force==false) && (logCount < maxRecords))
return logStream;
final OutputStream oldStream = logStream;
// First close the stream. On some platforms you cannot rename
// a file that has open streams...
//
if (oldStream != null) {
oldStream.flush();
oldStream.close();
}
final File newFile = (basename==null)?null:createNewLogFile(basename);
newStream = (newFile==null)?null:new FileOutputStream(newFile,true);
logStream = newStream;
logFile = newFile;
fileCapacity = maxRecords;
logCount = 0;
}
sendNotification(new Notification(LOG_FILE_CHANGED,objectName,
getNextSeqNumber(),
basename));
return newStream;
}