you can use @Scheduled annotation of spring. You can annotate any method on the spring component by specifying the delaystring ,i.e. the interval it should be running after. This can be configured in the properties file. To specify interval you can use "fixedRate" or "fixedDelay".
fixedRate- Executes the new run even if previous run of the job is still in progress.
fixedDelay-controls the next execution time when the last execution finishes.
This had helped me in the past.
- https://spring.io/guides/gs/scheduling-tasks
- https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html
1.You can create the JOB class containing the task to be done:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyScheduledTask {
private static final SimpleDateFormat currentTime =
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
@Scheduled(fixedRate = 2000) //interval in millisconds
public void doSomeTask() {
System.out.println("doSome task exceuted at "
+ currentTime.format(new Date()));
}
}
2.Main Spring boot class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(new Object[] { Application.class }, args);
}
}
Hope this helps you a bit.