Pedram Farzaneh
Pedram Farzaneh

Reputation: 73

Why Spring Boot Batch job is running just one time?

I'm using spring boot. I have a batch job which I've implemented with these classes :

My main class is :

@SpringBootApplication
@ComponentScan("com.batch")
@PropertySource("classpath:application.properties")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

}

My scheduler is :

@Component
@EnableScheduling
public class JobScheduler {

    @Scheduled(fixedRate = 10000)
    public void runJob() {
        SpringApplication.run(MyBatchConfig.class);
    }
}

and my batch configuration class is :

@Configuration
@EnableBatchProcessing
public class MyBatchConfig {

    @Value("${database.driver}")
    private String databaseDriver;
    @Value("${database.url}")
    private String databaseUrl;
    @Value("${database.username}")
    private String databaseUsername;
    @Value("${database.password}")
    private String databasePassword;

    @Bean
    public Job myJob(JobBuilderFactory jobs, Step s) {
        Job job = jobs.get("myJob")
                .incrementer(new RunIdIncrementer())
                .flow(s)
                .end()
                .build();
        return job;
    }

    @Bean
    public Step myStep(StepBuilderFactory stepBuilderFactory, ItemReader<Account> reader,
                      ItemWriter<Person> writer, ItemProcessor<Account, Person> processor) {
        TaskletStep step = stepBuilderFactory.get("myStep")
                .<Account, Person>chunk(1)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
        step.setAllowStartIfComplete(true);
        return step;
    } ...

now, my problem is :

the scheduler works and it repeats every ten seconds, but the job executes only on application startup(reader, processor and writer just execute once in startup) and it seems that

SpringApplication.run(MyBatchConfig.class);

has no effect on re-running the job.

what should I do?

Thanks in advance

Upvotes: 5

Views: 7927

Answers (2)

Sabir Khan
Sabir Khan

Reputation: 10132

This is what I can think of,

1.You put this property in application.properties so your batch job doesn't start automatically by call of SpringApplication.run(...) from mainmethod.

spring.batch.job.enabled=false

This will simply initialize your Spring Batch configuration and not actually start job.

2.Put annotation @EnableScheduling on your Spring Boot Batch Job starting class i.e. on Application class in your code.

3.Remove @EnableScheduling annotation from JobScheduler class and call , jobLauncher.run(job, jobParameters) from runJob() instead of calling SpringApplication.run(MyBatchConfig.class).

JobLauncher & Job beans can be auto wired to your scheduler class since context is already initialized.

Hope it helps !!

Upvotes: 7

luboskrnac
luboskrnac

Reputation: 24561

You need to create JobLauncher bean and use that in scheduler for starting new job instances.

Upvotes: 0

Related Questions