Reading from streams instead of files in spring batch itemReader folder

I get the csv file as a webservice call that needs to be encoded. Right now I am saving it in the temp directory to provide it as setResource for Reader.

Is there a way to provide a stream (byte []), and instead save the file first?

+5
source share
1 answer

The setResource ItemReader method setResource ItemReader parameter as a parameter. This class has several ready-made implementations, among which you can find org.springframework.core.io.InputStreamResource . The constructor of this class accepts java.io.InputStream , which can be implemented by java.io.ByteArrayInputStream .

So technically, yes, you can use the byte[] parameter in ItemReader .

Now, how to actually do this, here are a few ideas:

1) Create your own FlatFileItemReader (since CSV is a flat file) and make it an implementation of StepExecutionListener

 public class CustomFlatFileItemReader<T> extends FlatFileItemReader<T> implements StepExecutionListener { } 

2) Override the beforeStep method, make your webservice call inside and save the result in a variable

 private byte[] stream; @Override public void beforeStep(StepExecution stepExecution) { // your webservice logic stream = yourWebservice.results(); } 

3) Override the setResource method to pass this stream as the actual resource.

 @Override public void setResource(Resource resource) { // Convert byte array to input stream InputStream is = new ByteArrayInputStream(stream); // Create springbatch input stream resource InputStreamResource res = new InputStreamResource(is); // Set resource super.setResource(res); } 

In addition, if you do not want to call your web service in ItemReader, you can simply save the byte array in JobExecutionContext and get it in the beforeStep method with stepExecution.getJobExecution().getExecutionContext().get("key");

+5
source

Source: https://habr.com/ru/post/1234322/


All Articles