Passing stream to job as parameter

僤鯓⒐⒋嵵緔 提交于 2019-12-22 10:38:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!