Interface BackgroundJob<V>
- Type Parameters:
V-
- All Known Subinterfaces:
BackgroundJobWithProperties<V>
- All Known Implementing Classes:
CompositeBackgroundJob,WeightedBackgroundJob
public interface BackgroundJob<V>
A unit of work that may be processed asynchronously.
An instance of this class represents a potentially long-running task that may
be scheduled and processed asynchronously by a
BackgroundJobScheduler.
This interface models the “inside” part of the contract that is to be
fulfilled by the code implementing a particular job.
To schedule a job, use BackgroundJobScheduler.schedule(BackgroundJob)
or BackgroundJobScheduler.schedule(String, BackgroundJob).
See also: BackgroundJobInfo, BackgroundJobScheduler,
BackgroundJobStatus- Author:
- lukas
-
Method Summary
Modifier and TypeMethodDescriptionDescribe the job.default booleanWhether this job can be canceled.
While most jobs should allow cancellation by the user, there will be cases where it is just not feasible or even dangerous to abort a job once it is running (i.e.default booleanWhether this job should be visible to the end user.work(BackgroundJobMonitor monitor) Execute the Job.
-
Method Details
-
work
Execute the Job. This method is typically called by some sort of scheduler. This method should never be called twice on the same instance! While executing, implementations should use theBackgroundJobMonitorto communicate with the scheduler:- Progress should be reported to the
BackgroundJobMonitorthrough theBackgroundJobMonitor.announceTotal(int),BackgroundJobMonitor.reportProgressAbsolute(int)andBackgroundJobMonitor.reportProgressIncrement(int)methods. - All logging should use the
BackgroundJobMonitor.log(org.evolvis.tartools.backgroundjobs.BackgroundJobMonitor.Severity, Object)Method. - Long-running operations should periodically poll the state of the
BackgroundJobMonitor.isAborting()property, to check whether a cancellation was requested. If this is the case, this method should return as quickly as possible. - Implementations can obtain a unique identifier for the current
execution of this job using the
BackgroundJobMonitor.getScheduledJobId()Method.
- Parameters:
monitor- The job monitor that should be used to communicate with the scheduler.- Returns:
- the result produced by this job. Schedulers should ignore the returned value if the job has been aborted before finishing normally.
- Throws:
Exception- The scheduler should be prepared to deal with any kind of exception by marking the job as failed.
- Progress should be reported to the
-
getDescription
String getDescription()Describe the job.- Returns:
- a human-readable Description of the job.
-
isCancellationSupported
default boolean isCancellationSupported()Whether this job can be canceled.
While most jobs should allow cancellation by the user, there will be cases where it is just not feasible or even dangerous to abort a job once it is running (i.e. once thework(BackgroundJobMonitor)method is executing). Those jobs usually simply ignore the value ofBackgroundJobMonitor.isAborting()and just keep on running until they are done.
If a job just cannot be cancelled, the UI should communicate this fact to the user (e.g. by not showing a Cancel-Button in the first place). Implementations should override this method and returnfalse.
NOTE: While in stateBackgroundJobInfo.State.SCHEDULED, a job can always be canceled, regardless of what this method returns.- Returns:
- a
booleanindicating whether this job can be canceled once its execution has started. - Since:
- 1.21
-
isVisible
default boolean isVisible()Whether this job should be visible to the end user.
In applications that provide some kind of manegment UI for scheduled/running jobs it might make sense to hide certain jobs from the user. Examples would be jobs that are started automatically, have a very short runtime and are not intended to be interacted with in any way.
Implementation can override this method and returnfalseto indicate that this job should not be shown in the UI.- Returns:
- a
booleanindicating whether this job should be visible to the end user. - Since:
- 1.21
-