下面列出了怎么用javax.management.NotificationListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Remove a notification event listener from this MBean.
*
* @param listener The listener to be removed (any and all registrations
* for this listener will be eliminated)
*
* @exception ListenerNotFoundException if this listener is not
* registered in the MBean
*/
@Override
public void removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if (generalBroadcaster != null) {
generalBroadcaster.removeNotificationListener(listener);
}
if (attributeBroadcaster != null) {
attributeBroadcaster.removeNotificationListener(listener);
}
}
private NotificationListener wrappedListener(
ObjectName name, Object userMBean, NotificationListener userListener)
throws InstanceNotFoundException {
ListenerTable table = new ListenerTable();
ListenerTable oldTable = listenerMap.putIfAbsent(name, table);
if (oldTable != null)
table = oldTable;
NotificationListener identityListener =
new IdentityListener(userListener);
synchronized (table) {
NotificationListener rewriteListener = null;
WeakReference<NotificationListener> wr =
table.get(identityListener);
if (wr != null)
rewriteListener = wr.get();
if (rewriteListener == null) {
rewriteListener = new RewriteListener(
name, userMBean, identityListener);
wr = new WeakReference<NotificationListener>(rewriteListener);
table.put(identityListener, wr);
}
return rewriteListener;
}
}
/**
* Remove a notification event listener from this MBean.
*
* @param listener The listener to be removed (any and all registrations
* for this listener will be eliminated)
*
* @exception ListenerNotFoundException if this listener is not
* registered in the MBean
*/
@Override
public void removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if (generalBroadcaster != null) {
generalBroadcaster.removeNotificationListener(listener);
}
if (attributeBroadcaster != null) {
attributeBroadcaster.removeNotificationListener(listener);
}
}
private NotificationListener getListener(ObjectName listener)
throws ListenerNotFoundException {
// ----------------
// Get listener object
// ----------------
DynamicMBean instance;
try {
instance = getMBean(listener);
} catch (InstanceNotFoundException e) {
throw EnvHelp.initCause(
new ListenerNotFoundException(e.getMessage()), e);
}
Object resource = getResource(instance);
if (!(resource instanceof NotificationListener)) {
final RuntimeException exc =
new IllegalArgumentException(listener.getCanonicalName());
final String msg =
"MBean " + listener.getCanonicalName() + " does not " +
"implement " + NotificationListener.class.getName();
throw new RuntimeOperationsException(exc, msg);
}
return (NotificationListener) resource;
}
/**
* Removes a listener for Notifications from the RequiredModelMBean.
*
* @param listener The listener name which was handling notifications
* emitted by the registered MBean.
* This method will remove all information related to this listener.
*
* @exception ListenerNotFoundException The listener is not registered
* in the MBean or is null.
*
* @see #addNotificationListener
**/
public void removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException {
if (listener == null)
throw new ListenerNotFoundException(
"Notification listener is null");
final String mth="removeNotificationListener(NotificationListener)";
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
RequiredModelMBean.class.getName(), mth, "Entry");
}
if (generalBroadcaster == null)
throw new ListenerNotFoundException(
"No notification listeners registered");
generalBroadcaster.removeNotificationListener(listener);
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER,
RequiredModelMBean.class.getName(), mth, "Exit");
}
}
private NotificationListener wrappedListener(
ObjectName name, Object userMBean, NotificationListener userListener)
throws InstanceNotFoundException {
ListenerTable table = new ListenerTable();
ListenerTable oldTable = listenerMap.putIfAbsent(name, table);
if (oldTable != null)
table = oldTable;
NotificationListener identityListener =
new IdentityListener(userListener);
synchronized (table) {
NotificationListener rewriteListener = null;
WeakReference<NotificationListener> wr =
table.get(identityListener);
if (wr != null)
rewriteListener = wr.get();
if (rewriteListener == null) {
rewriteListener = new RewriteListener(
name, userMBean, identityListener);
wr = new WeakReference<NotificationListener>(rewriteListener);
table.put(identityListener, wr);
}
return rewriteListener;
}
}
public void removeNotificationListener(ObjectName name,
ObjectName listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException, ListenerNotFoundException {
NotificationListener instance = getListener(listener);
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER,
DefaultMBeanServerInterceptor.class.getName(),
"removeNotificationListener",
"ObjectName = " + name + ", Listener = " + listener);
}
server.removeNotificationListener(name, instance, filter, handback);
}
public void addNotificationListener(ObjectName name,
NotificationListener listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException,
IOException {
final boolean debug = logger.debugOn();
if (debug)
logger.debug("addNotificationListener" +
"(ObjectName,NotificationListener,"+
"NotificationFilter,Object)",
"name=" + name
+ ", listener=" + listener
+ ", filter=" + filter
+ ", handback=" + handback);
final Integer listenerID =
addListenerWithSubject(name,
new MarshalledObject<NotificationFilter>(filter),
delegationSubject,true);
rmiNotifClient.addNotificationListener(listenerID, name, listener,
filter, handback,
delegationSubject);
}
private NotificationListener getListenerWrapper(NotificationListener l,
ObjectName name,
DynamicMBean mbean,
boolean create) {
Object resource = getResource(mbean);
ListenerWrapper wrapper = new ListenerWrapper(l, name, resource);
synchronized (listenerWrappers) {
WeakReference<ListenerWrapper> ref = listenerWrappers.get(wrapper);
if (ref != null) {
NotificationListener existing = ref.get();
if (existing != null)
return existing;
}
if (create) {
ref = new WeakReference<ListenerWrapper>(wrapper);
listenerWrappers.put(wrapper, ref);
return wrapper;
} else
return null;
}
}
public void removeNotificationListener(ObjectName name,
ObjectName listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException, ListenerNotFoundException {
NotificationListener instance = getListener(listener);
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER,
DefaultMBeanServerInterceptor.class.getName(),
"removeNotificationListener",
"ObjectName = " + name + ", Listener = " + listener);
}
server.removeNotificationListener(name, instance, filter, handback);
}
public void addNotificationListener(ObjectName name,
NotificationListener listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException,
IOException {
final boolean debug = logger.debugOn();
if (debug)
logger.debug("addNotificationListener" +
"(ObjectName,NotificationListener,"+
"NotificationFilter,Object)",
"name=" + name
+ ", listener=" + listener
+ ", filter=" + filter
+ ", handback=" + handback);
final Integer listenerID =
addListenerWithSubject(name,
new MarshalledObject<NotificationFilter>(filter),
delegationSubject,true);
rmiNotifClient.addNotificationListener(listenerID, name, listener,
filter, handback,
delegationSubject);
}
public void removeNotificationListener(ObjectName name,
ObjectName listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException, ListenerNotFoundException {
NotificationListener instance = getListener(listener);
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER,
DefaultMBeanServerInterceptor.class.getName(),
"removeNotificationListener",
"ObjectName = " + name + ", Listener = " + listener);
}
server.removeNotificationListener(name, instance, filter, handback);
}
/**
* Add an attribute change notification event listener to this MBean.
*
* @param listener Listener that will receive event notifications
* @param name Name of the attribute of interest, or <code>null</code>
* to indicate interest in all attributes
* @param handback Handback object to be sent along with event
* notifications
*
* @exception IllegalArgumentException if the listener parameter is null
*/
@Override
public void addAttributeChangeNotificationListener
(NotificationListener listener, String name, Object handback)
throws IllegalArgumentException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if (attributeBroadcaster == null)
attributeBroadcaster = new BaseNotificationBroadcaster();
if( log.isDebugEnabled() )
log.debug("addAttributeNotificationListener " + listener);
BaseAttributeFilter filter = new BaseAttributeFilter(name);
attributeBroadcaster.addNotificationListener
(listener, filter, handback);
}
/**
* Enable to remove an SNMP entry listener from this
* <CODE>SnmpMibTable</CODE>.
*
* @param listener The listener object which will handle the
* notifications emitted by the registered MBean.
* This method will remove all the information related to this
* listener.
*
* @exception ListenerNotFoundException The listener is not registered
* in the MBean.
*/
@Override
public synchronized void
removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException {
// looking for listener in handbackTable
//
java.util.Vector<?> handbackList = handbackTable.get(listener) ;
if ( handbackList == null ) {
throw new ListenerNotFoundException("listener");
}
// If handback is null, remove the listener entry
//
handbackTable.remove(listener) ;
filterTable.remove(listener) ;
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public void removeNotificationListener(ObjectName name,
NotificationListener listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException, ListenerNotFoundException {
checkRead();
getMBeanServer().removeNotificationListener(name, listener,
filter, handback);
}
public ListenerInfo(NotificationListener listener,
NotificationFilter filter,
Object handback) {
this.listener = listener;
this.filter = filter;
this.handback = handback;
}
public void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName) throws RuntimeOperationsException, ListenerNotFoundException
{
try
{
removeAttributeChangeNotificationListener(listener, attributeName, null);
}
catch (MBeanException e)
{
throw new RuntimeOperationsException(new RuntimeException(e.getMessage()));
}
}
/**
* MBean Notification support
* You shouldn't update these methods
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
public void addNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
throws IllegalArgumentException {
broadcaster.addNotificationListener(listener, filter, handback);
}
public void removeNotificationListener(
ObjectName name, ObjectName listener,
NotificationFilter filter, Object handback)
throws InstanceNotFoundException, ListenerNotFoundException {
NotificationListener nl =
(NotificationListener) getUserMBean(listener);
removeNotificationListener(name, nl, filter, handback);
}
/**
* Add a notification event listener to this MBean.
*
* @param listener Listener that will receive event notifications
* @param filter Filter object used to filter event notifications
* actually delivered, or <code>null</code> for no filtering
* @param handback Handback object to be sent along with event
* notifications
*
* @exception IllegalArgumentException if the listener parameter is null
*/
@Override
public void addNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
throws IllegalArgumentException {
if (listener == null)
throw new IllegalArgumentException("Listener is null");
if( log.isDebugEnabled() ) log.debug("addNotificationListener " + listener);
if (generalBroadcaster == null)
generalBroadcaster = new BaseNotificationBroadcaster();
generalBroadcaster.addNotificationListener
(listener, filter, handback);
// We'll send the attribute change notifications to all listeners ( who care )
// The normal filtering can be used.
// The problem is that there is no other way to add attribute change listeners
// to a model mbean ( AFAIK ). I suppose the spec should be fixed.
if (attributeBroadcaster == null)
attributeBroadcaster = new BaseNotificationBroadcaster();
if( log.isDebugEnabled() )
log.debug("addAttributeNotificationListener " + listener);
attributeBroadcaster.addNotificationListener
(listener, filter, handback);
}
public void
addConnectionNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback) {
if (listener == null)
throw new NullPointerException("listener");
connectionBroadcaster.addNotificationListener(listener, filter,
handback);
}
public void addNotificationListener(ObjectName name,
ObjectName listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException {
// ------------------------------
// ------------------------------
// ----------------
// Get listener object
// ----------------
DynamicMBean instance = getMBean(listener);
Object resource = getResource(instance);
if (!(resource instanceof NotificationListener)) {
throw new RuntimeOperationsException(new
IllegalArgumentException(listener.getCanonicalName()),
"The MBean " + listener.getCanonicalName() +
" does not implement the NotificationListener interface") ;
}
// ----------------
// Add a listener on an MBean
// ----------------
if (MBEANSERVER_LOGGER.isLoggable(Level.TRACE)) {
MBEANSERVER_LOGGER.log(Level.TRACE,
"ObjectName = " + name + ", Listener = " + listener);
}
server.addNotificationListener(name,(NotificationListener) resource,
filter, handback) ;
}
public ListenerInfo(NotificationListener listener,
NotificationFilter filter,
Object handback) {
this.listener = listener;
this.filter = filter;
this.handback = handback;
}
public synchronized void removeNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
throws ListenerNotFoundException
{
boolean before = hasListeners();
super.removeNotificationListener(listener,filter,handback);
boolean after = hasListeners();
if (before && !after) {
setNotificationEnabled(this,false);
}
}
public synchronized Integer
removeNotificationListener(ObjectName name,
NotificationListener listener,
NotificationFilter filter,
Object handback)
throws ListenerNotFoundException, IOException {
if (logger.traceOn()) {
logger.trace("removeNotificationListener",
"Remove the listener "+listener+" from "+name);
}
beforeRemove();
Integer id = null;
List<ClientListenerInfo> values =
new ArrayList<ClientListenerInfo>(infoList.values());
for (int i=values.size()-1; i>=0; i--) {
ClientListenerInfo li = values.get(i);
if (li.sameAs(name, listener, filter, handback)) {
id=li.getListenerID();
infoList.remove(id);
break;
}
}
if (id == null)
throw new ListenerNotFoundException("Listener not found");
return id;
}
public synchronized Integer[]
removeNotificationListener(ObjectName name,
NotificationListener listener)
throws ListenerNotFoundException, IOException {
beforeRemove();
if (logger.traceOn()) {
logger.trace("removeNotificationListener",
"Remove the listener "+listener+" from "+name);
}
List<Integer> ids = new ArrayList<Integer>();
List<ClientListenerInfo> values =
new ArrayList<ClientListenerInfo>(infoList.values());
for (int i=values.size()-1; i>=0; i--) {
ClientListenerInfo li = values.get(i);
if (li.sameAs(name, listener)) {
ids.add(li.getListenerID());
infoList.remove(li.getListenerID());
}
}
if (ids.isEmpty())
throw new ListenerNotFoundException("Listener not found");
return ids.toArray(new Integer[0]);
}
public void addNotificationListener(ObjectName name,
ObjectName listener,
NotificationFilter filter,
Object handback)
throws InstanceNotFoundException {
// ------------------------------
// ------------------------------
// ----------------
// Get listener object
// ----------------
DynamicMBean instance = getMBean(listener);
Object resource = getResource(instance);
if (!(resource instanceof NotificationListener)) {
throw new RuntimeOperationsException(new
IllegalArgumentException(listener.getCanonicalName()),
"The MBean " + listener.getCanonicalName() +
"does not implement the NotificationListener interface") ;
}
// ----------------
// Add a listener on an MBean
// ----------------
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER,
DefaultMBeanServerInterceptor.class.getName(),
"addNotificationListener",
"ObjectName = " + name + ", Listener = " + listener);
}
server.addNotificationListener(name,(NotificationListener) resource,
filter, handback) ;
}
public void removeNotificationListener(
ObjectName name, NotificationListener listener)
throws InstanceNotFoundException, ListenerNotFoundException {
NotificationBroadcaster userMBean =
(NotificationBroadcaster) getUserMBean(name);
NotificationListener wrappedListener =
wrappedListener(name, userMBean, listener);
userMBean.removeNotificationListener(wrappedListener);
}
/**
* Removes the specified listener from this CommunicatorServer.
* Note that if the listener has been registered with different
* handback objects or notification filters, all entries corresponding
* to the listener will be removed.
*
* @param listener The listener object to be removed.
*
* @exception ListenerNotFoundException The listener is not registered.
*/
@Override
public void removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException {
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
"removeNotificationListener","Removing listener "+ listener);
}
notifBroadcaster.removeNotificationListener(listener);
}
void dispatchNotification(TargetedNotification tn,
Integer myListenerID,
Map<Integer, ClientListenerInfo> listeners) {
final Notification notif = tn.getNotification();
final Integer listenerID = tn.getListenerID();
if (listenerID.equals(myListenerID)) return;
final ClientListenerInfo li = listeners.get(listenerID);
if (li == null) {
logger.trace("NotifFetcher.dispatch",
"Listener ID not in map");
return;
}
NotificationListener l = li.getListener();
Object h = li.getHandback();
try {
l.handleNotification(notif, h);
} catch (RuntimeException e) {
final String msg =
"Failed to forward a notification " +
"to a listener";
logger.trace("NotifFetcher-run", msg, e);
}
}