com.deltax.util.listener
Class ListenerSupport

java.lang.Object
  extended bycom.deltax.util.listener.ListenerSupport
All Implemented Interfaces:
IListenerSupport

public class ListenerSupport
extends java.lang.Object
implements IListenerSupport

This class is the pluggable support for asynchronous listening.

It exposes registration methods for signal listeners and the notify(Signal) method to be called to actually send signals.

Version:
2.0
Author:
Cristiano Sadun

Field Summary
static long DEFAULT_RECEIVE_TIMEOUT
          The default timea Listener is given to exit gracefully from the Listener.receive(Signal) method when it's removed.
protected  java.lang.Object lock
          A lock used in accessing the listeners/queue registry.
 
Constructor Summary
ListenerSupport()
          Create a listener support.
ListenerSupport(int maxThreads)
          Create a listener support which uses at most the given number of threads
 
Method Summary
 void addListener(Listener listener)
          Add a listener to the listeners set.
 int countListeners()
          Count the registered listeners.
protected  com.deltax.util.listener.SignalQueue[] getQueues()
          Allows subclasses to retrieve the queue threads (experimental)
 long getReceiveTimeout()
          Return the the amount of time (in ms.) a Listeneris given to exit gracefully from the Listener.receive(Signal) method when it's removed.
 boolean isRegistered(Listener listener)
          Check whether a listener is registered or not.
 void notify(Signal signal)
          Broadcast a signal.
 void removeAllListeners()
          Unconditionally unregister all the listeners.
 void removeListener(Listener listener)
          Remove a listener from the listeners set.
 void setReceiveTimeout(long receiveTimeout)
          Set the the amount of time (in ms.) a Listeneris given to exit gracefully from the Listener.receive(Signal) method when it's removed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RECEIVE_TIMEOUT

public static final long DEFAULT_RECEIVE_TIMEOUT
The default timea Listener is given to exit gracefully from the Listener.receive(Signal) method when it's removed.

It can be set by using setReceiveTimeout(long)after creating the listener support.

See Also:
Constant Field Values

lock

protected java.lang.Object lock
A lock used in accessing the listeners/queue registry.

Constructor Detail

ListenerSupport

public ListenerSupport(int maxThreads)
Create a listener support which uses at most the given number of threads


ListenerSupport

public ListenerSupport()
Create a listener support.

Method Detail

addListener

public void addListener(Listener listener)
Add a listener to the listeners set.

If the support has a maximum thread count, the method will block if there are no free threads for the listener, until another listener releases a thread.

Specified by:
addListener in interface IListenerSupport
Parameters:
listener - the listener to add

removeAllListeners

public void removeAllListeners()
Unconditionally unregister all the listeners.

Specified by:
removeAllListeners in interface IListenerSupport

isRegistered

public boolean isRegistered(Listener listener)
Check whether a listener is registered or not.

Specified by:
isRegistered in interface IListenerSupport
Parameters:
listener - the listener to check
Returns:
true if the listener is registered

notify

public void notify(Signal signal)
Broadcast a signal. The order of invocation is unspecified.

Specified by:
notify in interface IListenerSupport

removeListener

public void removeListener(Listener listener)
                    throws BlockedListenerException
Remove a listener from the listeners set.

Specified by:
removeListener in interface IListenerSupport
Parameters:
listener - the listener to remove
Throws:
BlockedListenerException - if the listener is blocked and cannot be removed

getQueues

protected com.deltax.util.listener.SignalQueue[] getQueues()
Allows subclasses to retrieve the queue threads (experimental)

Returns:

countListeners

public int countListeners()
Count the registered listeners.

Specified by:
countListeners in interface IListenerSupport
Returns:
the number of currently registered listeners
See Also:
IListenerSupport.countListeners()

getReceiveTimeout

public long getReceiveTimeout()
Return the the amount of time (in ms.) a Listeneris given to exit gracefully from the Listener.receive(Signal) method when it's removed.

Note that this timeout does not apply to normal operations (i.e. a listener can normally block for how long it wants) but only when a listener is removed.

Specified by:
getReceiveTimeout in interface IListenerSupport
Returns:
the timeout in milliseconds

setReceiveTimeout

public void setReceiveTimeout(long receiveTimeout)
Set the the amount of time (in ms.) a Listeneris given to exit gracefully from the Listener.receive(Signal) method when it's removed.

Note that this timeout does not apply to normal operations (i.e. a listener can normally block for how long it wants) but only when a listener is removed.

Specified by:
setReceiveTimeout in interface IListenerSupport
Parameters:
receiveTimeout - the timeout in milliseconds