Run a task in a different interval during weekend using Spring Scheduler and Custom Trigger
文章介绍了一种在周末使用不同时间间隔运行任务的方法。通过自定义Spring任务调度器和实现Trigger接口,可以根据业务逻辑动态调整任务执行频率,在周末改为每15分钟运行一次。 2025-2-27 17:44:0 Author: blog.gtiwari333.com(查看原文) 阅读量:9 收藏

Run a task in a different interval during weekend.

Instead of defining fixed interval to run a task every 1 minute (as shown in example below) using @Scheduled, we can customize Spring Task Scheduler to schedule job with a custom trigger to run the tasks in different schedule depending on business logic. 

@Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
void job1() {
System.out.println("Running job1 - this won't run on weekend");
}

For example, we can do the following to run the task every 15 minute instead of 1 minute during weekends. Here we are registering a task job1 with TaskScheduler with a CustomTrigger.

The CustomTrigger implements Spring's Trigger interface and overrides nextExecution() method to do business logic to find the interval on which the task should run. For the example purpose we are running tasks less often (every 15 minute) during weekend.

DayOfWeek day = LocalDate.now(ZoneId.of(ZoneId.SHORT_IDS.get("CST"))).getDayOfWeek();
Duration nextSchedulePeriod = WORKDAY_INTERVAL;

/*
* Run the task every 15 minute often during weekend
*/

if (day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY) {
nextSchedulePeriod = WEEKEND_INTERVAL;
}
return new PeriodicTrigger(nextSchedulePeriod).nextExecution(ctx);


import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.support.PeriodicTrigger;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

@Configuration
class CustomTaskScheduler implements InitializingBean {

static final Duration WORKDAY_INTERVAL = Duration.ofMinutes(1);
static final Duration WEEKEND_INTERVAL = Duration.ofMinutes(15);

@Autowired
TaskScheduler taskScheduler;

@Override
public void afterPropertiesSet() {
taskScheduler.schedule(this::job1, new CustomTrigger());
// schedule more tasks
}

void job1() {
System.out.println("Running job1 - this won't run on weekend");
}

static class CustomTrigger implements Trigger {
/**
* Determine the next execution time according to the given trigger context.
*/
@Override
public Instant nextExecution(TriggerContext ctx) {
DayOfWeek day = LocalDate.now(ZoneId.of(ZoneId.SHORT_IDS.get("CST"))).getDayOfWeek();
Duration nextSchedulePeriod = WORKDAY_INTERVAL;

/*
* Run the task every 15 minute often during weekend
*/

if (day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY) {
nextSchedulePeriod = WEEKEND_INTERVAL;
}
return new PeriodicTrigger(nextSchedulePeriod).nextExecution(ctx);
}

}

}


文章来源: http://blog.gtiwari333.com/2025/02/run-task-in-different-interval-during.html
如有侵权请联系:admin#unsafe.sh