Skip to content

Running async tasks in a spring boot api in a managed time and delay execution

Notifications You must be signed in to change notification settings

dilermando-lima/spring-schedule-task-runner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-schedule-task-runner

About

This project has a configuration and implementation to running scheduled taks on delay management beetwen each runnning

Creating a new task implementation

Create a new implementation of managed task, create a class thats implements ITaskRunner interface.

public class MyNewTaskRunner implements ITaskRunner {

    @Override
    public LongSupplier nextExecutionDelayInMilis() {
        // it is possible to manage delay of execution getting any data or processing from external services or database.
        
        return () ->  TimeUnit.MILLISECONDS.toMillis(1000L); // running every second. 
    }

    @Override
    public Runnable runner() {
        return () -> {  
            // doAnythin();
        }
    }
    
    @Override
    public void handleError(Throwable t) {
        // handle error on running
        LOGGER.error(t.getMessage(), t);
    }
}

Intancing and Starting tasks

It's possible to instance many tasks implementations

@Configuration
public class TaskRunnerRegister {

    @Bean
    public ITaskRunner taskRunner1(){
        return new MyNewTaskRunner1();
    }

    @Bean
    public ITaskRunner taskRunner2(){
        return new MyNewTaskRunner1();
    }

    @Bean
    public ITaskRunner taskRunner3(){
        return new MyNewTaskRunner1();
    }
}

Set default ThreadPoolTaskSchedule properties

It's possible to change default settings in application.properties

core.taskrunner.nextExecutionDelayInMilisDefault = 10000;
core.taskrunner.nextExecutionDelayInMilisOnTotalAvailableError = 60000;
core.taskrunner.threadPoolSize = 5;

About

Running async tasks in a spring boot api in a managed time and delay execution

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages