问题
Is there a way to pass a stream while launching the job through job Launcher, something similar to passing jobParameters?
I have a separate service for getting file and then I want to initiate the batch job to load it.
Code scenario :
Consider this sample. Here job is defined but actual launcher resides in the dependency underneath.
So consider in sample, I add a controller which read user's input file and then trigger the sample-job defined in sample which is run by joblauncher.run of underneath.
I was thinking to pass this file stream directly to the job's reader instead of writing it to external disc and reading in Reader's setSeResource
回答1:
After looking at the sample code you provided, I think you could do something like this :
1) Declare a static HashMap in the SimpleJobConfiguration class.
public static Map<String, Object> customStorage = new HashMap<String, Object>();
2) Populate this map from your service
SimpleJobConfiguration.customStorage.put("key", yourStream);
3) Use this static map in the setResource method of your ItemReader (as said in your previous question)
@Override
public void setResource(Resource resource) {
// Get your stream from the static map
Byte[] stream = (Byte[]) SimpleJobConfiguration.customStorage.get("key");
// 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);
}
This solution will only work if your service is next to your jobLauncher.
来源:https://stackoverflow.com/questions/33340367/passing-stream-to-job-as-parameter