本文介紹了如何將動態屬性分配給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();
}
使用此設置,將使用指定為作業參數的fileName
和columnNames
動態配置讀取器:
JobParameters jobParameters = new JobParametersBuilder()
.addString("fileName", "/path/to/input/file")
.addString("columnNames", "column1,column2,column5")
.toJobParameters();
希望這對您有幫助。
這篇關于如何將動態屬性分配給Spring批處理配置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,