如何将动态属性分配给Spring批处理配置?

How could I assign dynamic properties to a Spring Batch configuration?(如何将动态属性分配给Spring批处理配置?)

本文介绍了如何将动态属性分配给Spring批处理配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在Spring Batch Configuration bean中动态更改资源、列名、列位置和其他内容的用例。作业将从JobLauncher启动。

我构造了以下内容:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    public JobBuilderFactory jobBuilderFactory;
    public StepBuilderFactory stepBuilderFactory;

    //these are the properties i need to dynamically change
    private String[] names;
    private Resource inputResource;
    private String[] positions;
    private String tableName;
    private String columnNames;
    private String values;

    @Autowired
    public void setJobBuilderFactory(JobBuilderFactory jobBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
    }

    @Autowired
    public void setStepBuilderFactory(StepBuilderFactory stepBuilderFactory) {
        this.stepBuilderFactory = stepBuilderFactory;
    }

    @Bean
    BatchConfigurer configurer(@Qualifier("prestagingJpaDataSource") DataSource dataSource){
        return new DefaultBatchConfigurer(dataSource);
    }


    @Bean
    public FlatFileItemReader<String[]> reader() {

        return new FlatFileItemReaderBuilder<String[]>()
                .name("hacReader")
                //put file here
                .resource(inputResource)
                .lineMapper(new DefaultLineMapper<String[]>() {{
                    setLineTokenizer(new DelimitedLineTokenizer() {{
                        //put column names here
                        setNames(names);
                    }});
                    //put column positions here
                    setFieldSetMapper(fieldSet -> positions);
                }})
                .build();
    }

    //this is obviously wrong, as i would need to use an ItemPreparedStatementSetter
    @Bean
    public JdbcBatchItemWriter<String[]> writer(DataSource dataSource) {
        return new JdbcBatchItemWriterBuilder<String[]>()
                .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
                .sql("INSERT INTO " + tableName + " (" + columnsNames + ") VALUES (" + values)
                .dataSource(dataSource)
                .build();
    }

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

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

Spring Batch可以实现这样的功能吗?这些属性将始终变化,因此我绝对不能有任何硬编码值。

如果是,那么我需要做些什么才能使它们工作?

推荐答案

您可以通过将这些属性作为作业参数(在本例中最好作为非标识作业参数传递)和late-bind它们在运行时在读取器中传递来实现此目的。下面是一个示例:

@Bean
@StepScope
public ItemReader<String[]> itemReader(
        @Value("#{jobParameters['fileName']}") String fileName,
        @Value("#{jobParameters['columnNames']}") String columnNames
        ) {
    return new FlatFileItemReaderBuilder<String[]>()
            .name("hacReader")
            //put file here
            .resource(new FileSystemResource(fileName))
            .lineMapper(new DefaultLineMapper<String[]>() {{
                setLineTokenizer(new DelimitedLineTokenizer() {{
                    //put column names here
                    setNames(columnNames.split(","));
                }});
            }})
            .build();
}

使用此设置,将使用指定为作业参数的fileNamecolumnNames动态配置读取器:

JobParameters jobParameters = new JobParametersBuilder()
            .addString("fileName", "/path/to/input/file")
            .addString("columnNames", "column1,column2,column5")
            .toJobParameters();

希望这对您有帮助。

这篇关于如何将动态属性分配给Spring批处理配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将动态属性分配给Spring批处理配置?

基础教程推荐