Skip to content

Some doubts about this Spring Batch @Scheduled() annotation and how to manually start a Spring Batch job?

I am pretty new in Spring Batch and I have the following doubts about how to schedule the job start at a specific time.

Into my project I have this SpringBatchExampleJobLauncher class:

public class SpringBatchExampleJobLauncher {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpringBatchExampleJobLauncher.class);

    private final Job job;
    private final JobLauncher jobLauncher;
    private ExecutionContext executionContext;

    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 {"Spring Batch example job was started");
        List<NotaryDistrict> notaryDistrictsList = new ArrayList<NotaryDistrict>();
        executionContext.put("notaryDistrictsList", notaryDistrictsList);
       , newExecution());"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);

As you can see it contains the runSpringBatchExampleJob() method running the job at a specific time set by the cron expression into the Scheduled annotation:

@Scheduled(cron = "0/10 * * * * *")
public void  throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

What is the exact meaniong of this CRON exception: it seems to me that the job is runned every 10 seconds but maybe I am wrong. How can I change this CRON expression in order to perform my job every night at 01:00 AM?

Another doubt is: at the moment I have this empty JUnit test class:

class UpdateInfoBatchApplicationTests {

    void contextLoads() {


Is it possible to run my job directly from a JUnit class? I know that this should not be a proper unit test (maybe more an integration test) but I also want to test my entire job without depending on the time set in the CRON exception running a JUnit test method.



Spring cron job at every 1:00 am:

@Scheduled(cron = "0 0 1 * * ?")

Check here for the difference between Linux and Spring in terms of writing cron jobs.

For testing cron jobs check here. You need to test your own code.

In addition, you can use Awaitility library.