Posted In: Spring, Spring Boot

How to run a task periodically in Spring Boot

This class will schedule to run JOB after each hour

package com.amp.services.crm.configure;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableAsync
@EnableScheduling
public class ScheduleConfigure {
	
	@Scheduled(fixedDelay=3600000)
	public void sendEmails() {
	    System.out.println("ScheduleConfigure---------------------------");
	}
}

Another variant with initial delay

	@Scheduled(initialDelay=1000, fixedRate=5000)
	public void sendSms() {
	    System.out.println("ScheduleConfigure---------------------------");
	}

Variant with CRON like syntax. This JOB/TASK will only run week days

	@Scheduled(cron="*/5 * * * * MON-FRI")
	public void sendSms() {
	    System.out.println("ScheduleConfigure---------------------------");
	}	

Tags:

by , on March 29th, 2017

  • Categories