Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot CRON expression is not working properly

I have a Spring Boot application which will create a JAR file. In this application I have tow jobs to execute. One job is master job which saves data in MySQL database and second job perform some calculations and prepare some reports.

I need to execute master job at every 15 minutes and second job every 20 minute. So I used Spring Corn expression "* */15 * * * " for master job and " */20 * * * *" for second job. But the behavior of job execution is very abrupt. What I am doing wrong here?

like image 270
GD_Java Avatar asked Sep 18 '25 10:09

GD_Java


2 Answers

New Answer You are correct , i personally tested its working fine

test the cron executions using below code in java

CronTrigger t = new CronTrigger("* */15 * * * * ");
TriggerContext tc = new SimpleTriggerContext();
System.out.println("Current Time:" + new Date()+ " |Next Execution " + t.nextExecutionTime(tc));
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.MINUTE,16);
tc = new SimpleTriggerContext(c.getTime(),c.getTime(),c.getTime());
System.out.println("Current Time:" +c.getTime() + "Next Execution " + t.nextExecutionTime(tc));
c.add(Calendar.MINUTE,18);
tc = new SimpleTriggerContext(c.getTime(),c.getTime(),c.getTime());
System.out.println("Current Time:" + c.getTime() + "Next Execution " + t.nextExecutionTime(tc));

here are the imports

import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.SimpleTriggerContext;

import java.util.Calendar;
import java.util.Date;

Old Answer

You wrongly scheduled 1st Job Every day at 3PM the correct cron is */15 * * * * using

@Scheduled(cron = "*/15 * * * *")

You wrongly scheduled second job at every day at 8PM the correct cron is */20 * * * *

refer cron tab http://corntab.com/?c=*/20_*_*_*_*_

like image 105
RamiReddy P Avatar answered Sep 20 '25 02:09

RamiReddy P


Try to use the following expression and make sure you have handled exception properly. 0 */15 * * * * - Use this expression to run job every 15 min 0 */20 * * * * -Use this expression to run job every 20 min

like image 43
sathiya raj Avatar answered Sep 20 '25 03:09

sathiya raj