You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Batch @Scheduled注解疑问及手动启动批处理任务方法咨询

Answers to Your Spring Batch Questions

1. Understanding and Modifying the Cron Expression

Your guess is totally correct! The cron expression 0/10 * * * * * does run the job every 10 seconds. Let's break down what each part means (Spring's @Scheduled uses a 6-part cron syntax: second, minute, hour, day of month, month, day of week):

  • 0/10: Seconds field – starts at 0 seconds, then triggers every 10 seconds after that
  • *: Minutes field – runs every minute
  • *: Hours field – runs every hour
  • *: Day of month – runs every day
  • *: Month – runs every month
  • *: Day of week – runs every day of the week

To make the job run at 1 AM every day, use this cron expression:

0 0 1 * * ?

Here's the breakdown:

  • 0: Seconds – triggers exactly at 0 seconds
  • 0: Minutes – triggers exactly at 0 minutes
  • 1: Hours – triggers at 1 o'clock
  • *: Day of month – runs every day of the month
  • *: Month – runs every month
  • ?: Day of week – we use ? here to avoid conflicts between the day-of-month and day-of-week fields (since we already specified daily runs, we don't need to restrict by day of week)

2. Running Batch Jobs via JUnit (Integration Test)

Absolutely! You can manually trigger your batch job in a JUnit test without relying on the cron schedule. Here's how to update your test class to do this:

@SpringBootTest
class UpdateInfoBatchApplicationTests {

    // Inject the necessary beans from the Spring context
    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    @Qualifier("updateNotaryDistrictsJob")
    private Job updateNotaryDistrictsJob;

    @Test
    void runBatchJobManually() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        // Create unique job parameters to avoid "job instance already complete" errors
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("currentTime", new Date())
                .toJobParameters();

        // Launch the job
        JobExecution jobExecution = jobLauncher.run(updateNotaryDistrictsJob, jobParameters);

        // Optional: Add assertions to verify the job ran successfully
        Assertions.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
    }

    @Test
    void contextLoads() {
    }
}

Key Details:

  • We inject JobLauncher and your specific updateNotaryDistrictsJob directly from the Spring application context
  • Unique JobParameters (using the current date/time) are required because Spring Batch enforces unique parameters for each execution – this prevents errors like JobInstanceAlreadyCompleteException when re-running the test
  • You can extend this by adding assertions on the JobExecution object to check step statuses, read/write counts, or other metrics to validate your batch flow

内容的提问来源于stack exchange,提问作者AndreaNobili

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 06:47:32