简体   繁体   English

在Spring中运行SAME cron作业的多个实例

[英]Run multiple instances of the SAME cron job in Spring

I want to be able to run the SAME scheduled job in Spring. 我希望能够在Spring中运行SAME预定作业。 After searching on the Internet, I figured out how to run multiple different jobs at the same time. 在互联网上搜索后,我想出了如何同时运行多个不同的工作。

I have a @Service annotated class which has only one method, annotated with @Scheduled. 我有一个@Service注释类,它只有一个方法,用@Scheduled注释。 I want to have multiple instances of this job running at the same time. 我希望同时运行此作业的多个实例。

I am not using Quartz or Spring Batch( I have seen a lot of examples with Spring Batch). 我没有使用Quartz或Spring Batch(我已经看过很多Spring Batch的例子)。

The documentation doesn't clearly say if this can be achieved. 文档没有明确说明是否可以实现。

Yes, it can be easily achieved, but not with @Scheduled annotation. 是的,它很容易实现,但不能使用@Scheduled注释。 How? 怎么样? Let me first explain how Spring works. 我先来解释一下Spring的工作原理。

Spring from every method annotated with @Scheduled creates a new Runnable object and then schedules it for execution to the TaskScheduler ( ThreadPoolTaskScheduler to be precise). 每个使用@Scheduled注释的方法的Spring都会创建一个新的Runnable对象,然后将其调度执行到TaskScheduler (确切地说是ThreadPoolTaskScheduler )。 To see the exact code look at ScheduledAnnotationBeanPostProcessor.processScheduled() . 要查看确切的代码,请查看ScheduledAnnotationBeanPostProcessor.processScheduled()

So to fulfill your requirement: have multiple instances of the same job, but without using Quartz or Spring Batch we need to abandon @Scheduled annotation and do something a bit different than what the ScheduledAnnotationBeanPostProcessor does by default. 因此,为了满足您的要求:拥有相同作业的多个实例,但是如果不使用Quartz或Spring Batch,我们需要放弃@Scheduled注释,并执行与ScheduledAnnotationBeanPostProcessor默认情况相同的操作。

@Configuration
public class SpringConfig  {

  @Autowired
  private TaskScheduler scheduler;

  @Autowired
  private YourServiceAnnotatedClass service;

  @PostConstruct
  public void startJobs() {
    int numOfJobInstances = 3;
    List<ImportantJob> jobs = IntStream.range(0, numOfJobInstances)
        .mapToObj(i -> new ImportantJob("job" + i, service))
        .collect(Collectors.toList());

    jobs.forEach(this::schedule);
  }

  private void schedule(ImportantJob job) {
    scheduler.schedule(job, new CronTrigger("*/5 * * * * *"));
    // Above CronTrigger with 5 seconds was used, but feel free to use other variants, e.g.
    // scheduler.scheduleAtFixedRate()
    // scheduler.scheduleWithFixedDelay()
  }

  @Bean(destroyMethod = "shutdown")
  public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(3); // Adjust it to your needs
    return taskScheduler;
  }
}

class ImportantJob implements Runnable {
  private final String name;
  private final YourServiceAnnotatedClass service;

  public ImportantJob(String name, YourServiceAnnotatedClass service) {
    this.name = name;
    this.service = service;
  }

  @Override
  public void run() {
    service.doSth();
  }
}

As you can see, although @Scheduled is useful and simple, it is not very flexible. 正如您所看到的,尽管@Scheduled非常有用且简单,但它不是很灵活。 But with some effort you can gain much more control over scheduled tasks. 但通过一些努力,您可以更好地控制计划任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM