optimus
optimus

Reputation: 11

Spring Batch - Invoking all readers of different jobs

I am new to Spring Batches.

` When I am running job1 through CLI , I am able to see logs(only for readers) for both job1 and
job2 as well.
All the readers of every job are getting invoked (job1,job2...) but not any unintended writer or Processors. I am not getting any errors but getting a bulk log file every time I run any batch.

 Getting below logs when running **job1** : 

 "Inside reader1 in SpringBatchTesting"
 "Inside reader2 in SpringBatchTesting"
 "Inside reader3......
 "Inside reader4......
 .
 .
 "Inside writer1 in SpringBatchTesting"
 "Inside processor1 in SpringBatchTesting"

How we can avoid/restrict the command to go inside specific reader of job that is invoked ? What am
I missing here. Please advice. Thanks in Advance!`
 @Configuration
    @EnableBatchProcessing
    @Import({ Dummy.class })

    public class SpringBatchTesting {

    private static final Logger log = LoggerFactory.getLogger(SpringBatchTesting.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    // Starting Job1        
    @Bean
    public Job job1() throws Exception {
    return jobBuilderFactory.get("job1").incrementer(new RunIdIncrementer())
    .start(step1()).build();

    @Bean
    public Step step1() throws Exception {
            return stepBuilderFactory.get("step1").
                <someModel1,someJsonModel1>chunk(100).reader(reader1())
                .processor(processor1()).writer(writer1()).listener(listener1()).build();         
    }

    @Bean
    public JdbcCursorItemReader<someModel1> reader1() throws Exception {
        **log.info("Inside reader1 in SpringBatchTesting");**
        JdbcCursorItemReader<someModel1> reader = new JdbcCursorItemReader<someModel1>();
        reader.setDataSource(jobDataDbSource);
        reader.setSql(someQuery);
        reader.setRowMapper(new rowMapper());
        **log.info("End of reader1 in SpringBatchTesting");**
        return reader;
    }
    // Starting Job 2

    @Bean
        public Job job2() throws Exception {
            return jobBuilderFactory.get("job2")
            .incrementer(new RunIdIncrementer())
            .start(step2())
            .build();

    @Bean
    public Step step2() throws Exception {
        return stepBuilderFactory.get("step2").
                <someModel2,someJsonModel2>chunk(100).reader(reader2())     
                .processor(processor2()).writer(writer2()).listener(listener2()).build();
    }

    `@Bean
    public JdbcCursorItemReader<someModel2> reader2() throws Exception {
        **log.info("Inside reader2 in SpringBatchTesting");**
        JdbcCursorItemReader<someModel2> reader = new JdbcCursorItemReader<someModel2>();
        reader.setDataSource(jobDataDbSource);
        reader.setSql(someQuery);

        reader.setRowMapper(new rowMapper1());
        **log.info("End of reader2 in SpringBatchTesting");**
        return reader;
    }
    }
Tried using @stepscope to restrict singleton, didn't worked out.

Upvotes: 1

Views: 276

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31620

The log messages you added are inside the bean definition methods. Those methods will be called by Spring Framework at configuration time, not by Spring Batch at runtime. So what you are seeing are the configuration logs, not the actual reading of data at runtime.

How we can avoid/restrict the command to go inside specific reader of job that is invoked

If you use Spring Boot you would need to run a single job by specifying its name on the CLI, something like:

$>java -jar myapp.jar --spring.batch.job.names=job1

If you do not use Spring Boot and use the CommandLineJobRunner from Spring Batch you can specify the job name to run as follows:

$>java CommandLineJobRunner SpringBatchTesting job1

Please refer to the reference documentation for more details about how to run jobs.

Upvotes: 1

Related Questions