简体   繁体   English

Spring Boot项目中的作业计划库

[英]Job Schedule Library in a Spring Boot Project

I am looking for a scheduler library which can perform a simple task of invoking other REST APIs at a specific time of a day. 我正在寻找一个调度程序库,该库可以执行在一天的特定时间调用其他REST API的简单任务。 Please advise on which is a good library to use in a Spring Boot project. 请告知哪个是在Spring Boot项目中使用的好库。 I am basically looking for something which logs job config information to DB automatically and has a UI interface to check the status of jobs (preferable but not mandatory). 我基本上是在寻找一种将作业配置信息自动记录到DB并具有用于检查作业状态的UI界面的工具(首选但不是强制性的)。

I did come across this but as I have no prior experience with any of them but Quartz so I am not able to make the call: http://blog.dreamcss.com/tools/java-based-job-scheduler/ 我确实遇到过这个问题,但是由于除Quartz之外没有其他经验,所以我无法拨打电话: http : //blog.dreamcss.com/tools/java-based-job-scheduler/

Note : I did use Quartz in my previous project but I ran into multiple issue with it as it seems to have issues with not logging job related info to DB. 注意 :我在先前的项目中确实使用了Quartz,但是由于没有将与作业相关的信息记录到DB中而遇到问题,因此遇到了很多问题。 Specifically it would not log proper info into DB about last run time and whether the last job run completed successfully or not. 具体来说,它不会将有关上次运行时间以及上一次作业是否成功完成的正确信息记录到数据库中。 Also, I have seen that the Jobs in Quartz gets blocked if the previous job takes longer to complete. 另外,我已经看到,如果以前的作业需要更长的时间才能完成,则Quartz中的作业会被阻塞。

In spring boot you have an embedded simple engine for scheduling. 在春季启动中,您有一个用于计划的嵌入式简单引擎。

Use @Scheduled annotation in your @Component for example. 例如,在@Component中使用@Scheduled批注。 And don't remember to enable scheduling by using @EnableScheduling annotation. 并且不要记得通过使用@EnableScheduling注释启用调度。

You can read more about this topic in this article spring.io link 您可以在本文的spring.io链接中阅读有关此主题的更多信息。

Using a Trigger you can calculate the next execution time on the fly. 使用触发器,您可以即时计算下一次执行时间。

Something like this should do the trick (adapted from the Javadoc for @EnableScheduling): 这样的事情应该可以解决问题(从Javadoc改编为@EnableScheduling):

@Configuration
@EnableScheduling
public class MyAppConfig implements SchedulingConfigurer {

    @Autowired
    Environment env;

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }

    @Bean(destroyMethod = "shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(100);
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
        taskRegistrar.addTriggerTask(
                new Runnable() {
                    @Override public void run() {
                        myBean().getSchedule();
                    }
                },
                new Trigger() {
                    @Override public Date nextExecutionTime(TriggerContext triggerContext) {
                        Calendar nextExecutionTime =  new GregorianCalendar();
                        Date lastActualExecutionTime = triggerContext.lastActualExecutionTime();
                        nextExecutionTime.setTime(lastActualExecutionTime != null ? lastActualExecutionTime : new Date());
                        nextExecutionTime.add(Calendar.MILLISECOND, env.getProperty("myRate", Integer.class)); //you can get the value from wherever you want
                        return nextExecutionTime.getTime();
                    }
                }
        );
    }
}

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

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