设置`IteamReader`输入来自`ExecutionContext`。

huangapple go评论81阅读模式
英文:

Set IteamReader input from ExecutionContext

问题

我的Spring Batch中的第一步实际上是从服务器下载CSV文件,为它分配一个动态名称,并将其存储在ExecutionContext中。

现在的问题是,在bean创建时如何将输入文件的名称传递给FlatFileItemReader。

例如:

@Bean
public FlatFileItemReader<Customer> customerItemReader() {
    FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();

    reader.setLinesToSkip(1);
    reader.setResource(new ClassPathResource("/data/customer.csv"));

    DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>();

    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setNames(new String[] {"id", "firstName", "lastName", "birthdate"});

    customerLineMapper.setLineTokenizer(tokenizer);
    customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
    customerLineMapper.afterPropertiesSet();

    reader.setLineMapper(customerLineMapper);

    return reader;
}

请注意,reader.setResource(new ClassPathResource("/data/customer.csv")) 是在创建bean时设置的。

如何将信息从ExecutionContext传递给FlatFileItemReader作为输入源?

我是否需要查找Spring Batch表?

英文:

My first step in Spring Batch actually downloads a CSV from a server and assigns it a dynamic name and stores it in the ExecutionContext.

Now the question how do I pass the name of the input file to the FlatFileItemReader at the time of bean creation.

Take for example:

@Bean
	public FlatFileItemReader&lt;Customer&gt; customerItemReader() {
		FlatFileItemReader&lt;Customer&gt; reader = new FlatFileItemReader&lt;&gt;();

		reader.setLinesToSkip(1);
		reader.setResource(new ClassPathResource(&quot;/data/customer.csv&quot;));

		DefaultLineMapper&lt;Customer&gt; customerLineMapper = new DefaultLineMapper&lt;&gt;();

		DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
		tokenizer.setNames(new String[] {&quot;id&quot;, &quot;firstName&quot;, &quot;lastName&quot;, &quot;birthdate&quot;});

		customerLineMapper.setLineTokenizer(tokenizer);
		customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
		customerLineMapper.afterPropertiesSet();

		reader.setLineMapper(customerLineMapper);

		return reader;
	}

Note that reader.setResource(new ClassPathResource(&quot;/data/customer.csv&quot;)) is set at the time when the bean is created.

How do I pass the information from the ExecutionContext to the FlatFileItemReader as the input source ?

Do I need to lookup the Spring batch tables ?

答案1

得分: 1

Make your bean @StepScope or @JobScope and use @Value to get the value.

@Bean
@StepScope
public FlatFileItemReader<Customer> customerItemReader(@Value("#{stepExecutionContext['your.name']}") String file) {
    // Bean creation logic here.
}

Now if you would have taken the time to read the documentation you would have read it yourself.

英文:

Make your bean @StepScope or @JobScope and use @Value to get the value.

@Bean
@StepScope
public FlatFileItemReader&lt;Customer&gt; customerItemReader(@Value(&quot;#{stepExecutionContext[&#39;your.name&#39;]} String file) {
 // Bean creation logic here. 
}

Now if you would have taken the time to read the documentation you would have read it yourself.

huangapple
  • 本文由 发表于 2020年8月11日 05:48:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63348463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定