@Scheduled注释 - for循环

时间:2018-04-11 12:34:48

标签: java spring annotations spring-scheduled

我需要使用预定注释。我在String of List中有几个REST,我想每6分钟一个一个地检查。

第一次休息应该开始,6分钟后应该执行第二次休息。

如何在Scheduled方法中创建这种循环?

此外,我不能在方法错误原因(Only no-arg methods may be annotated with @Scheduled)中加入任何参数。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是使用@Scheduled类而不是@EnableScheduling class SomeClass implements SchedulingConfigurer{ // Autowire and initialize the beans you need to execute your task. @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { // Execute your task } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { String cron = createCron(); // Create cron expression for next execution CronTrigger trigger = new CronTrigger(cron); return trigger.nextExecutionTime(triggerContext); } }); } private String createCron() { // return cron expression for next trigger } } 并动态计算下一个执行时间。

以下是示例代码。

{{1}}

这使用spring上下文中提供的默认调度程序