I have a requirement of sending a Custom Object to the Spring Batch Job , where this Object is used continuously used by the Item Processor for the business requirement.
How can we send custom object from outside to the Job Context. This Object changes from Job to Job and generated at runtime depending on Business case.
How can send this as a Job Parameter? or is there any way that i can set this Object to the respective Job ?
can overriding Spring JobParameter help me in any way? or are there any Big issues as an outcome of this Overriding behaviour ?
JobParameters is a set of parameters used to start a batch job. JobParameters can be used for identification or even as reference data during the job run. They have reserved names, so to access them we can use Spring Expression Language.
public interface JobExplorer. Entry point for browsing executions of running or historical jobs and steps.
What is a "Job Repository" in Spring Batch? "JobRepository" is the mechanism in Spring Batch that makes all this persistence possible. It provides CRUD operations for JobLauncher, Job, and Step instantiations.
I have summarized reasons why you cannot send object as JobParameter
in this question. There are couple of ideas on that question how you can do it.
TL:TR: I think best way would be either to create table in DB which stores Object and pass id of that record as JobParameter or to serialize Object to json and pass it as String in job as JobParameter. If you go with second option be aware that string_val is stored in DB as varchar 250 so limit is 250 characters.
Use the below class to send CustomObject.
public static class CustomJobParameter<T extends Serializable> extends JobParameter {
private T customParam;
public CustomJobParameter(T customParam){
super(UUID.randomUUID().toString());//This is to avoid duplicate JobInstance error
this.customParam = customParam;
}
public T getValue(){
return customParam;
}
}
===========================
Sending parameter:
JobParameters paramJobParameters = new JobParametersBuilder().addParameter("customparam", new CustomJobParameter<MyClass>(myClassReference)).toJobParameters();
Retrieving parameter:
MyClass myclass = (MyClass)jobExecution.getJobParameters().getParameters().get("customparam").getValue();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With