Package net.sf.basedb.core.signal
Class AbstractSignalReceiver
- java.lang.Object
-
- net.sf.basedb.core.signal.AbstractSignalReceiver
-
- All Implemented Interfaces:
SignalReceiver
- Direct Known Subclasses:
LocalSignalReceiver
,SocketSignalReceiver
public abstract class AbstractSignalReceiver extends Object implements SignalReceiver
Abstract base class that is useful when implementing signal receivers. This class helps with keeping track of signal handlers and with generating return values for theregisterSignalHandler(SignalHandler)
method. This class will generate values in the form ofURI
:s.signal://handlerId@receiverId/?supportedSignals
.- The receiverId part is given by the parameter in the
init(String)
method and must be set by the implementing subclass. - The handlerId part is given by calling
getLocalSignalHandlerId(SignalHandler)
. This method may be overridden by subclasses that needs a different method of local ID generation. The important thing is that the ID is unique among all registered signal handlers. - The supportedSignals part is created by joining all signal ID:s
from
SignalHandler.getSupportedSignals()
into a comma-separated string.
AbstractSignalTransporter
contains methods for parsing this scheme and make the information available to subclasses as aURI
and as higher-level objects.Subclasses should override the
getGlobalSignalId(SignalHandler)
if they want to use a different ID generation scheme.- Version:
- 2.6
- Author:
- nicklas
- Last modified
- $Date: 2015-05-12 11:27:08 +0200 (ti, 12 maj 2015) $
-
-
Field Summary
Fields Modifier and Type Field Description private Map<SignalHandler,String>
handlerIds
private Map<String,SignalHandler>
handlers
Maps local signal handler ID to -> signal handler itself.private static org.slf4j.Logger
logger
Log signals processing.private Thread
notifyThread
private String
receiverId
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractSignalReceiver()
Create a new signal receiver.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close(int wait)
Close this signal receiver.protected String
getGlobalSignalId(SignalHandler handler)
Generate a signal ID string.protected String
getLocalSignalHandlerId(SignalHandler handler)
Get the local signal handler id of the given signal handler.protected String
getReceiverId()
Get the receiver ID that was passed to theinit(String)
method.protected SignalHandler
getSignalHandler(String localId)
Get the signal handler with a given ID.void
init(String receiverId)
Initialise the signal receiver.protected void
processSignalMessage(String message)
Process a signal message.String
registerSignalHandler(SignalHandler handler)
Register a signal handler with this receiver.void
sendToAll(SignalSender sender)
Let the signal sender send one or more signals to all registered signal handlers on this reciever.void
unregisterSignalHandler(SignalHandler handler)
Unregister a signal handler.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.sf.basedb.core.signal.SignalReceiver
getSignalTransporterClass
-
-
-
-
Field Detail
-
logger
private static final org.slf4j.Logger logger
Log signals processing.
-
handlers
private Map<String,SignalHandler> handlers
Maps local signal handler ID to -> signal handler itself.
-
handlerIds
private Map<SignalHandler,String> handlerIds
-
receiverId
private String receiverId
-
notifyThread
private Thread notifyThread
-
-
Method Detail
-
init
public void init(String receiverId)
Initialise the signal receiver.- Specified by:
init
in interfaceSignalReceiver
- Parameters:
receiverId
- The ID of the signal receiver, needed if the default ID generation should be used
-
close
public void close(int wait)
Close this signal receiver. If a subclass overrides this method it should callsuper.close()
if it wants to use the default shutdown notification to signal handlers. This implementation will start a separate notification thread that first sends theSignal.SHUTDOWN
signal to all registered handlers, and thenSignal.ABORT
signal to all that are still alive after the shutdown signal. The current thread will wait at most the given time. If the number of registered handlers goes down to 0 before the time has ended the current thread will be awakened so it can continue. Note that the notification thread will usually finish in a short time, but it may take longer for all worker thread of each job to react to the signal.- Specified by:
close
in interfaceSignalReceiver
- Parameters:
wait
- Number of milliseconds to wait for registered jobs to abort, use a negative value to disable notification, and 0 to disable waiting
-
registerSignalHandler
public String registerSignalHandler(SignalHandler handler)
Register a signal handler with this receiver.- Specified by:
registerSignalHandler
in interfaceSignalReceiver
- Parameters:
handler
- The signal handler to register- Returns:
- The global ID of the signal handler
- See Also:
AbstractSignalReceiver
-
unregisterSignalHandler
public void unregisterSignalHandler(SignalHandler handler)
Unregister a signal handler.- Specified by:
unregisterSignalHandler
in interfaceSignalReceiver
- Parameters:
handler
- The signal handler to unregister
-
sendToAll
public void sendToAll(SignalSender sender)
Description copied from interface:SignalReceiver
Let the signal sender send one or more signals to all registered signal handlers on this reciever.- Specified by:
sendToAll
in interfaceSignalReceiver
- Parameters:
sender
- A signal sender implementation
-
getReceiverId
protected String getReceiverId()
Get the receiver ID that was passed to theinit(String)
method.
-
getGlobalSignalId
protected String getGlobalSignalId(SignalHandler handler)
Generate a signal ID string. This string is returned by theregisterSignalHandler(SignalHandler)
method and is used inSignalTransporter.init(String)
method to initialise a transporter object so that it can send signals to the specified handler. See the class documentation for a description of the format of the generated string. The string is of the format:signal://handlerId@receiverId/?supportedSignals
See class description for detailed information.
- Parameters:
handler
- The signal handler to generate the ID for- Returns:
- The signal handler ID
-
getLocalSignalHandlerId
protected String getLocalSignalHandlerId(SignalHandler handler)
Get the local signal handler id of the given signal handler. This implementation generates a unique ID for the handler.- Parameters:
handler
- The handler to get the id for- Returns:
- The local handler id
-
getSignalHandler
protected SignalHandler getSignalHandler(String localId)
Get the signal handler with a given ID.- Parameters:
localId
- The local signal handler ID as returned by thegetLocalSignalHandlerId(SignalHandler)
method- Returns:
- The signal handler, or null if no handler is found
-
processSignalMessage
protected void processSignalMessage(String message)
Process a signal message. If the message can't be understood or if no handler can be found this method does nothing. The signal will be delivered to the signal handler in the current thread.- Parameters:
message
- The message to process, the format of the message must be compatible with the message thatAbstractSignalTransporter.generateSignalMessage(Signal)
generates
-
-