![](/img/trans.png)
[英]ScheduledExecutorService, schedule different tasks at different intervals
[英]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的此類作業的最佳方法是什么?
用例:
如何使用上述用例並通過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.