4

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

explorer
  • 486
  • 1
  • 6
  • 19
  • Found the following with some suggestions : https://jira.spring.io/browse/BATCH-966 – explorer Oct 26 '15 at 16:39
  • Can you add the code with which you launch the job? I think you could simply use a static Map somewhere in your code to share objects between your service and your job. – Thrax Oct 27 '15 at 15:21
  • @Thrax : I have edited the initial question with Code Scenario. – explorer Oct 27 '15 at 18:16

1 Answers1

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.

Thrax
  • 1,926
  • 1
  • 17
  • 32