简体   繁体   中英

How to pass List to Spring Batch ItemReader via REST call

I'm working on a REST method that will perform a job using Spring Batch.

I have a simple job defined,

<job id="myIndexJob" xmlns="http://www.springframework.org/schema/batch">
    <step id="step1">
        <tasklet>
            <chunk reader="myIndexItemReader" processor="myIndexItemProcessor" writer="myIndexItemWriter" commit-interval="1" />
        </tasklet>
    </step>
</job>

This job mimics a question I posted earlier,

Spring Batch ItemReader list processed only once

But this time, instead of executing the job on a schedule, I want to manually execute it via a REST call.

The problem I'm having is passing a List to the myIndexItemReader . My REST call will generate a List based on some query string. The previous question I posted got it's List handed to it via the spring bean in the XML each time the step ran.

I'd like to do something like this,

@RequestMapping(value="/rest/{regex}", method=RequestMethod.GET)
public void run(@PathVariable String regex) {

    List<String> myList = new ArrayList<>();
    myList.add("something");

    long nanoBits = System.nanoTime() % 1000000L;
    if (nanoBits < 0) {
        nanoBits *= -1;
    }
    String dateParam = new Date().toString() + System.currentTimeMillis() 
            + "." + nanoBits;
    JobParameters param = new JobParametersBuilder()
            .addString("date", dateParam)
            .toJobParameters();
    JobExecution execution = jobLauncher.run(job, param);

}

but I just don't know how to pass myList to the myIndexItemReader .

As of now I can do this by creating a RepeatTemplate and calling iterate on a callback, but the job chunk seems more clean.

Anyone have any ideas or suggestions? Thanks /w

I took an alternate approach and stored information in a database table based on the REST criteria. Then the ItemReader read the table and cleared it after each run.

You can pass queries as job parameters, but you have to be carefully because string job parameter has a finite lenght (250) (look Metadata schema ).
If this can be a limit you can precompile a property file like that:

queries.properties

query1=<query string 1>
query2=<query string 2>
query3=<query string 3>
queryn=<query string n>

As job parameters you can pass:

queriesIdsCount (integer): number of queries (0..n)
queryId0 (string): identifier of query in queries.properties file (ex: query2)
queryId1 (string): (ex. query3)
queryIdn (string): (ex. query1)

and so on so you can select queries from your list.
With a Tasklet or a usual Reader/Process/Writer (as first step) you can process your job parameters and create List<> using REST.
Job parameters are available using spEL look about late-binding .

I hope I was clear, English is not my native language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM