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) {
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");
source share