简体   繁体   English

Spring Boot调度不同任务的间隔时间不同

[英]Spring boot scheduling of different tasks at different intervals

I am using Spring boot to build all the back end REST API's and reactJS for front end. 我正在使用Spring boot构建所有后端REST API和前端的reactJS。 My web application allows us to run different tasks present on the UI. 我的Web应用程序允许我们运行UI上存在的不同任务。 Which basically means, REST API is triggered with the parameters for that particular task being executed. 从本质上讲,这意味着REST API是通过正在执行的特定任务的参数触发的。 All is fine when we execute it through UI, but if i want to add a schedule feature for some of these tasks then i am little confused as to how to proceed with it. 当我们通过UI执行它时,一切都很好,但是如果我想为其中一些任务添加计划功能,那么我对如何进行它就不会感到困惑。

I have seen few examples within spring boot, but they have the condition to have void return type of the object. 我在Spring Boot中没有看到几个示例,但是它们的条件是必须具有对象的void返回类型。 Whereas my objects return String or long in some instances. 在某些情况下,我的对象返回String或long。 what can be the best way to schedule jobs like these where the API needs to be run for different paramters at different times? 安排需要在不同时间针对不同参数运行API的此类作业的最佳方法是什么?

use case : 用例:

  • there are different tasks in my application 我的应用程序中有不同的任务
  • each task has different parameters that triggers an API 每个任务具有触发API的不同参数
  • each task can have different schedule times. 每个任务可以有不同的计划时间。

how can the above use cases be used and built a scheduler using a spring boot app? 如何使用上述用例并通过Spring Boot应用程序构建调度程序?

Examples I have seen are : 我看到的例子是:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

 @SpringBootApplication
 @EnableScheduling
 public class Application {
public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class);
  }
}
 /***************************************************************/
 package hello;

 import java.text.SimpleDateFormat;
 import java.util.Date;

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;

 @Component
 public class ScheduledTasks {

 private static final Logger log = 
  LoggerFactory.getLogger(ScheduledTasks.class);

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
    log.info("The time is now {}", dateFormat.format(new Date()));
}
  }

You described concerns that are good fit for batching framework. 您描述了非常适合批处理框架的问题。 Spring Batch is top framework for this from Spring Platform. Spring Batch是Spring平台为此提供的顶级框架。

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

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