繁体   English   中英

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

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

我正在使用Spring boot构建所有后端REST API和前端的reactJS。 我的Web应用程序允许我们运行UI上存在的不同任务。 从本质上讲,这意味着REST API是通过正在执行的特定任务的参数触发的。 当我们通过UI执行它时,一切都很好,但是如果我想为其中一些任务添加计划功能,那么我对如何进行它就不会感到困惑。

我在Spring Boot中没有看到几个示例,但是它们的条件是必须具有对象的void返回类型。 在某些情况下,我的对象返回String或long。 安排需要在不同时间针对不同参数运行API的此类作业的最佳方法是什么?

用例:

  • 我的应用程序中有不同的任务
  • 每个任务具有触发API的不同参数
  • 每个任务可以有不同的计划时间。

如何使用上述用例并通过Spring Boot应用程序构建调度程序?

我看到的例子是:

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()));
}
  }

您描述了非常适合批处理框架的问题。 Spring Batch是Spring平台为此提供的顶级框架。

暂无
暂无

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

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