Annotations
All the common configuration tasks in Velai can be achieved via annotations on the task implementation class.
Example
The following example illustrates use of the most common annotations.
@Daemon public class MyTask implements Callable<Void> { @Context private ExecutionContext execution; private double fractionDone = 0.0; @Init public void preValidate() throws RejectedExecutionException { // any initialisation and/or error checking that needs to be // done when a call is made to TaskExecutor.start() or // TaskExecutor.execute(). } @BeforeCall public void prepare() { // anything to do prior to call() being executed. // Typically defined within super-classes. } @AfterCall public void cleanup() { // anything to do after call() being executed. // Typically defined within super-classes. } public Void call() throws Exception { while (!execution.isCancellationRequested(true)) { // do stuff } return null; } @OnCancel public void cancelled(boolean mayInterruptIfRunning) { // anything special that needs to be done immediately, // such as calling Object.notify() on something } @OnPauseStateChanged public void pauseStateChanged() { if (execution.isPauseRequested()) { // anything that needs to be done as it is paused } else { // anything that needs to be done as it is resumed } } @Progress public ProgressState getProgress() { return new ProgressState() { public Double getFractionDone() { return fractionDone; } }; } }
Details
The following table lists all the annotations and their purpose. For full details of each annotation, see the javadocs.
Annotation | Description |
---|---|
@AfterCall |
Called just before Callable.call() is executed. |
@BeforeCall |
Called just after Callable.call() is executed. |
@Context |
Marks the injection point for an ExecutionContext instance within a task. |
@Daemon |
Indicates that a task should be executed within a deamon thread if executed asynchronously. |
@Init |
Called just before Callable.call() is executed, in the same thread as the call to Executor.execute() or Executor.start(). Typically used to validate that the task can be started. |
@OnCancel |
Marks a method for immediately handling cancellation. |
@OnPauseStateChanged |
Marks a method for immediately handling requests to pause and unpause. |
@Progress |
Marks a method for indicating instantaneous progress of the task. |
@ThreadName |
Marks a method or field to supply the name of the thread when the thread is first created. |