Class AbstractFileWatchingService
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ClusterStateListener
,LifecycleComponent
,Releasable
- Direct Known Subclasses:
FileSettingsService
A file watching service watches for changes in a particular file on disk. There are three assumptions about the file structure:
- The file itself may or may not exist.
- The file's parent directory may or may not exist.
- The directory above the file's parent directory must always exist.
For example, if the watched file is under /usr/elastic/elasticsearch/conf/special/settings.yml, then /usr/elastic/elasticsearch/conf/ must exist, but special/ and special/settings.yml may be created, updated, or deleted during runtime.
What this class does not do is define what should happen after the file changes.
An implementation class should override processFileChanges()
to define
the correct behavior.
-
Field Summary
Fields inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
lifecycle
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractFileWatchingService
(ClusterService clusterService, Path watchedFile) -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addFileChangedListener
(FileChangedListener listener) final void
Called when cluster state changes.protected final void
doClose()
Close this component.protected final void
doStart()
Start this component.protected final void
doStop()
Stop this component.protected abstract void
Any implementation of this class must implement this method in order to define what happens once the watched file changes.protected boolean
shouldRefreshFileState
(ClusterState clusterState) There may be an indication in cluster state that the file we are watching should be re-processed: for example, after cluster state has been restored from a snapshot.final Path
final Path
protected final void
final boolean
watching()
Methods inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
addLifecycleListener, close, lifecycleState, start, stop
-
Constructor Details
-
AbstractFileWatchingService
-
-
Method Details
-
processFileChanges
protected abstract void processFileChanges() throws InterruptedException, ExecutionException, IOExceptionAny implementation of this class must implement this method in order to define what happens once the watched file changes.- Throws:
IOException
- if there is an error reading the file itselfExecutionException
- if there is an issue while applying the changes from the fileInterruptedException
- if the file processing is interrupted by another thread.
-
shouldRefreshFileState
There may be an indication in cluster state that the file we are watching should be re-processed: for example, after cluster state has been restored from a snapshot. By default, we do nothing, but this method should be overridden if different behavior is desired.- Parameters:
clusterState
- State of the cluster- Returns:
- false, by default
-
addFileChangedListener
-
watchedFileDir
-
watchedFile
-
clusterChanged
Description copied from interface:ClusterStateListener
Called when cluster state changes.Cluster states are applied one-by-one which means they can be a performance bottleneck. Implementations of this method should therefore be fast, so please consider forking work into the background rather than doing everything inline.
- Specified by:
clusterChanged
in interfaceClusterStateListener
-
doStart
protected final void doStart()Description copied from class:AbstractLifecycleComponent
Start this component. Typically that means doing things like launching background processes and registering listeners on other components. Other components have been initialized by this point, but may not yet be started.If this method throws an exception then the startup process will fail, but this component will not be stopped before it is closed.
This method is called while synchronized on
AbstractLifecycleComponent.lifecycle
. It is only called once in the lifetime of a component, although it may not be called at all if the startup process encountered some kind of fatal error, such as the failure of some other component to initialize or start.- Specified by:
doStart
in classAbstractLifecycleComponent
-
doStop
protected final void doStop()Description copied from class:AbstractLifecycleComponent
Stop this component. Typically that means doing the reverse of whateverAbstractLifecycleComponent.doStart()
does.This method is called while synchronized on
AbstractLifecycleComponent.lifecycle
. It is only called once in the lifetime of a component, after callingAbstractLifecycleComponent.doStart()
, although it will not be called at all if this component did not successfully start.- Specified by:
doStop
in classAbstractLifecycleComponent
-
doClose
protected final void doClose()Description copied from class:AbstractLifecycleComponent
Close this component. Typically that means doing the reverse of whatever happened during initialization, such as releasing resources acquired there.This method is called while synchronized on
AbstractLifecycleComponent.lifecycle
. It is called once in the lifetime of a component. If the component was started then it will be stopped before it is closed, and once it is closed it will not be started or stopped.- Specified by:
doClose
in classAbstractLifecycleComponent
-
watching
public final boolean watching() -
watcherThread
protected final void watcherThread()
-