1. Introduction
The tasks plugin provides the means to schedule and manage tasks. Tasks will always be executed off the UI thread. In many respects task execution follows the same rules as JDK’s SwingWorker.
This plugin contains code written by Eike Kettner (swing-tasks).
1.1. Creating Tasks
Tasks must implement the Task
interface. There is a helper class
(AbstractTask
) which can be subclassed, for which the execute()
method must
be implemented as a minimum. This method should contain the long running code.
The following example depicts a Task used to find the first N prime numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import griffon.plugins.tasks.Tracker;
import org.codehaus.griffon.runtime.tasks.AbstractTask;
public class PrimeNumbersTask extends AbstractTask<List<Integer>, Integer> {
public PrimeNumbersTask(int numbersToFind) {
//initialize
}
@Override
public Integer execute(Tracker<Integer> tracker) throws Exception {
while (!enough && !isCancelled()) {
int number = nextPrimeNumber();
tracker.publish(number);
tracker.setProgress(100 * numbers.size() / numbersToFind);
}
return 0;
}
}
1.2. TaskManager
The TaskManager
is the entry point for scheduling tasks. It is used to create
TaskControl
instances, register listeners and find currently running tasks.
You can provide your own implementation by binding it inside a Module.
1.3. Executing and controlling Tasks
Use the TaskManager
to create a TaskControl
object and invoke its execute()
method.
TaskControl taskControl = taskManager.create(new PrimeNumbersTask(10));
taskControl.execute();
The execute()
method will return immediately and submit the task for
execution. Now the TaskControl
can be used to cancel the execution of the
task. The method waitFor()
can be invoked to wait for the task to finish.
1.4. Listeners
You can register TaskListeners to receive notifications on tasks. There are 3 different ways to register listeners concerning the scope of events received:
-
register on TaskManager (use
taskManager.getTaskListenerSupport()
) to receive events for all tasks -
register on TaskManager using a taskId to receive events for tasks of the given taskId
-
register on a TaskControl (use
taskControl.getTaskContext()
) to receive events solely for one execution
Griffon version: 2.12.0
2. Build Configuration
2.1. Gradle
You have two options for configuring this plugin: automatic and manual.
2.1.1. Automatic
As long as the project has the org.codehaus.griffon.griffon
plugin applied to it you
may include the following snippet in build.gradle
dependencies {
griffon 'org.codehaus.griffon.plugins:griffon-tasks-plugin:2.1.0'
}
The griffon
plugin will take care of the rest given its configuration.
2.2. Maven
First configure the griffon-tasks-plugin
BOM in your POM file, by placing the following
snippet before the <build>
element
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.codehaus.griffon.plugins</groupId>
<artifactId>griffon-tasks-plugin</artifactId>
<version>2.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Next configure dependencies as required by your particular setup
<dependency>
<groupId>org.codehaus.griffon.plugins</groupId>
<artifactId>griffon-tasks-core</artifactId>
</dependency>
3. Modules
The following sections display all bindings per module. Use this information to successfully override a binding on your own modules or to troubleshoot a module binding if the wrong type has been applied by the Griffon runtime.