在 spring boot 應用程式中,我必須為給定的頻率呼叫一個方法。這個頻率將從資料庫中讀取。頻繁可能是幾秒、幾分鐘、幾小時或幾天等。我如何為 spring 啟動實施這個時間表?
uj5u.com熱心網友回復:
為此,您可以使用帶有此注釋的 cron @Scheduled
Spring 會安排帶注釋的方法在每個月的第 15 天的上午 10:15 運行
@Scheduled(cron = "0 15 10 15 * ?")
public void scheduleTaskUsingCronExpression() {
long now = System.currentTimeMillis() / 1000;
System.out.println(
"schedule tasks using cron jobs - " now);
}
請參閱此檔案https://spring.io/guides/gs/scheduling-tasks/ 和此https://spring.io/guides/gs/scheduling-tasks/
為了從資料庫中獲取 cron exp:請參閱如何在運行時更改 Spring 的 @Scheduled fixedDelay?
uj5u.com熱心網友回復:
除了@Scheduled 注釋之外,還提供更多其他替代解決方案。
您可以使用 ScheduledExecutorService 以固定速率或固定延遲執行任務。
ScheduledExecutorService executorService = Executors
.newSingleThreadScheduledExecutor();
然后您可以使用以下功能,具體取決于您的需要:
scheduleAtFixedRate
scheduleWithFixedDelay
例子:
Future<String> resultFuture =
executorService.schedule(callableTask, 1, TimeUnit.SECONDS);
Future<String> resultFuture = service
.scheduleAtFixedRate(runnableTask, 100, 450, TimeUnit.MILLISECONDS);
service.scheduleWithFixedDelay(task, 100, 150, TimeUnit.MILLISECONDS);
參考:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html https://www.baeldung.com/java-executor-service-tutorial
PS:我也強烈推薦石英框架,它可以用許多不同的策略調度任務。
uj5u.com熱心網友回復:
您可以使用Spring 的任務執行和調度 API。
您的任務以計劃模式運行
public class MyTask implements Runnable {
public void run(){
System.out.println("MyTask is running");
}
}
用于動態創建預定作業的服務。受此啟發所以回答。
您可以將其添加到您的庫中。
@Component
public class DbScheduledJobs {
private final TaskScheduler executor;
@Autowired
public DbScheduledJobs(TaskScheduler taskExecutor) {
this.executor = taskExecutor;
}
public void schedule(final Runnable task, String cronLike) {
executor.schedule(task, new CronTrigger(cronLike));
}
public void schedule(final Runnable task, LocalDateTime runOnceAt) {
executor.schedule(task, Date.from(runAt.atZone(ZoneId.systemDefault()).toInstant()));
}
public void schedulingExamples(final Runnable task) {
// Schedule a task to run once at the given date (here in 1minute)
executor.schedule(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()));
// Schedule a task that will run as soon as possible and every 1000ms
executor.scheduleAtFixedRate(task, 1000);
// Schedule a task that will first run at the given date and every 1000ms
executor.scheduleAtFixedRate(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()), 1000);
// Schedule a task that will run as soon as possible and every 1000ms after the previous completion
executor.scheduleWithFixedDelay(task, 1000);
// Schedule a task that will run as soon as possible and every 1000ms after the previous completion
executor.scheduleWithFixedDelay(task, Date.from(LocalDateTime.now().plusMinutes(1)
.atZone(ZoneId.systemDefault()).toInstant()), 1000);
// Schedule a task with the given cron expression
executor.schedule(task, new CronTrigger("*/5 * * * * MON-FRI"));
}
}
您的實際服務,從 Db 讀取。
@Service
public class DbService {
private final DbScheduledJobs dbScheduledJobs;
@Autowired
public DbService(DbScheduledJobs dbScheduledJobs) {
this.dbScheduledJobs = dbScheduledJobs;
}
public void example() {
// Read your cron like schedule from db
String cronLikeScedule = "fetched from db"; // Ex. "*/5 * * * * MON-FRI"
// Choose your Runnable instance - either statically or using Reflection.
Runnable myRunnable = new MyTask();
// Add this task to run on a schedule basis dynamically.
this.dbScheduledJobs.schedule(myRunnable, cronLikeScedule);
}
}
希望它有所幫助,此解決方案未經測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373535.html
