简体   繁体   中英

Dynamically configure a Job in spring-batch

Is it possible to dynamically configure a Job in spring-batch ?

Here is what I want to do. I have created several different ItemReader , ItemWriter like below:

  • FlatFileItemReader
  • DBItemReader
  • FlatFileItemWriter
  • DBItemWriter
  • .....
  • XMLItemWriter

I want to be able to mix and match them dynamically while creating a batch Job. For example, suppose I need a Job with 2 steps. First step contains a Tasklet for pre-processing . Second Step will have a Tasklet for chunk based data processing using my reader/writer.... Something like this:

// define job parameters
JobParametersBuilder parameters = new JobParametersBuilder();

// create two steps
TaskletStep step1 = new TaskletStep(); 
TaskletStep step2 = new TaskletStep();
step1.setName("PreProcessingStep");
step2.setName("ChunkReadWriteStep");

// create two TaskLets
Tasklet tasklet1 = new PreProcessingTasklet();
Tasklet tasklet2; <------ HOW DO I ATTACH MY reader/writer IN THIS TASKLET??

// attach the TaskLet to the step
step1.setTasklet(tasklet1);
step2.setTasklet(tasklet2);

// attach the steps to the job
SimpleJob job = new SimpleJob("MyBatchJob");
job.addStep(step1);
job.addStep(step2); 

jobLauncher.run(job, parameters.toJobParameters());

In XML, I can do this like below:

<job id="MyBatchJob">
    <step id="preprocessing" next="readWriteStep">
        <tasklet ref="PreProcessingTasklet"/>
    </step>
    <step id="readWriteStep">
        <tasklet>
            <chunk reader="FlatFileItemReader" writer="DBItemWriter"/>
        </tasklet>
    </step>
</job>

But how do I do it programatically like above?

Spring Batch provides java based configuration for the building of jobs via java code instead of XML. By using the provided builders, you can dynamically construct jobs as you need. I'd strongly suggest using this method instead of wiring a ChunkOrientedTasklet by hand since there is quite a bit going on in that particular Tasklet .

The best place to get started with Spring Batch's java configuration is in the Spring Batch guide: http://spring.io/guides/gs/batch-processing/ . It's a 15 minute walk through on getting started with Spring Batch using tools like Spring Boot and java configuration.

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