public abstract class BackgroundLoop extends Object
Often a system needs all kinds of cleanup jobs which run constantly in the background, without ever affecting the system performance.
Therefore a BackgroundLoop is called repeatedly to perform some work. If no work is available the loop automatically throttles itself to a certain frequency so that no system resources are wasted.
By default the doWork()
method is at most executed every ten seconds. The can be changed by overriding
maxCallFrequency()
.
Note that subclasses must wear an Register
annotation like this:
@Register(classes = BackgroundLoop.class)
to be visible to the framework.
Constructor and Description |
---|
BackgroundLoop() |
Modifier and Type | Method and Description |
---|---|
protected String |
determineExecutor()
Determines the executor (thread pool) used to execute the actual work.
|
protected abstract void |
doWork()
Executes the actual work.
|
String |
getExecutionInfo()
Contains the timestamp and duration of the last execution for monitoring purposes.
|
abstract String |
getName()
Returns the name of the loop.
|
protected void |
loop()
Calls
executeWork() in the determined executor. |
protected double |
maxCallFrequency()
Determines the maximal call frequency of
doWork() in Hertz (ticks per second). |
String |
toString() |
@Nonnull public abstract String getName()
protected abstract void doWork() throws Exception
Exception
- in case of any error. The outer loop has a catch all rule to log exceptions.protected double maxCallFrequency()
doWork()
in Hertz (ticks per second).@Nonnull protected String determineExecutor()
By default the background executor is used.
doWork
.protected void loop()
executeWork()
in the determined executor.
This is kind of the main loop, as executeWork()
will call loop()
once the computation
(doWork()
is finished. Using ExecutionBuilder.frequency(Object, double)
this is limited to the
call frequency as determined by maxCallFrequency()
.
public String getExecutionInfo()
Copyright © 2018. All rights reserved.