What is the difference between spring scheduled tasks and spring batch jobs

11,195

Solution 1

2 aspects which i can think of: afaik when a job-run fails, in 2. run, it will run with the same job parameters.. at least you can configure this i think. and this kind of error situations which you can configure more easily than writing all in code in the same place manually (your scheduled method). Secondly, maybe batch gives a structure to your code when you also have to read your data from somewhere, and write somewhere... batch has some kind of reader, processor, writer schema.. Also some automatically created database tables (BATCH_JOB_INSTANCE) and batch job results.. like when the job started etc...

Edit: More Reasons for a batch: large amount of data, Transaction management, Chunk based processing, Declarative I/O, Start/Stop/Restart, Retry/Skip, Web based administration interface.

Solution 2

Spring Scheduler is for orchestrating something based on a schedule. Spring Batch is a robust batch processing framework designed for the building of complex compute problems. Spring Batch does not handle the orchestration of jobs, just the building of them. You can orchestrate Spring Batch jobs with Spring Scheduler if you want.

Share:
11,195

Related videos on Youtube

wertigom
Author by

wertigom

Updated on September 15, 2022

Comments

  • wertigom
    wertigom over 1 year

    I dont understand the difference between scheduled tasks and batch jobs in spring. By scheduled tasks I mean the ones which are configured like these:

    @EnableScheduling 
    public class AppConfig{
    ..
    

    and used like

    @Scheduled(fixedRate=550)
    public void doSomething(){
    ..
    

    By batch jobs I mean these:

    @EnableBatchProcessing
    public class AppConfig{
    ..
    

    and lots of implementations like: Jobs, Job launcher, Steps, ItemReader, ItemWriter... etc

    I would like to know the main difference between them besides the implementation differences and also I am curious why to use batch jobs and make a lot of long implementations while we can use simple scheduled tasks. I mean the implementation of scheduled jobs is quite easy but maybe they had disadvantages according to the batch jobs?