Package org.jboss.mx.remote.dispatcher

Source Code of org.jboss.mx.remote.dispatcher.LowPriorityNotificationDispatcher

package org.jboss.mx.remote.dispatcher;

import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
import EDU.oswego.cs.dl.util.concurrent.Channel;

import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;

/**
* low priority dispatcher
*
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
* @version $Revision: 1.2 $
*/
public class LowPriorityNotificationDispatcher
        extends AbstractNotificationDispatcher
{
    private long sleepTime = 1;

    public LowPriorityNotificationDispatcher()
    {
        super();
    }

    /**
     * subclasses override to provide their own queue implementation
     */
    protected Channel createQueue()
    {
        return new BoundedBuffer(5000);
    }

    /**
     * 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();
    }

    /**
     * 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 = 1; // force it to at least 10 ms
            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#dispatch(NotificationListener, NotificationFilter, Object, Notification)
     */
    protected void dispatch(
            final NotificationListener listener,
            final NotificationFilter filter,
            final Object handback,
            final Notification event)
    {
        // give any pending priority threads a potential to execute before us
        Thread.currentThread().yield();
        if (filter == null || filter.isNotificationEnabled(event))
        {
            listener.handleNotification(event, handback);
            try
            {
                // this is on the dispatch thread - so sleep before
                // sending the next notification - this means that at best each event
                // will be delivered at a rate of 1 per ms
                Thread.currentThread().sleep(sleepTime);
                Thread.currentThread().yield();
            }
            catch (InterruptedException ex)
            {
                return;
            }
        }
    }

    /**
     * @see org.jboss.mx.remote.dispatcher.AbstractNotificationDispatcher#getDispatcherPriority()
     */
    protected int getDispatcherPriority()
    {
        return Thread.MIN_PRIORITY;
    }


}
TOP

Related Classes of org.jboss.mx.remote.dispatcher.LowPriorityNotificationDispatcher

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.