NotificationBroadcasterSupportpublic class NotificationBroadcasterSupport extends Object implements NotificationEmitterProvides an implementation of {@link
javax.management.NotificationEmitter NotificationEmitter}
interface. This can be used as the super class of an MBean that
sends notifications.
By default, the notification dispatch model is synchronous.
That is, when a thread calls sendNotification, the
NotificationListener.handleNotification method of each listener
is called within that thread. You can override this default
by overriding handleNotification in a subclass, or by passing an
Executor to the constructor.
If the method call of a filter or listener throws an {@link Exception},
then that exception does not prevent other listeners from being invoked. However,
if the method call of a filter or of {@code Executor.execute} or of
{@code handleNotification} (when no {@code Excecutor} is specified) throws an
{@link Error}, then that {@code Error} is propagated to the caller of
{@link #sendNotification sendNotification}.
Remote listeners added using the JMX Remote API (see JMXConnector) are not
usually called synchronously. That is, when sendNotification returns, it is
not guaranteed that any remote listeners have yet received the notification. |
Fields Summary |
---|
private List | listenerList | private final Executor | executor | private final MBeanNotificationInfo[] | notifInfo | private static final Executor | defaultExecutor | private static final MBeanNotificationInfo[] | NO_NOTIFICATION_INFO | private static final ClassLogger | logger |
Constructors Summary |
---|
public NotificationBroadcasterSupport()Constructs a NotificationBroadcasterSupport where each listener is invoked by the
thread sending the notification. This constructor is equivalent to
{@link NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
MBeanNotificationInfo[] info) NotificationBroadcasterSupport(null, null)}.
this(null, (MBeanNotificationInfo[]) null);
| public NotificationBroadcasterSupport(Executor executor)Constructs a NotificationBroadcasterSupport where each listener is invoked using
the given {@link java.util.concurrent.Executor}. When {@link #sendNotification
sendNotification} is called, a listener is selected if it was added with a null
{@link NotificationFilter}, or if {@link NotificationFilter#isNotificationEnabled
isNotificationEnabled} returns true for the notification being sent. The call to
NotificationFilter.isNotificationEnabled takes place in the thread
that called sendNotification . Then, for each selected listener,
{@link Executor#execute executor.execute} is called with a command
that calls the handleNotification method.
This constructor is equivalent to
{@link NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
MBeanNotificationInfo[] info) NotificationBroadcasterSupport(executor, null)}.
this(executor, (MBeanNotificationInfo[]) null);
| public NotificationBroadcasterSupport(MBeanNotificationInfo info)Constructs a NotificationBroadcasterSupport with information
about the notifications that may be sent. Each listener is
invoked by the thread sending the notification. This
constructor is equivalent to {@link
NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
MBeanNotificationInfo[] info)
NotificationBroadcasterSupport(null, info)}.
If the info array is not empty, then it is
cloned by the constructor as if by {@code info.clone()}, and
each call to {@link #getNotificationInfo()} returns a new
clone.
this(null, info);
| public NotificationBroadcasterSupport(Executor executor, MBeanNotificationInfo info)Constructs a NotificationBroadcasterSupport with information about the notifications that may be sent,
and where each listener is invoked using the given {@link java.util.concurrent.Executor}.
When {@link #sendNotification sendNotification} is called, a
listener is selected if it was added with a null {@link
NotificationFilter}, or if {@link
NotificationFilter#isNotificationEnabled isNotificationEnabled}
returns true for the notification being sent. The call to
NotificationFilter.isNotificationEnabled takes
place in the thread that called
sendNotification . Then, for each selected
listener, {@link Executor#execute executor.execute} is called
with a command that calls the handleNotification
method.
If the info array is not empty, then it is
cloned by the constructor as if by {@code info.clone()}, and
each call to {@link #getNotificationInfo()} returns a new
clone.
this.executor = (executor != null) ? executor : defaultExecutor;
notifInfo = info == null ? NO_NOTIFICATION_INFO : info.clone();
|
Methods Summary |
---|
public void | addNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)Adds a listener.
if (listener == null) {
throw new IllegalArgumentException ("Listener can't be null") ;
}
listenerList.add(new ListenerInfo(listener, filter, handback));
| public javax.management.MBeanNotificationInfo[] | getNotificationInfo()
if (notifInfo.length == 0)
return notifInfo;
else
return notifInfo.clone();
| protected void | handleNotification(javax.management.NotificationListener listener, javax.management.Notification notif, java.lang.Object handback)This method is called by {@link #sendNotification
sendNotification} for each listener in order to send the
notification to that listener. It can be overridden in
subclasses to change the behavior of notification delivery,
for instance to deliver the notification in a separate
thread.
The default implementation of this method is equivalent to
listener.handleNotification(notif, handback);
listener.handleNotification(notif, handback);
| public void | removeNotificationListener(javax.management.NotificationListener listener)
ListenerInfo wildcard = new WildcardListenerInfo(listener);
boolean removed =
listenerList.removeAll(Collections.singleton(wildcard));
if (!removed)
throw new ListenerNotFoundException("Listener not registered");
| public void | removeNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)
ListenerInfo li = new ListenerInfo(listener, filter, handback);
boolean removed = listenerList.remove(li);
if (!removed) {
throw new ListenerNotFoundException("Listener not registered " +
"(with this filter and " +
"handback)");
// or perhaps not registered at all
}
| public void | sendNotification(javax.management.Notification notification)Sends a notification.
If an {@code Executor} was specified in the constructor, it will be given one
task per selected listener to deliver the notification to that listener.
if (notification == null) {
return;
}
boolean enabled;
for (ListenerInfo li : listenerList) {
try {
enabled = li.filter == null ||
li.filter.isNotificationEnabled(notification);
} catch (Exception e) {
if (logger.debugOn()) {
logger.debug("sendNotification", e);
}
continue;
}
if (enabled) {
executor.execute(new SendNotifJob(notification, li));
}
}
|
|