Small async task executor service, that allow perform tasks in parallel, cancel them and retrieve results
Create TaskService with required implementation of TaskStore and RejectResultService interfaces. By default InMemoryTaskStore and LoggingRejectResultServiceIml would be used.
ExecutorService executor = Executors.newFixedThreadPool(5);
TaskStoreService storeService = new TaskStoreService();
TaskService taskService = new TaskService(executor, storeService);
TaskInfo info = taskService.perform(() -> {
return <T> ... // result of long running task
});
T result = taskService.<T>result(info.getTaskId()); // or
T result = taskService.<T>result(info.getTaskId(), 1, TimeUnit.MILLISECONDS);
Method 'result' automatically remove task from the store after loading result
List<Callable<T>> tasks = new ArrayList<>();
... // add required tasks to collection
List<TaskInfo> info = taskService.perform(tasks);
String groupId = "groupId";
List<TaskInfo> info = taskService.perform(tasks, groupId);
Returned list info contains information about all started tasks (all task would have same groupId and auto generated UUID.randomUUID() taskId) Task id and groupId can be specified manually
TaskInfo info = taskService.perform(() -> {
return <T> ... // result of long running task
});
taskService.cancel(info.getTaskId());
List<Callable<T>> tasks = new ArrayList<>();
... // add required tasks to collection
String groupId = "groupId";
List<TaskInfo> info = taskService.perform(tasks, groupId);
taskService.cancelGroup(groupId);
All tasks would be deleted from the store, all threads would be marked as canceled. If there is some code in the body of started tasks, that could throw InterruptedException, this exception would be thrown and handled properly by TaskService
String groupId = "groupId";
List<Callable<T>> tasks = new ArrayList<>();
... // add required tasks to collection
Map<String, T> result = taskService.merge(groupId);
Started tasks results can be collected to the map
TaskOptions options = new TaskOptions(commandId).setCallback(taskId -> () -> {
... // execute some staff related to started task
});
TaskInfo info = taskService.perform(() -> {
return <T> ... // result of long running task
}, options);
Callback function would be executed after calling result method.