Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 517
Pedram Farzaneh Avatar asked Dec 28 '16 12:12

Pedram Farzaneh


People also ask

Can a Spring Batch have multiple jobs?

Multiple jobs can be run simultaneously. There are two main types of Spring Batch Parallel Processing: Single Process, Multi-threaded, or Multi-process. These are also divided into subcategories, as follows: Multi-threaded Step (Step with many threads, single process)

How do you run two jobs sequentially in Spring Batch?

Once context is initialized, either you can do - JobLauncher jobLauncher = (JobLauncher) ctx. getBean("jobLauncher"); or do Autowired for this JobLauncher bean in main class and launch specific jobs sequentially in specific sequential order by invoking , jobLauncher. run(job, jobParameters) .

Does Spring Batch run in parallel?

When you are ready to start implementing a job with some parallel processing, Spring Batch offers a range of options, which are described in this chapter, although some features are covered elsewhere. At a high level, there are two modes of parallel processing: Single process, multi-threaded. Multi-process.


1 Answers

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 !!

like image 137
Sabir Khan Avatar answered Sep 29 '22 22:09

Sabir Khan