0% found this document useful (0 votes)
227 views26 pages

Spring Boot - Batch Processing Notes - Raghu-224-249

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
227 views26 pages

Spring Boot - Batch Processing Notes - Raghu-224-249

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

[Raghu Sir] [NareshIT, Hyd]

CHAPTER#6 SPRING BOOT BATCH PROCESSING


1. Introduction:--

Batch:-- Multiple Operations are executed as one Task or one large/Big Task executed
step by Step.
=>Every task is called as “JOB” and sub task is called as “STEP”.
=>One JOB can contain one or more Steps (Step can also called as Sub Task).
=>One Step contains.
a. Item Reader (Read data from Source).
b. Item Process (Do calculations and logics/operations etc…).
c. Item writer (Provide output to next Step or final output).

=>JOBs are invoked by JobLauncher also known as JobStarter.


=>JOB, Steps and Lanuncher details must be stored in JobRepository (Config file).

Step Implementation:--
=>In a Job (work) we can define one or multiple steps which are executed in order
(step by step).
=>Job may contain 1 step, job may contain 2 step…… job may contains many Steps so,
finally 1-job = * (many) Step.
=>Every step having 3 execution stages
a. ItemReader<T>
b. ItemProcessor<I, O>
c. ItemWriter<T>.

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 224


[Raghu Sir] [NareshIT, Hyd]
a>ItemReader<T> :-- It is used to read data (Item) from source (Input location) as
input to current Step. A source can be File, DB, MQ, (Message Queues), Networks,
XML, JSON… etc.

b>ItemProcessor<I, O> :-- It is used to process input data given by Reader and returns
in Modifier (or same) format of data.

c>ItemWriter<T> :-- It will read bulk of Items from Processor at a time and writes to
one destination. Even Destination can be a File (ex: DB, Text File, CSV, EXCEL,
XML…etc).
=>To start Job, we need one JobLauncher… which works in general way or Scheduling
based.
=>Here details like: Job, Steps, Launcher… are store in one memory which is called as
JobRepository [Ex: H2DB or MySQL, DB … any one DB].

NOTE:--
1. An Item can be String, Array, Object of any class, Collection (List/Set….).
2. ItemReader will read one by one Item from Source. For example Source has 10
items then 10 times ItemReader will be executed.
3. ItemReader GenericType must match with ItemProcessor Input GenericType.
4. ItemProcess will read item by item from Reader and does some process (calculate,
convert, check conditions, and convert to any class Object etc…).
5. ItemProcessor will provide output (may be same as Input type) which is called as
Transformed Type.
6. ItemWriter will collect all output Items into one List type from Processor at a time
(Only one time).
7. ItemWriter writes data to any destination.

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 225


[Raghu Sir] [NareshIT, Hyd]
8. Source/Destinatation can be Text, DB, Network, MessageQueue, Excel, CSV, JSON
data, XML etc…

Step Execution Flow :--


=>Step contains 3 interfaces (impls classes) given with generic types.
a. ItemReader<T> : (T= Input Data Type)
b. ItemProcessor<I, O> : ( I must match with Reader T type and O must match with
writer T type).
c. ItemWriter<T> : (T = Type after processing)

=>Here T/I/O can be String Object of any class or even collection (List, Set…).
=>Here ItemReader reads data from source with the helps of steps.
=>Reader and Processor are executed for every item, but writer is called based on
chunk size.

Ex:-- No. of Items = 200, chunk=50 then Reader and Processor are executed 200 times
but writer is called one time after 50 items are processed (Here 4 times executed).
=>ItemWriter writes data to destination with the help of step.

Spring Boot Batch coding Files and Steps:-


Batch programming is implemented in 3 stages.
1. Step Creation
2. Job Creation
3. Job Executions

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 226


[Raghu Sir] [NareshIT, Hyd]
1. Step Creation:- This process is used to construct one (or more) Step(s).
Ex:-- Step1, Stpe2, Step3..
=>here, Step is interface, It is constructed using “StepBuilderFactory (C)” with 3 inputs
taken as (interfaces Impl class object) :
a>ItemReader<T>
b>ItemProcessor<I, O>
c>ItemWriter<T>

=>Programmer can define above interfaces Impl classes or can be also use exist
(pre-defined) classes.
=>Reader, Writer, Processor is functional interfaces. We can define them using
Lambda Expression and methods references even.

2>Job Creation:-- One job is collection of Steps executed in order (one by one).
=>Even job may contain one Step also. Here, job is interface which is constructed using
“JobBuilderFactory <C>” and Step (I) instances.
=>To execute any logic before or after job, define Impl class for “JobExecutionListener
(I)” having method
Like: beforeJob(…) : void and
afterJob(…) : void
=>Here Listener is optional, It may be used to find current status of Batch (Ex:
COMPLETED, STOPTES, FAILED…) start date and time, end date and time etc.

3>Job Execution:-- Once Steps and job are configured, then we need to start them
using “JobLauncher (I)” run(…) method.
=>This run(…) method takes 2 parameters
a. Job (I) and
b. JobParameter (C)

=>Here, JobParameters (C) are inputs given to Job While starting this one.
Ex:- Parameters are : Server Data and Time, Customer Name flags (true/false), Task
Name… etc.
=>JobParameters (C) object is created using “JobParametesBuilder” and its method
toJobParameters().

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 227


[Raghu Sir] [NareshIT, Hyd]

Step :-- One Step can be constructed using StepBuilderFactory (sf) class by providing
name, chunk size, reader, processor and writer.

StepBuilderFactory (sf):--
sf.get(“step1”) =>Provide name of Step.
.<String, String> chunk(1) => No. of Items to be read at a time.
.reader (readerObj) =>Any Impl class of IteamReader<T> (I)
.processor(processorObj) =>Any Impl class of itemProcessor <I, O>
.writer(writerObje =>Any Impl class of ItemWriter <T> (I)
.build(); =>Convert to step (impl class) Object.

UML Notation:--

JobBuilderFactory (C) :--


=>This class is used to create one or more Jobs using Steps, listener, Incrementer….
=>Job (I) Construction flow

JobBuilderFactory jf :--
Jf.get(“jobA”) =>Job Name
.incremental(runIdIncrementar) =>Incrementer
.listener (jobExListener) =>Job Execution Listener
.start (stepA =>First Step

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 228


[Raghu Sir] [NareshIT, Hyd]
.next (stepB)
.next (stepC) =>Steps in Order
.next (stepD)
.build(); =>Create job Type

JobExecutionListener (I):--
=>This Interface is provided Spring Batch f/w, which gets called automatically for our
Job.
=>For one job – one Listener can be configured.
=>It is an Interface which has provided two abstract methods.
a. beforeJob (JobExecution) : void
b. afterJob (JobExecution) : void
=>If we write any impl class for above Listener (I) we need to implement two abstract
method in our class.
=>Some times we need only one method in our class file afterJob() method is required.
Then go for JobListenerAdapter(C) which has provided default impl logic for both
beforeJob; and afterJob(); methods.
=>JobExecution is a class which is used to find current job details like jobParameters,
BatchStatus, stepExecutions etc…

=>***BatchStatus is an enum having possible values : COMPLETED, STARTING,


STARTED, STOPPING, STOPPED, FAILED, ABANDONED, UNKNOWN.

Q>What are possible Job Status/Batch Status in Batch Programming?


A> All possible status in Batch Execution are given as enum “BatchStatus”. Those are

1 COMPLETED =>Successfully done.


2 STARTING =>About to call run(…).
3 STARTED =>Entered into run(…).

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 229


[Raghu Sir] [NareshIT, Hyd]
4 STOPPED =>Abort & come out of run(…).
5 STOPING =>Run(…) method finished.
6 FAILED =>Exception in run(…).
7 ABANDONED =>Stopped by External service.
8 UNKNOWN =>Unable to provide.

**JobLauncher (I) :--


=>This interface is used to invoke our Job with Parameters (input) like creationTime,
uniqueId (name), programmerData etc.
=>This Interface is having method run (Job, JobParameters).
=>This Interface object is created by JobParametersBuilder which holds data in Map
<key, Value> style.

Final Spring Boot Batch Implementation Diagram:--

Step#1:- Define one Spring Starter Project and select Spring Batch or else add below
dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 230


[Raghu Sir] [NareshIT, Hyd]
Step#2:- Define Impl classes for IteamReader, ItemProcessor and IteamWriter
Ex: Reader (C), Processor (C), Writer (C).

Step#3:- Define one class for Batch Configuration.


a>Create Beans for Reader, Processor, Writer.
b>Create Bean for Step using StepBuilderFactory
(BatchConfig--------------------<>StepBuilderFactory)
c>Create Bean for job using JobBuilderFactory
(BatchConfig----------------------<>JobBuilderFactory)
d>Define JobExecutionListener (I) Impl class (It is optional)

Step#4:- Create ConsoldeRunner to make job Execution using JobLauncher [run(…)


method].
ConsoleRunner--------------------<>JobLauncher
ConsoleRunner--------------------<>Job
=>Provider JobParameters using its builder obj.

Step#5:- At Starter class level add annotation: @EnableBatchProcessing

Step#6:- add below key in application.properties


spring.batch.job.enabled=false
=>By default Starter class executes Job one time on startup application and even
JobLauncher is executing another time. To avoid starter execution by starter class add
above key as false value.

Coding order:--
1. Reader
2. Processor
3. Writer
4. Step Configuration using StepBuilderFactory
5. JobExecutionListener
6. Job Config –job BuilderFactory
7. JobParameters using JobParametersBuilder
8. JobLauncher using CommandLineRunner
9. ** Add key in application.properties

Spring.batch.job.enabled=false
=>To avoid execution of job multiple times (by Starter class)

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 231


[Raghu Sir] [NareshIT, Hyd]
#30. Folder Structure of SpringBoot batch Programing:--

application.properties:--
#Disable this otherwise job executed one time by SpringBoot on startup
And also one more time by our launcher
spring.batch.job.enabled=false
spring.batch.initialize-schema=always
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=system

1>DataReader:--
package com.app.batch.reader;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.stereotype.Component;

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 232


[Raghu Sir] [NareshIT, Hyd]
@Component
public class DataReader implements ItemReader<String> {

String message[] = {"hi","hello","hoe"};


int index;

@Override
public String read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
if(index < message.length)
{
return message[index++];
} else {
index=0;
}
return null;
}
}
2. DataProcessor:--
package com.app.batch.processor;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

@Component
public class DataProcessor implements ItemProcessor<String, String> {

@Override
public String process(String item) throws Exception {
return item.toUpperCase();
}
}
3. DataWriter:--
package com.app.batch.writer;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;

@Component
public class DataWriter implements ItemWriter<String> {

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 233


[Raghu Sir] [NareshIT, Hyd]
@Override
public void write(List<? extends String> items) throws Exception {
for(String item:items) {
System.out.println(item);
}
}
}
4. BatchConfig.java:--
package com.app.batch.config;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.
EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.app.batch.listener.MyJobListener;
import com.app.batch.processor.DataProcessor;
import com.app.batch.reader.DataReader;
import com.app.batch.writer.DataWriter;

@Configuration
@EnableBatchProcessing
public class BatchConfig {

@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;

@Bean
public Job jobA() {
return jobBuilderFactory.get("jobA")
.incrementer(new RunIdIncrementer())
.listener(listener())
.start(stepA())
//.next(step)

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 234


[Raghu Sir] [NareshIT, Hyd]
.build();
}
@Bean
public Step stepA() {
return stepBuilderFactory.get("stepA")
.<String, String>chunk(50)
.reader(new DataReader())
.processor(new DataProcessor())
.writer(new DataWriter())
.build();
}
@Bean
public JobExecutionListener listener() {
return new MyJobListener();
}
}
5. MyJobListener.java:--
package com.app.batch.listener;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.stereotype.Component;

@Component
public class MyJobListener implements JobExecutionListener {

@Override
public void beforeJob(JobExecution jobExecution) {
System.out.println(jobExecution.getStartTime());
System.out.println(jobExecution.getStatus());
}
@Override
public void afterJob(JobExecution jobExecution) {
System.out.println(jobExecution.getEndTime());
System.out.println(jobExecution.getStatus());
}
}
6. MyJobLauncher.java:--
package com.app.batch.runner;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 235


[Raghu Sir] [NareshIT, Hyd]
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyJobLauncher implements CommandLineRunner {

@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;

@Override
public void run(String... args) throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(job, jobParameters);
}
}
Output:--

2. Spring Boot Batch Processing : Converting .csv file data to DataBase table:--
=>Consider input data given by csv file is related to products having product id, name,
cost, by using one item reader convert .csv file data to Product class object.
=>Define one Processor class to calculate gst (Goods and Service Tax) and discount of
product.
=>Finally Product should have id, name, cost, gst, discount.
=>Use one ItemWriter class to convert one object to one Row in DB table.

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 236


[Raghu Sir] [NareshIT, Hyd]

=>In realtime CSV (Comma Separated Values) Files are used to hold large amount of
data using symbol / Tokenizer ‘,’ or (It will be , . -, \, /).
=>This data will be converted to one Model class(T) object format.
=>It means one line data(id, code, cost…) converted to one java class object by Reader.
=>Calculate Discount and GST… etc using Processor class. Processor may return same
class (or) different Object (i.e I==O)
=>Based on chunck(int) size all objects returned by Processor will be collected into one
List by Writer.
=>Every Object in List will be converted into its equal “INSERT SQL…”.
=>Multiple SQLs are converted to one JDBC Batch and sent to DB at a time.
=>No of calls between Writer and Database depends on chunk size (and no of Items).

Technical Flow for csv to DB using Spring Boot Batch:--

FlatFileItemReader:---
=>This class is provided by Spring Batch to read data from any Text Related file
(.txt, .csv,...).

DefaultLineMapper:-- It will load one row(/n) at a time as one Line Object.

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 237


[Raghu Sir] [NareshIT, Hyd]
DelimitedLineTokenizer :-- It will devided one line values into multiple values using
any Delimiter (like comma, space, tab, new, line, dash.. etc).
=>Default Delimiter is “,” [Comma].

BeanWrapperFieldSetMapper :-- It maps data to variables, finally converted to one


class type (TargetType).

Execution Flow:--
=>Spring Boot Batch f/w has provided pre-defined ItemReaders and ItemWriters.
=>FlatFileItemReader <T> is used to load any file (source) as input to read data
example .txt, .csv,… etc.
=>It will read one line data based on LineMapper (\n).
=>One Line data is divided into multiple values based on Tokenizer (Delimitar = ,).
=>These values are mapped to one class (T) type object also known as Target.

=>This Target object is input to ItemProcessor. After processing object (calculations,


Logical checks, data modifications… etc) Processor returns output type Object.
=>JdbcBatchItemWriter collects all Items from ItemProcessor into List<T> based on
chunk (int) size.
=>Provide one DataSource (DB Connection) to communicate with DB Tables.

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 238


[Raghu Sir] [NareshIT, Hyd]
=>Define one SQL which inserts data into table based on Parameter (Variable Name)
SourceProvider.
=>Multiple SQLs are converted to one batch and send to DB table.
*** Here Batch Size = Chunk Size.
Example chunk size = 150 (int value)
=>Chunk indicates maximum Items to be sent to Writer in one network call from Step.
=>For last network call chunk may contain few Items (no. of Items <chunk size).
-----------------------------------------------------------------------------------------------------------------

=>In application.properties add below key-value pairs :


spring.batch.job.enabled=false
spring.batch.initialize-schema=always

=>Here job.enabled=false will disable execution of job by one time on app starts by
Starter class.
=>Initialize-schema=always will allow Spring Batch to communicate DB to hold its
Repository details (like Job, Step, current status details…).
=>***In case of Embedded Database initialize-schema not required.

Coding Order for Batch :- csv to ORACLE


1> Model class
2> Processor class***
3> BatchConfig
4> ConsoleRunner
5> Application.properties
6> Starter class
7> Pom.xml

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 239


[Raghu Sir] [NareshIT, Hyd]
#31. Folder Structure of Spring Boot Batch transferring data from csv file to DB:--

application.properties:--
spring.batch.job.enabled=false
spring.batch.initialize-schema=always
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=system

1. Model class (Product.java):--


package com.app.model;
import lombok.Data;

@Data
public class Product {
private Integer prodId;
private String prodName;
private Double prodCost;
private Double prodGst;
private Double prodDisc;
}

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 240


[Raghu Sir] [NareshIT, Hyd]
2. ProductProcess.java:--
package com.app.process;
import org.springframework.batch.item.ItemProcessor;
import com.app.model.Product;

public class ProductProcessor implements ItemProcessor<Product, Product> {

@Override
public Product process(Product item) throws Exception {
item.setProdGst(item.getProdCost()*12/100.0);
item.setProdDisc(item.getProdCost()*25/100.0);
return item;
}
}
3. BatchConfig.java:--
package com.app.config;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.
EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.
BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import com.app.model.Product;
import com.app.process.ProductProcessor;

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 241


[Raghu Sir] [NareshIT, Hyd]
@EnableBatchProcessing
@Configuration
public class BatchConfig
{
//STEP
@Autowired
private StepBuilderFactory sf;

@Bean
public Step stepA() {
return sf.get("stepA")
.<Product, Product> chunk(3)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}

//JOB
@Autowired
private JobBuilderFactory jf;

@Bean
public Job jobA() {
return jf.get("jobA")
.incrementer(new RunIdIncrementer())
.start(stepA())
.build();
}
}
//dataSource -- Creates DB connection
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
ds.setUsername("system");
ds.setPassword("system");
return ds;
}

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 242


[Raghu Sir] [NareshIT, Hyd]
//1. Item Reader from CSV file
/** Code snippet for CSV to ORACLE DB**/
@Bean
public ItemReader<Product> reader() {
FlatFileItemReader <Product> reader = new FlatFileItemReader<Product>();
//--reader.setResource(new FileSystemResource("d:/abc/products.csv"));
//--loading file/Reading file
reader.setResource(new ClassPathResource("myprods.csv"));
//--read data line by line
reader.setLineMapper(new DefaultLineMapper<Product>(){{
//--make one into multiple part
setLineTokenizer (new DelimitedLineTokenizer() { {
//-- Store as variables with names
setNames ("prodId", "prodName", "prodCost");
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Product>() {{
//--Convert to model class object
setTargetType (Product.class);
}});
}});
return reader;
}
//2. Item Processor
@Bean
public ItemProcessor<Product, Product> processor() {
//return new ProductProcessor();
return (p)-> {
p.setDisc(p.getCost()*3/100.0);
p.setGst(p.getCost()*12/100.0);
return p;
}
//3. Item Writer
@Bean
public ItemWriter<Product> writer() {
JdbcBatchItemWriter<Product> writer = new JdbcBatchItemWriter<>();
writer.setDataSource(dataSource ());
writer.setItemSqlParameterSourceProvider(new
BeanPropertyItemSqlParameterSourceProvider<Product>());
Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 243
[Raghu Sir] [NareshIT, Hyd]
writer.setSql("INSERT INTO PRODSTAB(PID, PNAME, PCOST, PGST, PDISC)
VALUES(:prodId, :prodName, :prodCost, :prodGst, :prodDisc)");
return writer;
}
4. MyJobLauncher.java:--
package com.app.launcher;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyJobLauncher implements CommandLineRunner{

@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;

@Override
public void run(String... args) throws Exception {
jobLauncher.run(job,new JobParametersBuilder()
.addLong("time",System.currentTimeMillis())
.toJobParameters());
}
}
----------------------------------------------------------------------------------------------------------------
DB Table:-- Execute below SQL query before execution of program to create table.
SQL>CREATE TABLE prodstab (PID number (10), PNAME varchar2 (50), PCOST number,
PGST number, PDISC number);

Create a file under src/main/resources folder:--


myprods.csv : under src/main/Resources.
10, PEN, 2.36
11, CAR, 8.8
12, BUS, 99.56
---------------------------------------------------------------------------------------------------------------

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 244


[Raghu Sir] [NareshIT, Hyd]
=>To read file from outside Application from File System (D:/driver or C:/driver) then
use FileSystemResource.
=>It same way to read file from network (internet URL based) then use UrlResource in
place of ClassPathResource.

Task:--
#1. Write Spring boot Batch Application To read data from database (Oracle DB) using
“JdbcCursorItemReader” and write data to csv file using “FlatFIleItemWriter”.

Task#1: (JDBC ->CSV)


JdbcCursorItemReader<T>
FlatFileItemWriter<T>

#2. Write data from MongoDB using “MongoItemReader” and write data to JSON file
using “JsonFIleItemWriter”.

Task#2: MongoDB to JSON file


MongoItemReader<T>
JsonFileItemWriter<T>

Task#2: ORACLE to CSV


(ORM -> CSV)
HibernateCursorItemReader<T>
FlatFileItemWriter<T>

Note:-- Every step contains


a> ItemReader
b> ItemProcessor
c> ItemWriter

=>All these are functional Interface (contains only one (1) abstract method.
So Logic can be provided to these interfaces using Lambda Expression.

=>Lambda coding syntax:--


(parameters) -> {
method body;
}

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 245


[Raghu Sir] [NareshIT, Hyd]

=>Writing Lambda expression


Simple code:--
public class MyProcessor implements ItemProcessor<Product, Product> {
public Product process(Product p) throw Exception {
double cost=p.getCost();
p.setDisc(cost*3/100.0);
p.setGst(cost*12/100.0);
return p;
}
}
@Bean
public ItemProcessor<Product, Product>
process() {
return new MyProcessor();
}

Lambda Exp:--
@Bean
ItemProcessor<Product, Product> process() {
return (p) -> {
double cost=p.getCost();
p.setDisc(cost*3/100.0);
p.setGst(cost*12/100.0);
return p;
};
}
Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 246
[Raghu Sir] [NareshIT, Hyd]
***Different ways of creating object and calling method:--
Consider below class:--
class Sample {

Sample() {
System.out.println("Constructor");
}
void show () {
System.out.println("Method");
}
}
Text class:--
public class WayOfCreatingObject
{
public static void main(String[] args) {

//1. Creating object and calling method


Sample s = new Sample();
s.show();

//2. Creating Object and calling method


new Sample().show();

//3. Creating object (add extra code, override methods) and calling method
new Sample () {
public void show() {
System.out.println("NEW LOGIC");
}
}.show();

//4. While creating object invoke method


new Sample() {
//Instance initialize block
{
show();
}
};
}
}

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 247


[Raghu Sir] [NareshIT, Hyd]
Ex:-- Code Extension and instance block:--
package com.app;

public class Sample {


System.out.println(“From instance-inside”);
}
public sample() {
System.out.println(“From Constructor”);
}
//getters setters.. toString
}

package Com.app;
public class Test
Public static void main {

//1. Create object then call method


Sample s1 = new Sample();
s1.setSid(10);

//2. calling method while creating obj


Sample s2 = new Sample() { {
setSid(10);
}};

//3. Code Extension for current object


Sample s3 = new Sample() {
void m1() {
System.out.println("OK");
}
{
setSid(10);
m1();
}
};
}

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 248


[Raghu Sir] [NareshIT, Hyd]

Naresh IT, Hyderabad P: 040-2374 6666,9000994007 /08] Page 249

You might also like