Skip to content

Small async task executor service, that allow perform tasks in parallel, cancel them and retrieve results

License

Notifications You must be signed in to change notification settings

chirkovd/task-dispatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

task-dispatcher

BCH compliance Codacy Badge

Small async task executor service, that allow perform tasks in parallel, cancel them and retrieve results

Getting Started

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);

Run task

    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

Run tasks

    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

Cancel task

    TaskInfo info = taskService.perform(() -> {
        return <T> ... // result of long running task
    });
    taskService.cancel(info.getTaskId());

Cancel tasks

    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

Merge results

    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

Start task with callback function


    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.

About

Small async task executor service, that allow perform tasks in parallel, cancel them and retrieve results

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages