package org.jboss.mx.remote.dispatcher;
import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
import EDU.oswego.cs.dl.util.concurrent.Channel;
import org.jboss.mx.util.ThreadPool;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
/**
* medium priority dispatcher
*
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
* @version $Revision: 1.3 $
*/
public class MediumPriorityNotificationDispatcher
extends AbstractNotificationDispatcher
{
// NOTE: all instances share the same pool so make sure this setting is correct
private static final ThreadPool POOL = new ThreadPool(); //FIXME - make a smaller pool that queues vs. spinning new threads
static
{
POOL.setMaximumSize(5);
POOL.setActive(true);
}
long sleepTime = 0;
public MediumPriorityNotificationDispatcher()
{
super();
}
public void shutdown()
{
super.shutdown();
POOL.setActive(false);
}
/**
* subclasses override to provide their own queue implementation
*/
protected Channel createQueue()
{
return new BoundedBuffer(1500);
}
/**
* set the max capacity of the queue
*/
public synchronized void setQueueLimit(int size)
{
this.queue = new BoundedBuffer(size);
}
/**
* get the max capacity of the queue
*/
public final int getQueueLimit()
{
return ((BoundedBuffer) queue).capacity();
}
/**
* return the current size of the queue
*/
public final int getQueueSize()
{
return ((BoundedBuffer) queue).size();
}
/**
* @see org.jboss.mx.remote.dispatcher.AbstractNotificationDispatcher#dispatch(NotificationListener, NotificationFilter, Object, Notification)
*/
protected void dispatch(
final NotificationListener listener,
final NotificationFilter filter,
final Object handback,
final Notification event)
{
if (filter == null || filter.isNotificationEnabled(event))
{
Runnable r = new Runnable()
{
public void run()
{
listener.handleNotification(event, handback);
}
};
POOL.run(r);
}
}
/**
* set the maximum to wait between notification events. after sending an
* event to a NotificationListener - the amount of time in milliseconds set will
* be wait before sending the next event (if another event is pending)
*/
public void setWaitTimeBetweenNotifications(long time)
{
if (time <= 0)
{
sleepTime = 0; // force it to at least 0ms
return;
}
this.sleepTime = time;
}
/**
* get the wait time between sending events
*
* @see #setWaitTimeBetweenNotifications(long time)
*/
public final long getWaitTimeBetweenNotifications()
{
return this.sleepTime;
}
/**
* @see org.jboss.mx.remote.dispatcher.AbstractNotificationDispatcher#getDispatcherPriority()
*/
protected int getDispatcherPriority()
{
return Thread.NORM_PRIORITY - 1;
}
}