Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch Passing list of values as a parameter

I want to pass list of id's as one of parameter to Spring batch. Is this possible to achieve?

Thanks in advance.

like image 328
Aditya Avatar asked Jan 13 '14 11:01

Aditya


2 Answers

What you are trying to do is not possible.

From the JobParameter doc:

Domain representation of a parameter to a batch job. Only the following types can be parameters: String, Long, Date, and Double. The identifying flag is used to indicate if the parameter is to be used as part of the identification of a job instance.

You might be tempted write your list of of id's to a comma delimited string and pass that as a single parameter but beware that when stored in the DB it has a length of at most 250 bytes. You'll either have to increase that limit or use another way.

Perhaps you can explain what why you need to pass that list of ids.

like image 98
M.P. Korstanje Avatar answered Oct 06 '22 18:10

M.P. Korstanje


If you want to pass the list from ItemReader, then you have to get JobParameters first (you have to declare your reader to be step scoped for that, see this thread also).

You will have to put your list as a parameter to the JobParameters. As JobParameters is immutable, you will have to create a new object then

List yourList = ....   
JobParameters jp = (JobParameters) fac.getBean("params");
Map map=params.getParameters();
map.put("yourList", list);
params=new JobParameters(map);
launcher.run(job, params);
like image 26
senseiwu Avatar answered Oct 06 '22 17:10

senseiwu