Class AbstractEventSource<T extends Event>
java.lang.Object
de.aristaflow.adept2.core.eventmanager.common.AbstractEventSource<T>
- Type Parameters:
T- The type of events this event source creates.
- All Implemented Interfaces:
EventSource<T>,Runnable
- Direct Known Subclasses:
AbstractSharingActEventSource,DBEventSource,FileEventSource,MailEventSource
This abstract class provides the means for managing the handler registered
for an event source.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StringThe ID (name) of the events this event source produces.protected final List<EventHandler>All handler registered for this event source.protected StringThe ID of this event source.protected final ReadWriteLockThe lock for synchronising accesses to the list of handler.protected final LoggerThe logger for exceptions that occur when handling an event.protected final StringThe type of this event source.protected ThreadThe thread currently running,nullif no thread is running.protected final ReadWriteLockThe lock for synchronising access to the thread and the interrupt handling. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractEventSource(Registry registry, String eventType) Deprecated, for removal: This API element is subject to removal in a future version.AbstractEventSource(Registry registry, String eventType, LogService logService) Creates a new event source providing the management of registered event handler.AbstractEventSource(String sourceType, String eventType) Deprecated, for removal: This API element is subject to removal in a future version.UseAbstractEventSource(String, String, LogService)instead.AbstractEventSource(String sourceType, String eventType, LogService logService) Creates a new event source providing the management of registered event handler. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAll(List<EventHandler> handler) Adds the designated event handler in the designated order to the list of the handler that are already registered.protected voidaddedHandlerLocked(int index, EventHandler handler) Signals subclasses that the designated event handler has been added to this event source at the designated index.protected voidaddedHandlerLocked(List<EventHandler> handlers) Signals subclasses that the designated event handlers have been added to this event source at the end of the list of event handlers.voidaddHandler(EventHandler handler, int index) Adds the designated handler at the designated position (0-based) to the list of registered handler.protected abstract voidChecks for an event and handles it (usually by usingforwardEvent(Event, boolean, boolean)).protected PatterncreatePattern(String filter) Creates a pattern for the designated filter.
NOTE: This method is used by filtered event sources.protected booleanforwardEvent(T event, boolean consumed, boolean concurrent) Sends the designated event to all registered event handler.Gets the event handler that have been registered for this event source.Gets the hierarchical name of this source, that is the event type, the source type and the source ID for usage in log messages and alike.getID()Gets the ID of this source.getType()Gets the type of this source.protected voidInterrupts the current thread if there is one.protected voidMasks interrupts that occur after calling this method.protected abstract booleannextHandler(T event, boolean consumed, boolean concurrent) This method is called right before an event is offered to the next handler.removeHandler(String handlerID) Removes the handler with the designated ID from the list and returns it.protected voidremovingHandlerLocked(int index, EventHandler handler) Signals subclasses that the designated event handler is about to be removed from the designated index of the handler list of this event source.protected StringreplaceValueParams(String value, InputDataContext dataContext) WrapsParamRefTools#replaceParams(String, InputDataContext)by allowing fornulland the empty string as value.final voidrun()This method tracks the running state of this source and delegates to subclasses for checking for an event and handling it appropriately.voidsetConfiguration(Configuration conf, String id) Sets the (additional) configuration of this event source and its ID.protected booleanUnmasks the interrupt once.
-
Field Details
-
logger
The logger for exceptions that occur when handling an event. -
sourceType
The type of this event source. -
id
The ID of this event source. -
eventType
The ID (name) of the events this event source produces. -
lock
The lock for synchronising accesses to the list of handler. -
handlerList
All handler registered for this event source. -
thread
The thread currently running,nullif no thread is running. This allows to prevent concurrent runs and interrupting runs.
Synchronise access to this field viathreadLock. -
threadLock
The lock for synchronising access to the thread and the interrupt handling.
-
-
Constructor Details
-
AbstractEventSource
@Deprecated(since="16.2.0", forRemoval=true) public AbstractEventSource(String sourceType, String eventType) Deprecated, for removal: This API element is subject to removal in a future version.UseAbstractEventSource(String, String, LogService)instead.Creates a new event source providing the management of registered event handler.- Parameters:
sourceType- The type of this event source.eventType- The ID (name) of the events this event source produces.
-
AbstractEventSource
@Deprecated(since="16.2.0", forRemoval=true) public AbstractEventSource(Registry registry, String eventType) Deprecated, for removal: This API element is subject to removal in a future version.UseAbstractEventSource(Registry, String, LogService)instead.Creates a new event source providing the management of registered event handler.- Parameters:
registry- The registry providing the source type of this event source.eventType- The ID (name) of the events this event source produces.
-
AbstractEventSource
Creates a new event source providing the management of registered event handler.- Parameters:
registry- The registry providing the source type of this event source.eventType- The ID (name) of the events this event source produces.logService- The log service to retrieve the logger from.
-
AbstractEventSource
Creates a new event source providing the management of registered event handler.- Parameters:
sourceType- The type of this event source.eventType- The ID (name) of the events this event source produces.logService- The log service to retrieve the logger from.
-
-
Method Details
-
getType
Description copied from interface:EventSourceGets the type of this source. This is usually the simple instance name of the plugin, if the event source has been loaded as plugin.- Specified by:
getTypein interfaceEventSource<T extends Event>- Returns:
- The type of this source.
-
getID
Description copied from interface:EventSourceGets the ID of this source. Until separatelysetthis ID is arbitrary and may even benull.- Specified by:
getIDin interfaceEventSource<T extends Event>- Returns:
- The ID of this source.
-
getHierarchicalSourceName
Description copied from interface:EventSourceGets the hierarchical name of this source, that is the event type, the source type and the source ID for usage in log messages and alike.- Specified by:
getHierarchicalSourceNamein interfaceEventSource<T extends Event>- Returns:
- The hierarchical name of this source, that is the event type, the source type and the source ID.
-
setConfiguration
Description copied from interface:EventSourceSets the (additional) configuration of this event source and its ID. The configuration is additional with respect to the configuration provided by the plugin load mechanism, if the event source is loaded as plugin.
It is ensured that this method will be called beforeRunnable.run().- Specified by:
setConfigurationin interfaceEventSource<T extends Event>- Parameters:
conf- The configuration of this event source. This adheres (and overwrites) the configuration provided by the plugin loading, if this event source has been loaded as plugin. Whether this is allowed to benulldepends on the implementation of the event source.id- The ID of this event source. This must not benullnor the empty string.- Throws:
ConfigurationException- If there are problems with the designated configuration (configuration isnullalthough this is not allowed, a required configuration value is missing, ...), aConfigurationExceptionwill be thrown.
-
createPattern
Creates a pattern for the designated filter.
NOTE: This method is used by filtered event sources.- Parameters:
filter- The filter as string which to transform to a pattern.- Returns:
- The pattern for the designated filter or
nullin case the filter isnullor the empty string.
-
replaceValueParams
protected String replaceValueParams(String value, InputDataContext dataContext) throws ConfigurationException WrapsParamRefTools#replaceParams(String, InputDataContext)by allowing fornulland the empty string as value. In this casenullwill be returned.
NOTE: This method is used by filtered event activity sources.- Parameters:
value- The configuration value in which to replace parameter references.dataContext- The data context with which to replace parameter references.- Returns:
- A string having all parameter references in the value replaced or
null. - Throws:
ConfigurationException- If a parameter in the value cannot be replaced with the designated data context, aConfigurationExceptionwill be thrown.
-
getEventHandler
Description copied from interface:EventSourceGets the event handler that have been registered for this event source. These handler are notified about an event in the designated order.- Specified by:
getEventHandlerin interfaceEventSource<T extends Event>- Returns:
- The event handler that have been registered for this event source.
-
addHandler
Description copied from interface:EventSourceAdds the designated handler at the designated position (0-based) to the list of registered handler. It is not checked, whether the handler is already registered.- Specified by:
addHandlerin interfaceEventSource<T extends Event>- Parameters:
handler- The handler which to add.index- The index (0-based) at which to add the handler in the list.
-
addedHandlerLocked
Signals subclasses that the designated event handler has been added to this event source at the designated index. The write lock for adding is still held.
This implementation does nothing as default.
- Parameters:
index- The index (position in the list of all handlers) at which the designated handler has been added to this event source.handler- The event handler which has been added to this event source.- See Also:
-
addAll
Description copied from interface:EventSourceAdds the designated event handler in the designated order to the list of the handler that are already registered. It is not checked whether a handler is already registered (and neither whether a handler is several times in the designated list.- Specified by:
addAllin interfaceEventSource<T extends Event>- Parameters:
handler- The handler to register in the designated order. They are added at the end of the list of handler that are already registered.
-
addedHandlerLocked
Signals subclasses that the designated event handlers have been added to this event source at the end of the list of event handlers. The write lock for adding is still held.
This implementation does nothing as default.
- Parameters:
handlers- The event handlers which have been added to this event source.- See Also:
-
removeHandler
Description copied from interface:EventSourceRemoves the handler with the designated ID from the list and returns it. If there is no handler registered with the designated ID,nullwill be returned.- Specified by:
removeHandlerin interfaceEventSource<T extends Event>- Parameters:
handlerID- The ID of the handler which to remove.- Returns:
- The removed handler or
nullin case no handler with the designated ID has been registered.
-
removingHandlerLocked
Signals subclasses that the designated event handler is about to be removed from the designated index of the handler list of this event source. The write lock for removing is still held.This implementation does nothing as default.
- Parameters:
index- The index (position in the list of all handlers) from which the designated handler will be removed from this event source.handler- The event handler which is about to be removed from this event source.- See Also:
-
run
public final void run()This method tracks the running state of this source and delegates to subclasses for checking for an event and handling it appropriately. It is ensured that it is not run concurrently on an event source instance. Additionally the handler list will be read locked. -
checkForAndHandleEvent
protected abstract void checkForAndHandleEvent()Checks for an event and handles it (usually by usingforwardEvent(Event, boolean, boolean)). -
forwardEvent
protected boolean forwardEvent(T event, boolean consumed, boolean concurrent) throws EventHandlingException Sends the designated event to all registered event handler. Concurrent event handling is done if the caller of this method allows it and there are appropriate event handler. Exceptions from the event handling will be logged and handling will continue. However, the first occurredEventHandlingExceptionwill be rethrown.
Depending on whether the handler is interruptible or not, while event handling interrupts will be masked. If a masked interrupt occurs, the interrupt flag will be set and this method will return early, that is before all event handler have been notified.- Parameters:
event- The event to be handled by the registered handler.consumed- Whether the event has been consumed yet.concurrent- Whether the event can be handled concurrently.- Returns:
- Whether the event has been consumed by one of the registered handler.
- Throws:
EventHandlingException- The critical exception that occurred when handling the designated event having other occurred non-critical exceptions as suppressed ones.
-
nextHandler
This method is called right before an event is offered to the next handler. In case of concurrent handling, this method will only be called once for all concurrent handling but this will be indicated by the corresponding parameter.- Parameters:
event- The event which is about to be handled by the next registered handler.consumed- Whether the event has been consumed yet.concurrent- Whether the next event handling is concurrent and thus the event will be offered to several event handler concurrently.- Returns:
- Whether the event handling may continue. In case of serious
problems preventing further event handling, return
falsehere.
-
maskInterrupt
protected void maskInterrupt()Masks interrupts that occur after calling this method. This is reentrant, that is masking is totalled. -
unmaskInterrupt
protected boolean unmaskInterrupt()Unmasks the interrupt once. If there are no more masks and an interrupt occurred while being masked,truewill be returned.
Note that a pending interrupt will be set as interrupt in the thread but just as return value of this method.- Returns:
- Whether an interrupt occurred while being masked but only if no more masks are left.
-
maskedInterrupt
protected void maskedInterrupt()Interrupts the current thread if there is one. If the current thread masks its interrupts, the interrupt will remain pending until the thread has unmasked completely.
-
AbstractEventSource(Registry, String, LogService)instead.