Spring batch basic

Spring Batch is one of Spring module and open source framework used to build a lightweight and robust application. Spring Batch is mainly used for batch processing. So what does batch processing means?

Batch processing allows to executes jobs automatically(i.e without human interaction). Again what is Job? Job means some kind of task. Let’s see some example.

First Scenario – Suppose you have to read one excel file and save the data to the database.

  • One possible solution – we will have an upload option from UI, which will allow us to upload the excel and further some rest call will happen which will read all data from excel and save the data into Database.
  • Another Solution – We want to happen these all process automatically. It should read the excel file and save the data into the database. Here Spring Batch comes in picture. It can be some other scenario, for the understanding purpose we are considering this one.

Second Scenario – Suppose we are using JBOSS server or Tomcat server for development purpose. We have log folder where all log details will be available. Now what we want is to delete all log files which is older than one month.

  • One possible solution we will go and check manually and the logs which are older than one month, we will delete it.
  • The second option, why do we not schedule a job which will delete all logs older than one month. We can run batch every one month which will take care of this log cleanup task.

Let’s see some benefits of Spring Batch –

  • Spring Batch can be used to process a large amount of data for example logging/tracing, transaction management, job processing, and resource management.
  • It allows us to perform a particular task at a scheduled time.

 

 

Spring Batch Architecture –


 

 

Annotations used for Spring Batch.

@EnabledScheduling –

  • This annotation enables Spring batch task scheduling feature.
  • This is available in org.springframework.scheduling.annotation package. See here documentation.
  • Mainly used with configuration class or Spring boot main class.

@Scheduled


  • This annotation is used to make any method scheduled.
  • Available in org.springframework.scheduling.annotation package. See here documentation.
  • We need to define attributes fixedDelay or fixedRate.
  • Attributes of @Scheduled annotation –
    •  cron – An expression, triggers on the second, minute, hour, day of month, month and day of week.
    • fixedRate – if we give value 2000 for this, the method will get invoked after each 2 sec.
    • fixedRateString – behave similarly as the fixedRate only difference we can pass the value as String.
    • fixedDelay – if we give value 2000 for this, the method will get invoked after each 2 sec + time to execute that method. Suppose we have given the value for fixedDelay is 2000 i.e 2 sec and time taking to execute some method(with we are using fixedDelay) is 2 sec then total time will be 4 sec.
    • fixedDelayString –  behave similarly as the fixedRate only difference we can pass the value as String.
    • initialDelay – Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.
    • initialDelayString – Similar to initialDelay except we can pass the parameter as String.
    • zone – A time zone for which the cron expression will be resolved.

 

Example of Spring batch using Spring boot.