Spring Boot #Scheduled running twise - spring-boot

I have a scheduler which runs everyday once at 8.01pm. But i would seem that when it runs at 8.01pm it run twice. Following is my code
#Scheduled(cron = "0 1 20 * * *")
public void reportCurrentTime() {
logger.info("started");
}
Can anyone help me to fix my problem.Thanks.

Related

Single Cron expression to run a scheduled task between 14:30 to 15:30 for every 5 mins on FRI & SAT in Spring Boot?

Is it possible to run a scheduled task between 14:30 to 15:30 for every 5 mins on Friday & Saturday using a single Cron expression in Spring Boot?
If not, what is the best possible way to achieve this with or without Cron in Spring boot 2?
Note: I have already come up with the below approach however it makes two schedulers.
Scheduler 1: 0 30-59/5 14 ? * FRI,SAT *
Scheduler 2: 0 0-30/5 15 ? * FRI,SAT *
This should work in Spring 5.3
https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions.
Edited 1: change seconds to 0
#Scheduled("0 30/5 14-15 * * FRI-SAT")
public void run(){
...
}
Edited 2:
#Configuration
#RequiredArgsConstructor
public class SchedulerConfig {
private final TaskScheduler taskScheduler;
private final MyService myService;
#Bean
public ApplicationRunner runner() {
return args -> {
taskScheduler.schedule(myService::run, new CronTrigger("0 30-59/5 14 ? * FRI,SAT *"));
taskScheduler.schedule(myService::run, new CronTrigger("0 0-30/5 15 ? * FRI,SAT *"));
}
}

Spring Boot Scheduler not triggered at Scheduled Time

When I Schedule a Job for an email Trigger with Cron Expression in Application Properties, email is not trigger and each Time when I stop the Tomcat and reconfigure the Time it is triggering the job.
Please verify you have configuration correctly. And trigger job for particular time you have to have method level configuration also as below.
#Configuration
#EnableScheduling
public class SpringConfig {
#Scheduled(cron = "0 0 0 0 * ?")
public void scheduleTaskUsingCronExpression() {
//Email trigger at 12 clock
}
}

Prevent Spring Schedulers run at the same time

I have three Spring Schedulers like as shown below
Scheduler 1 (Runs every 15 min)
#Scheduled(cron = "0/15 * * * *")
public void scheduler1() {
// some logic
}
Scheduler 2 (Runs every 20 min)
#Scheduled(cron = "0/20 * * * *")
public void scheduler2() {
// some logic
}
Scheduler 3 (Runs every 25 min)
#Scheduled(cron = "0/25 * * * *")
public void scheduler3() {
// some logic
}
The schedulers are working fine, but there are times in sometimes they will run at the same time which will create some issues. I would like to know if there is any way in which we can prevent more than one scheduler to execute at a time in Spring

Spring 4.0 EL cron expression doesn't get resolve from #Bean method

As per requirement, I need to get cron expression from DB and set it to
Scheduled annotation's param but getting Invalid Cron expression error. Below is
my code and it works only when I add cron expression directly in #Scheduled param
#Component
#EnableScheduling
#Configuration
public class TicketReportScheduler
{
private static Log _log =
LogFactoryUtil.getLog(TicketReportScheduler.class);
#Autowired
NotificationUtility notificationUtility;
#Bean
public String getConfigRefreshValue() {
return "0 */1 * * * ?";
}
//#Scheduled(cron = "0 */1 * * * ?") - works
#Scheduled(cron = "#{#getConfigRefreshValue}") // error
public void demoServiceMethod() throws Exception {
_log.info("Cron job started " + new Date());
}
}
Error while run:
I googled a lot but unable to fix my problem. As per requirement, I have to pass cron expression from DB. For POC, I created a simple method and return cron expression but it doesn't work and I am getting below error:
Caused by: java.lang.IllegalStateException: Encountered invalid #Scheduled method 'demoServiceMethod': Cron expression must consist of 6 fields (found 1 in "#{#getConfigRefreshValue}")

Spring Boot Scheduler fixedDelay and cron

I'm running a spring boot scheduled process that takes 5-10 seconds to complete. After it completes, 60 seconds elapse before the process begins again (Note that I'm not using fixedRate):
#Scheduled(fixedDelay=60_000)
Now, I want to limit it to run every minute Mon-Fri 9am to 5pm. I can accomplish this with
#Scheduled(cron="0 * 9-16 ? * MON-FRI")
Problem here is that this acts similar to fixedRate - the process triggers EVERY 60 seconds regardless of the amount of time it took to complete the previous run...
Any way to to combine the two techniques?
it worked for me like this
I created a bean that returns a specific task executor and allowed only 1 thread.
#Configuration
#EnableAsync
public class AsyncConfig implements AsyncConfigurer {
#Bean(name = "movProcTPTE")
public TaskExecutor movProcessualThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
exec.setMaxPoolSize(1);
exec.initialize();
return exec;
}
}
In my service, I injected my task executor and wrapped my logic with it, so even though my schedule runs every minute, my logic will only run when the task executor is free.
#Service
#EnableScheduling
public class ScheduledService {
#Autowired
private ReportDataService reportDataService;
#Autowired
private AsyncService async;
#Autowired
#Qualifier("movProcTPTE")
private TaskExecutor movProcTaskExecutor;
#Scheduled(cron = "0 * * 1-7 * SAT,SUN")
public void agendamentoImportacaoMovProcessual(){
movProcTaskExecutor.execute(
() -> {
reportDataService.importDataFromSaj();
}
);
}
}
try this:
#Schedules({
#Scheduled(fixedRate = 1000),
#Scheduled(cron = "* * * * * *")
})
You can try this one:
#Scheduled(cron="1 9-16 * * MON-FRI")
Also you can try write correct on this site https://crontab.guru/
You can pass fixed delay (and any other number of optional parameters) to the annotation, like so:
#Scheduled(cron="0 * 9-16 ? * MON-FRI", fixedDelay=60_000)
From the documentation: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

Resources