java - 如何更改此 Spring Batch @Scheduled() 注释以指定执行时间和时间?
问题描述
我正在开发一个执行作业的 Spring Batch 应用程序。作业是使用 Spring CRON 表达式安排的。
在我的项目中,我有这个 SpringBatchExampleJobLauncher 类:
@Component
public class SpringBatchExampleJobLauncher {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBatchExampleJobLauncher.class);
private final Job job;
private final JobLauncher jobLauncher;
private ExecutionContext executionContext;
@Autowired
public SpringBatchExampleJobLauncher(@Qualifier("updateNotaryDistrictsJob") Job job,
JobLauncher jobLauncher,
ExecutionContext executionContext) {
this.job = job;
this.jobLauncher = jobLauncher;
this.executionContext = executionContext;
}
@Scheduled(cron = "0/10 * * * * *")
public void runSpringBatchExampleJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
LOGGER.info("Spring Batch example job was started");
List<NotaryDistrict> notaryDistrictsList = new ArrayList<NotaryDistrict>();
executionContext.put("notaryDistrictsList", notaryDistrictsList);
jobLauncher.run(job, newExecution());
LOGGER.info("Spring Batch example job was stopped");
}
private JobParameters newExecution() {
Map<String, JobParameter> parameters = new HashMap<>();
JobParameter parameter = new JobParameter(new Date());
parameters.put("currentTime", parameter);
return new JobParameters(parameters);
}
}
在此特定示例中,使用的 CRON 异常是:
@Scheduled(cron = "0/10 * * * * *")
因此该作业每 10 秒执行一次。这工作得很好,但它不适合真实的情况。我必须每天在特定的时间和分钟执行我的工作
根据这篇文章中显示的内容: Spring cron vs normal cron?
I know that, doing in the following way, I can perform my job at 12:00 PM on the first day of every month.
"0 0 12 1 * *"
and now I have two problem:
For test purposes I must perform my job something like every 18:30 of every day (basically I want do specify not only the hour but also the time of when the job must be executed. Moreover I want perform it every day and not on the first day of the month as specified by the previous expression). How can I modify this CRON expression in order to obtain the desired behavior?
For a next production phase I have to change this behavior in order to perform my job at a specific day (for example Moday) of a specific week (for example every 2 weeks) in a specific hour and time. For example: the job must be performed the first Monday and the third on the month at 02:30 pm. How can I modify this CRON expression in order to obtain the desired behavior?
Thank you
解决方案
From Spring documentation, the format for Cron expressions is:
┌───────────── second (0-59)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of the month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
│ │ │ │ │ ┌───────────── day of the week (0 - 7)
│ │ │ │ │ │ (or MON-SUN -- 0 or 7 is Sunday)
│ │ │ │ │ │
* * * * * *
- You can achieve the first case with
0 30 18 * * *
. Asterisk*
meansevery
in cron expressions. - For this you can use
0 30 14 ? * MON#1,MON#3
.MON#X
means theX
th Monday of the month. In this case we use?
for day of the month field, because it doesn't matter which day of the month the first/third Monday falls on.?
indicates no specific value.
推荐阅读
- c# - 尝试启用 Texture2D 时 OpenTK“无效枚举”
- ssh - Gitlab CI - 使用 ssh 找不到 Composer
- sql - 来自两个表和一个链接表的 SQL 查询,为表 a 的每一行返回所有表 c,其中表 b 中没有链接
- javascript - 如何在基于类的组件中进行 Expo/react-native Google 身份验证?
- javascript - Fastify 重新加载浏览器
- javascript - 如何使用 Excel JavaScript API 获取选定或活动单元格的数据透视表名称和数据透视字段名称?
- amazon-web-services - 从联合角色获取 CLI 的 AWS 凭证
- redux-toolkit - 在 createAsyncThunk 方法中覆盖对象属性
- verilog - Verilog 时钟脉冲
- java - 任何 Java IDE Intellisense 都缺少某些元素