Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Scheduled for every one hour doesn't work. tried various options

Tags:

spring

i need to use @Schedule spring annotation to with cron parameter to run the job for every one hour. I have tried various option but it doesn't seems to work.

Could someone help me with the valid expression to run for every 1 hour?

ex: 1:00 2:00 3:00 4:00 5:00 6:00 7:00 etc.,

Referred: http://www.baeldung.com/spring-scheduled-tasks and http://www.baeldung.com/cron-expressions

Tried the following

0 0/60 * * * * 
0 * 0/1 * * *
* * 0/1 * * *
* * 0/60 * * *

Thanks.

like image 959
Kathir Avatar asked Jan 04 '18 04:01

Kathir


3 Answers

FixedRate annotation got worked to run for every hour

@Scheduled(fixedRate=60*60*1000)
public void scheduleFixedRateTask() {
System.out.println(
  "Fixed rate task - " + System.currentTimeMillis() / 1000);
}

FixedRate annotation to run for every hour with 10 minutes delay. Incase if you want to make an initial delay to start the job, you can specify "initialDelay". If you specify this value, the the very first time job will be started after the given delay. In the below example, the method is scheduled to run at every hour with initial start delay as 10 minutes.

@Scheduled(fixedRate=60*60*1000, initialDelay=10*60*1000)
like image 89
Kathir Avatar answered Oct 22 '22 22:10

Kathir


@Component
public class SomeScheduler {

   @Scheduled(cron = "0 0 0/1 * * ?")
   public void print() {
      System.out.println("====>> print method()...");
   }
}

@EnableScheduling
@Configuration
public class AppStarter {

}
like image 22
sanit Avatar answered Oct 22 '22 23:10

sanit


Spring @Scheduled annotation has a optional element called timeUnit to setup the time unit use in the fixedDelay, fixedDelayString, fixedRate, fixedRateString, initialDelay, and initialDelayString

So to run a job every one hour @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)

For other time units (TimeUnit Enum constants) see the docs

like image 1
Ravindu Rathugama Avatar answered Oct 22 '22 23:10

Ravindu Rathugama