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

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

Set IteamReader input from ExecutionContext

问题

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

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

例如:

  1. @Bean
  2. public FlatFileItemReader<Customer> customerItemReader() {
  3. FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
  4. reader.setLinesToSkip(1);
  5. reader.setResource(new ClassPathResource("/data/customer.csv"));
  6. DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>();
  7. DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
  8. tokenizer.setNames(new String[] {"id", "firstName", "lastName", "birthdate"});
  9. customerLineMapper.setLineTokenizer(tokenizer);
  10. customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
  11. customerLineMapper.afterPropertiesSet();
  12. reader.setLineMapper(customerLineMapper);
  13. return reader;
  14. }

请注意,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:

  1. @Bean
  2. public FlatFileItemReader&lt;Customer&gt; customerItemReader() {
  3. FlatFileItemReader&lt;Customer&gt; reader = new FlatFileItemReader&lt;&gt;();
  4. reader.setLinesToSkip(1);
  5. reader.setResource(new ClassPathResource(&quot;/data/customer.csv&quot;));
  6. DefaultLineMapper&lt;Customer&gt; customerLineMapper = new DefaultLineMapper&lt;&gt;();
  7. DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
  8. tokenizer.setNames(new String[] {&quot;id&quot;, &quot;firstName&quot;, &quot;lastName&quot;, &quot;birthdate&quot;});
  9. customerLineMapper.setLineTokenizer(tokenizer);
  10. customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
  11. customerLineMapper.afterPropertiesSet();
  12. reader.setLineMapper(customerLineMapper);
  13. return reader;
  14. }

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.

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

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.

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

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:

确定