AM13
AM13

Reputation: 683

Job of Spring batch start automatically when application start. How to avoid id?

I'm doing a simple project in Spring to understand how Spring batch work. My project work but the job is executed when the application start and I do not want that. There is a way to avoid it?

I am already able to start the task when I want (for example go to a certain URL mapped by a @RestController). The only problem is that the batch job start when the application start too.

However my configuration code for Spring batch is:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public FlatFileItemReader<Person> reader() {
      return new FlatFileItemReaderBuilder<Person>()
        .name("personItemReader")
        .resource(new ClassPathResource("sample-data.csv"))
        .delimited()
        .names(new String[]{"firstName", "lastName"})
        .fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
          setTargetType(Person.class);
        }})
        .build();
    }

    @Bean
    public PersonItemProcessor processor() {
      return new PersonItemProcessor();
    }

    @Bean
    public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
      return new JdbcBatchItemWriterBuilder<Person>()
        .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
        .sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
        .dataSource(dataSource)
        .build();
    }

    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
      return jobBuilderFactory.get("importUserJob")
        .incrementer(new RunIdIncrementer())
        .listener(listener)
        .start(step1)
//      .flow(step1)
//      .end()
        .build();
    }

    @Bean
    public Step step1(JdbcBatchItemWriter<Person> writer) {
      return stepBuilderFactory.get("step1")
        .<Person, Person> chunk(10)
        .reader(reader())
        .processor(processor())
        .writer(writer)
        .build();
    }
}

I have a github repository public for this project: https://github.com/Mazzotta13/SpringBatch.git

Thanks in advance.

Upvotes: 1

Views: 741

Answers (1)

A.khalifa
A.khalifa

Reputation: 2496

In your application.properties add this

spring.batch.job.enabled=false

Hope useful

Upvotes: 3

Related Questions