Search code examples
springspring-batchjobs

Use jobParameters in mapper


I'm creating a job with Spring Batch, the job is launched manually from a Camel Processor.

I need to pass some parameters to my job, and one of them need to be used in the mapper of my job but I can't find a way to achieve this.

Here is my job:

    @Bean
    public LineMapper<MyEntity> lineMapper() {
        DefaultLineMapper<MyEntity> lineMapper = new DefaultLineMapper<MyEntity>();
        DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer() {
            {
                setNames(new String[] { "val1", "val2" });
            }
        };
        lineTokenizer.setDelimiter(";");

        lineMapper.setLineTokenizer(lineTokenizer);

        lineMapper.setFieldSetMapper(new MyEntityFieldsetMapper());

        return lineMapper;
    }

    public class MyEntityFieldsetMapper implements FieldSetMapper<MyEntity> {

        @Override
        public MyEntity mapFieldSet(FieldSet fieldSet) throws BindException {
            MyEntity MyEntity = new MyEntity();
            
            MyEntity.setValOne(fieldSet.readString("val1"));
            MyEntity.setValTwo(fieldSet.readString("val2"));
            
            // Here I need to set the variable in job parameters
            MyEntity.setValThree();

            return MyEntity;
        }
    }

    @Bean
    @StepScope
    public FlatFileItemReader<MyEntity> MyEntityFlatFileItemReader(
            @Value("#{jobParameters[inputCsvPath]}")
                    String inputCsvPath, @Value("#{jobParameters[myCustomVal]}")
                    String myCustomVal) {
        //Create reader instance
        FlatFileItemReader<MyEntity> reader = new FlatFileItemReader<MyEntity>();

        //Set input file location
        reader.setResource(new FileSystemResource(inputCsvPath));

        //Set number of lines to skips. Use it if file has header rows.
        reader.setLinesToSkip(0);

        //Configure how each line will be parsed and mapped to different values
        reader.setLineMapper(lineMapper());
        return reader;
    }

    @Bean
    public Job readCSVFilesJob(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory,
                               ItemReader<MyEntity> MyEntityItemReader, ItemWriter<MyEntity> MyEntityWriter) {

        Step step = stepBuilderFactory.get("step").<MyEntity, MyEntity>chunk(10000).reader(MyEntityItemReader)
                .writer(MyEntityWriter).build();

        return jobBuilderFactory.get("readCSVFilesJob").incrementer(new RunIdIncrementer()).start(step).build();
    }

I can get the jobParameters in the ItemReader but can't pass it to my lineMapper and FieldSetMapper.

Is there a way to do this ?


Solution

  • Make your LineMapper bean step scoped and inject job parameters in it as you did for the reader, for example:

    @Bean
    @StepScope
    public LineMapper<MyEntity> lineMapper(@Value("#{jobParameters[inputCsvPath]}") String inputCsvPath) {
       // use job parameter here
    }
    

    You might also need to declare MyEntityFieldsetMapper as a step scoped bean if you want to inject job parameters in it as well.