springspring-bootspring-batchspring-batch-tasklet

How to run SystemCommandTasklet asynchronously


I want to run a shell script using Spring Batch and let the batch control job id and status. But I don't want my app to wait/hang until this shell script (SystemCommandTasklet) to be completed.

    @Override
    @Bean(name = "myJobLauncher")
    public SimpleJobLauncher getJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(getJobRepository());
        //jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

   @Bean
    public Step myStep(Tasklet tasklet) {
        return this.stepBuilderFactory.get("myStep")
                .listener(tasklet)
                .tasklet(tasklet)
                .build();
    }

    @Bean
    @StepScope
    public SystemCommandTasklet systemCommandTasklet(@Value("#{jobParameters['dir']}") String dir,
                                                     @Value("#{jobParameters['command']}") String command) {
        SystemCommandTasklet tasklet = new SystemCommandTasklet();
        tasklet.setWorkingDirectory(dir);
        tasklet.setCommand(command);
        tasklet.setTimeout(100000);

        return tasklet;
    }

When I run the code above, batch/application waits until 'command' is completed. If I add jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor()); then it fails without any error logged.


Solution

  • I had an issue in different part of my code.

    adding new SimpleAsyncTaskExecutor() is actually working.