Skip to content

Add support for passing objects as job parameters [BATCH-966] #2615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Dec 10, 2008 · 7 comments
Closed
Labels
in: core status: declined Features that we don't intend to implement or Bug reports that are invalid or missing enough details type: feature

Comments

@spring-projects-issues
Copy link
Collaborator

Eddie Mc Greal opened BATCH-966 and commented

As discussed in http://forum.springframework.org/showthread.php?t=64673

At the moment there is no way of passing objects to the job execution. This would very useful when executing a batch from within e.g. a web application where by a user uploads a file to be processed. There is currently no way of passing this resource to the ItemReader - you have to save it to disk with a static URL so as the ItemReader can pick it up or you can use the dynamic filename mechanism.

In any case I think its something to consider for a future release!


No further details from BATCH-966

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

I would prefer to leave this feature to users. The JobParameters are really part of the identity of a job (hence the problem that Robert mentioned with equals()), so making them arbitrary objects just creates more issues than it solves. If your job instance really has an identity, then it should be possible for your launcher client and a StepExecutionListener to share some state (e.g. via a Map) and for the listener to use a key from the JobParameters to populate the step component. I would be more interested in looking at a volatile facet of ExecutionContext - anything you put in there would need to be derived from JobParameters or persistent context of some sort. For your use case in the forum you would still need to organise the shared state, but I don't think that's a problem for you.

@spring-projects-issues
Copy link
Collaborator Author

Eddie Mc Greal commented

I understand now why using the JobParameters is not such a good idea - and I agree that probably the best transporter of objects into the process is the ExecutionContext.
In could basically move my HashMap of Objects from the DynamicJobParameters to the ExecutionContext class but this means I'd have to

  1. Patch the batch code by adding a volatile HashMap to JobExecution - and wait for you guys to add this feature later
  2. Create my own JobLauncher that in turn creates a DynamicExecutionContext - then I'd have to do some ugly casting in the ItemReader to retrieve my resource.
  3. Start messing with thread local storage - no thanks!

#1 is probably the easiest and best solution

BTW - My ItemReader has a property called inputResourcID which I can set to a constant e.g. "XYZ" in my job xml config file. I can use this later to retrieve the object from the ExecutionContext. This means there is nothing hard coded and I can configure multiple resources or other objects that I may need during execution

In general I would think that being able to access objects that are dynamically created outside of the job execution is a necessary feature. My scenario is pretty common - uploading a file to a webserver and then running a batch with uploaded file.

@spring-projects-issues
Copy link
Collaborator Author

Eddie Mc Greal commented

sorry just to clarify
#1, #2, #3 are alternatives

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

I don't think you need to patch anything actually. You can just share a Map between the code that calls the JobLauncher and a StepExecutionListener (as already suggested). If you want to use ThreadLocal that's up to you, but if the map keys are not unique then you have more of a business problem than a technical one. Or am I missing something?

@spring-projects-issues
Copy link
Collaborator Author

Eddie Mc Greal commented

Sorry Dave but I don't understand how I can " share a Map between the code that calls the JobLauncher and a StepExecutionListener (as already suggested)"

The simple job launcher launches a job as follows:

final JobExecution jobExecution = jobRepository.createJobExecution(job, jobParameters); 
taskExecutor.execute(new Runnable() {... etc 

My ItemReader needs to access a Map that I would create before I call taskExecutor.execute - this map can be a member of "MyLauncher" e.g.
MyLauncher. dynamicValuesHashMap
But how can I navigate from the JobExecution to my map in my launcher? - the StepExecutionListener has no path to the launcher. I don't want to put the object in the ExecutionContext as it would serialize the object which is a ByteArrayResource and it would end up serializing the uploaded file which is not necessary

So again concretely; how do I navigate from
MyItemReader.beforeStep(StepExecution stepExecution)
to my Map in e.g. MyLauncher.dynamicValuesHashMap ?

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Something like this:

<bean class="MyItemReader">
<property name="dynamicValuesHashMap" ref="map"/>
...
</bean>
<bean class="MyLauncher">
<property name="dynamicValuesHashMap" ref="map"/>
...
</bean>

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Nothing to do here, so resolving.

@spring-projects-issues spring-projects-issues added type: feature status: declined Features that we don't intend to implement or Bug reports that are invalid or missing enough details in: core labels Dec 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: declined Features that we don't intend to implement or Bug reports that are invalid or missing enough details type: feature
Projects
None yet
Development

No branches or pull requests

1 participant