Spring Batch Passing list of values as a parameter
Asked Answered
C

4

12

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

Thanks in advance.

Chaffin answered 13/1, 2014 at 11:7 Comment(0)
U
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.

Uhf answered 2/12, 2014 at 12:3 Comment(0)
O
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);
Ootid answered 13/1, 2014 at 11:28 Comment(0)
Q
0

If you are trying to use list as an input for your reader then directly its not possible as Spring batch does not have that feature. However You can use ArchivalItemReader to wrap the itemReader and create the list of input. this will allow you to convert your input into list and then use that list in itemProcessor.

It will look something like

public class ArchivalItemReader extends JpaPagingItemReader {
private static final Integer READER_PAGE_SIZE = 100;

public EntityManagerFactory entityManagerFactory;

public ArchivalItemReader(EntityManagerFactory entityManagerFactory) {
    this.entityManagerFactory = entityManagerFactory;
}

public JpaPagingItemReader<Earmark> archivalItemReader(final Map<String, Object> context) throws Exception {
    final Map<String, Object> queryParams = new HashMap<>();
    queryParams.put(CURRENT_DATE, context.get(CURRENT_DATE));
    queryParams.put("statuses", Arrays.asList(REVERSED, CLEARED, REJECTED));

    final JpaNativeQueryProvider<Earmark> queryProvider = new JpaNativeQueryProvider<>();
    queryProvider.setSqlQuery(ARCHIVAL_QUERY);
    queryProvider.setEntityClass(Earmark.class);
Quantify answered 10/7, 2024 at 20:8 Comment(0)
T
-1

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.

Turret answered 28/10, 2022 at 4:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.