Spring Boot Scheduler not triggered at Scheduled Time - spring

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
}
}

Related

Programmatically trigger a #Scheduled method?

I'm working on a Springboot app that includes a task that's executed on a schedule. It typically takes about two to three minutes to run.
#Scheduled(cron = "* */30 * * * *")
public void stageOfferUpdates() throws SQLException {
...
}
We have a requirement to be able to kick off the execution of that task at any time by calling a rest endpoint. Is there a way my #GET method can programmatically kick this off and immediately return an http 200 OK?
So you just want to trigger an async task without waiting for results. Because you are using Spring, the #Async annotation is an easy way to achieve the goal.
#Async
public void asyncTask() {
stageOfferUpdates();
}
Couldn't you just run the method in another Thread:
executor.execute(() -> {
stageOfferUpdates();
}
and then procede and return 200?

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 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

Spring Boot #Scheduled running twise

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.

Can the fixed delay of a #Scheduled task be configured at runtime

I have a database poller that I have implemented as a scheduled task. I am using Spring 3.0. I want to control the fixed-delay of the scheduled task at runtime using a restful api. Is there a way to achieve this in spring?
as of now i have:
#Scheduled(fixedDelay = 30000)
public void poll() {
if (pollingEnabled) {
// code to do stuff in database
}
}
I want the fixedDelay to be dynamic.Thanks.

Resources