Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring中运行同一cron作业的多个实例_Java_Spring_Multithreading_Cron_Scheduled Tasks - Fatal编程技术网

Java 在Spring中运行同一cron作业的多个实例

Java 在Spring中运行同一cron作业的多个实例,java,spring,multithreading,cron,scheduled-tasks,Java,Spring,Multithreading,Cron,Scheduled Tasks,我希望能够在春季运行相同的计划作业。 在互联网上搜索之后,我发现了如何同时运行多个不同的工作 我有一个@Service注释类,它只有一个方法,用@Scheduled注释。我希望同时运行此作业的多个实例 我没有使用Quartz或Spring Batch(我见过很多Spring Batch的示例) 文档没有明确说明这是否可以实现。是的,可以很容易地实现,但不是通过@Scheduled注释。 怎么用?首先让我解释一下Spring是如何工作的 用@Scheduled注释的每个方法的Spring都会创建一

我希望能够在春季运行相同的计划作业。 在互联网上搜索之后,我发现了如何同时运行多个不同的工作

我有一个@Service注释类,它只有一个方法,用@Scheduled注释。我希望同时运行此作业的多个实例

我没有使用Quartz或Spring Batch(我见过很多Spring Batch的示例)


文档没有明确说明这是否可以实现。

是的,可以很容易地实现,但不是通过
@Scheduled
注释。 怎么用?首先让我解释一下Spring是如何工作的

@Scheduled
注释的每个方法的Spring都会创建一个新的
Runnable
对象,然后将其调度到
TaskScheduler
ThreadPoolTaskScheduler
)。 要查看确切的代码,请查看
scheduledNotationBeanPostProcessor.processScheduled()

因此,为了满足您的要求:拥有同一作业的多个实例,但如果不使用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();
  }
}
@配置
公共类SpringConfig{
@自动连线
专用任务调度器;
@自动连线
私人YourServiceAnnotatedClass服务;
@施工后
public void startJobs(){
int numOfJobInstances=3;
List jobs=IntStream.range(0,numOfJobInstances)
.mapToObj(i->新的重要作业(“作业”+i,服务))
.collect(Collectors.toList());
jobs.forEach(此::计划);
}
专用作废时间表(重要作业作业){
scheduler.schedule(作业,新的CronTrigger(“*/5*****”);
//使用了5秒以上的CronTrigger,但可以随意使用其他变体,例如。
//scheduleAtFixedRate()调度程序
//scheduleWithFixedDelay()调度程序
}
@Bean(destromethod=“shutdown”)
公共任务调度器任务调度器(){
ThreadPoolTaskScheduler taskScheduler=新的ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(3);//根据需要调整它
返回任务调度程序;
}
}
类ImportantJob实现可运行{
私有最终字符串名;
私人最终YourServiceAnnotatedClass服务;
公共重要作业(字符串名称,YourServiceAnnotatedClass服务){
this.name=名称;
服务=服务;
}
@凌驾
公开募捐{
service.doSth();
}
}

正如您所看到的,虽然
@Scheduled
非常有用和简单,但它不是很灵活。但只要付出一些努力,你就可以更好地控制计划的任务。

我也在寻找同样的任务。@Bianca有什么发现吗?