11

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

Thanks in advance.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Aditya
  • 1,033
  • 3
  • 11
  • 24

3 Answers3

16

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.

M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
3

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);
Community
  • 1
  • 1
senseiwu
  • 5,001
  • 5
  • 26
  • 47
0

You cannot use the List<T> concept itself in spring-batch, but I think you can implement your intentions(listOf(a, b, c, d..)) in the following way.

The job parameter itself receives a comma-separated string of items.

@Nonnull
private List<String> someList = Collections.emptyList();

@Value("#{jobParameters['someList']}")
public void setTableNames(@Nullable final String someList) {
    if (StringUtils.isNotBlank(tableNames)) {
        this.someList = Arrays.stream(StringUtils.split(someList, ","))
                .map(String::trim)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList());
    }
}

Hope it was helpful for using list-type parameters in spring-batch! Thanks.

Johnny
  • 1
  • 1