简体   繁体   English

如何将Spring-boot调度器设计为Service,以便其他微服务可以使用

[英]How to Design a Spring-boot scheduler as Service, so that other micro-services can use it

I want to design a scheduler as service using spring-boot.我想使用 spring-boot 将调度程序设计为服务。 My scheduler should be generic so that other microservices can use it as they want.我的调度器应该是通用的,以便其他微服务可以根据需要使用它。

I tried normal spring boot examples.我尝试了正常的 spring 引导示例。

/** * This scheduler will run on every 20 Seconds. /** * 此调度程序将每 20 秒运行一次。 */ @Scheduled(fixedRate = 20 * 1000, initialDelay = 5000) public void scheduleTaskWithInitialDelay() { logger.info("Fixed Rate Task With Initail Delay 20 Seconds:: Execution Time - "+dateTimeFormatter.format(LocalDateTime.now())); */ @Scheduled(fixedRate = 20 * 1000, initialDelay = 5000) public void scheduleTaskWithInitialDelay() { logger.info("Fixed Rate Task With Initail Delay 20 seconds:: Execution Time - "+dateTimeFormatter.format(LocalDateTime.now() )); } }

/**
 * This scheduler will run on every 10 Seconds.
 */
@Scheduled(fixedRate = 10* 1000, initialDelay = 5000)
public void scheduleTaskWithInitialDelay1() {
    logger.info("Fixed Rate Task With Initail Delay 10 Seconds:: Execution Time - "+dateTimeFormatter.format(LocalDateTime.now()));
}

You need to store other microservice's requests to schedule something in your persistent.您需要存储其他微服务的请求以在您的持久化中安排某些内容。 So, you have an inventory that which microservice requested the scheduling service and with delay or cron or something else.因此,您有一个清单,其中微服务请求调度服务以及延迟或 cron 或其他内容。

Now, you can read all the requested configuration from the database and start scheduler for them.现在,您可以从数据库中读取所有请求的配置并为它们启动调度程序。

This is a common use case in enterprise applications when people choose to write custom code.当人们选择编写自定义代码时,这是企业应用程序中的常见用例。

Your database table should contain all the detail + what to do if scheduler reached to given time (Push data/event to some URL or something else).您的数据库表应该包含所有详细信息 + 如果调度程序达到给定时间该怎么办(将数据/事件推送到某些 URL 或其他)。

Some technical detail一些技术细节

You schedule service should allow to您安排的服务应该允许

  • Add Schedule添加时间表
  • Start/Stop/Update existing schedule开始/停止/更新现有计划
  • Callback or some other operation when scheduler meet the time调度程序满足时间时的回调或其他操作

Hope, this will help.希望,这会有所帮助。

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

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