Following example shows how to implement SchedulingConfigurer and register a task programmatically.
package com.logicbig.example; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import java.time.LocalTime; @EnableScheduling @Configuration public class SchedulingConfigurerExample implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addFixedDelayTask(() -> { System.out.println("Running task : " + LocalTime.now()); }, 2000); } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( SchedulingConfigurerExample.class); } }
Running task : 10:54:48.759471400Running task : 10:54:50.769470300Running task : 10:54:52.796292400Running task : 10:54:54.798145100Running task : 10:54:56.810557200.....................
Dependencies and Technologies Used:
Version compatibilities of spring-context with this example:
Versions in green have been tested.