This Application demonstrates functioning of spring batch with manual trigger as well as quartz scheduler triggered batch job.
This repo has 2 spring batch jobs.
Manual Batch Job- This batch job is triggered by BatchJobController. It reads data fromsource_producttable in DB, processes it in chunks using batch and pushes to another tabletarget_productin DB.Scheduled Batch Job- It does same operation as above but is scheduled instead using quartz scheduler.
See Configurations: BatchConfiguration, QuartzConfiguration
- Run
docker-compose up -dto start postgress container.- Start
SpringBatchQuartzApplicationor run./gradlew bootRun
Spring batch is a very efficient tool for batch processing. See Documentation.
Below is a reference to Spring Batch model.
A Job is core of spring batch. A batch job can have one or more steps.
Spring batch supports 2 types of processing, one is chunk based processing and another
is tasklet based processing.
-
chunkbased approach helps to divide step execution into chunks, it is preferable when dealing with large amount of data. It requiresreader(reads from source),process(processes read data in chunks) andwriter(writes to destination in chunks).There are multiple
readerandwriterimplementations provided by spring batch out of the box.@Bean(name = "manualBatchJobStep") public Step manualBatchJobStep(ProductsReader reader, ProductProcessor processor, ProductWriter writer) { return stepBuilderFactory.get("manualStep") .<SourceProduct, TargetProduct>chunk(2) .reader(reader) .processor(processor) .writer(writer) .faultTolerant() .skipLimit(2) .skip(Exception.class) .build(); }
See implementaions: ProductsReader, ProductProcessor, ProductWriter
-
taskletbased processing is when you need only processing logic for your job. Tasklets are useful for tasks like cleanup.@Bean(name = "taskletBatchJobStep") public Step taskletBatchJobStep(Tasklet tasklet) { return stepBuilderFactory.get("manualStep") .tasklet(tasklet) .build(); }
Listeners provide ability to execute code before and after a job is executed.
See implementations: Listener
A batch job can contain one or more steps. Below is how you can configure a job.
@Bean(name = "manualBatchJob")
public Job manualBatchJob(@Qualifier("manualBatchJobStep") Step manualStep, ManualJobListener listener) {
return jobBuilderFactory.get("manualJob")
.listener(listener)
.incrementer(new RunIdIncrementer())
.flow(manualStep)
.end()
.build();
}See Config: BatchConfiguration
Triggering a job manually See BatchJobService
@Autowired
private final JobLauncher jobLauncher;
public String triggerBatchJob(Job job) {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("startTime", currentTimeMillis())
.toJobParameters();
try {
return jobLauncher.run(job, jobParameters).getStatus().name();
} catch (JobExecutionException e) {
e.printStackTrace();
}
return BatchStatus.FAILED.name();
}Spring quartz is built on top of native quartz api.
Quartz is a framework which is very efficient for scheduling tasks and is very flexible.
A job is core object which will contain the actual logic for your job. In this application
it triggers a batch job. For it we need to implement QuartzJobBean.
See Quartz Job ScheduledJob.
Quartz Configuration requires
JobDetail- contains your quartz job bean, in this caseScheduledJob.CronTrigger- containsJobDetailand info about cron expression, missfire config etc.SchedulerFactoryBean- parent bean that contains info about triggers, data sources, custom properties file etc.
See QuartzConfiguration.
Since quartz job bean ( ScheduledJob ) is initialized at runtime for every trigger, spring cannot inject required dependencies in ScheduledJob bean by default. We need to add this autowiring capability for our ScheduledJob bean.
See implementation SchedulerJobFactory.
