gpt4 book ai didi

java - 从外部 jar 文件获取 spring boot 计划的 cron 表达式

转载 作者:行者123 更新时间:2023-12-04 08:51:54 25 4
gpt4 key购买 nike

我有一个 spring boot java 服务,我必须安排在特定时间运行。我已启用 @Enablescheduling@Scheduled注释并给出 cron 表达式。
它工作正常。调度程序正在预期的时间运行。但我担心的是我应该从我的 jar 文件之外的某个地方控制 cron 表达式。我曾尝试在属性文件中使用它,但是在打包我的属性文件时也包含在其中。
示例代码:

    @PostMapping(path = "getoktatodynamodb")
@Scheduled(cron = "0 0/5 0 * * ?")
@ApiOperation("Sync data to DynamoDB")
public FinalResponse getdatatodynamodb() {
FinalResponse finalResponse = new FinalResponse();
try {
LOGGER.info("Sync data to DynamoDB starts - " + new Date());
finalResponse = dynamodbuserService.dynamoDbSync();
} catch (MyRestTemplateException ex) {
LOGGER.error(ex.getMessage());
finalResponse.setResponseMessage(ex.getMessage());
finalResponse.setStatusCode(ex.getStatusCode().value());
} catch (Exception execption) {
LOGGER.error(execption.getMessage());
finalResponse.setResponseMessage(execption.getMessage());
finalResponse.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
} finally {
LOGGER.info("Sync data DynamoDB Ends - " + new Date());
}
return finalResponse;
}
主要目的是每当我们需要更改它应该是可配置的时间时,调度程序应该在我们的控制之下。无需更改代码并重新启动调度程序以进行细微更改。
我们应该如何实现这一点,我们也想在 linux ec2 实例中安排它?如果我们有更好的建议来实现这一点,请分享。

最佳答案

您可以实现 SchedulingConfigurer:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/SchedulingConfigurer.html
这篇 DZone 文章展示了一个非常好的例子:https://dzone.com/articles/schedulers-in-java-and-spring我在这里展示以防文章不永久。

@Configuration
@EnableScheduling
public class ScheduledConfiguration implements SchedulingConfigurer {
TaskScheduler taskScheduler;
private ScheduledFuture<?> job1;
private ScheduledFuture<?> job2;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler =new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(10);// Set the pool of threads
threadPoolTaskScheduler.setThreadNamePrefix("scheduler-thread");
threadPoolTaskScheduler.initialize();
job1(threadPoolTaskScheduler);// Assign the job1 to the scheduler
// Assign the job1 to the scheduler
this.taskScheduler=threadPoolTaskScheduler;// this will be used in later part of the article during refreshing the cron expression dynamically
taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
private void job1(TaskScheduler scheduler) {
job1 = scheduler.schedule(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " The Task1 executed at " + new Date());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cronExp = "0/5 * * * * ?";// Can be pulled from a db .
return new CronTrigger(cronExp).nextExecutionTime(triggerContext);
}
});
}
private void job2(TaskScheduler scheduler){
job2=scheduler.schedule(new Runnable(){
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" The Task2 executed at "+ new Date());
}
}, new Trigger(){
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cronExp="0/1 * * * * ?";//Can be pulled from a db . This will run every minute
return new CronTrigger(cronExp).nextExecutionTime(triggerContext);
}
});
}
}

关于java - 从外部 jar 文件获取 spring boot 计划的 cron 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64057568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com