Spring Batch Application

This commit is contained in:
Kunwar
2020-07-12 01:34:03 +05:30
parent de69bf0bc0
commit 6649bbd2ec
6 changed files with 39 additions and 52 deletions

View File

@@ -1,9 +1,10 @@
package com.javadevjournal.springbootbatch.config;
import com.javadevjournal.springbootbatch.listener.SpringBatchJobCompletionListener;
import com.javadevjournal.springbootbatch.listener.SpringBatchJobExecutionListener;
import com.javadevjournal.springbootbatch.listener.SpringBatchStepListener;
import com.javadevjournal.springbootbatch.model.Employee;
import com.javadevjournal.springbootbatch.step.EmployeeItemProcessor;
import com.javadevjournal.springbootbatch.model.StockInfo;
import com.javadevjournal.springbootbatch.step.StockInfoProcessor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
@@ -18,11 +19,8 @@ import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.javadevjournal.springbootbatch.listener.SpringBatchJobCompletionListener;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.scheduling.annotation.Scheduled;
/**
* @Author - Kunwar Vikas
@@ -50,9 +48,9 @@ public class SpringBatchConfig {
public Step StockPricesInfoStep() {
return stepBuilderFactory.get("step1")
.listener(new SpringBatchStepListener())
.<Employee, String>chunk(10)
.<StockInfo, String>chunk(10)
.reader(reader())
.processor(processor())
.processor(stockInfoProcessor())
.writer(writer())
.faultTolerant()
.retryLimit(3)
@@ -61,19 +59,19 @@ public class SpringBatchConfig {
}
@Bean
public FlatFileItemReader<Employee> reader() {
return new FlatFileItemReaderBuilder<Employee>()
public FlatFileItemReader<StockInfo> reader() {
return new FlatFileItemReaderBuilder<StockInfo>()
.name("stockInfoItemReader")
.resource(new ClassPathResource("csv/stockinfo.csv"))
.delimited()
.names(new String[] {"stockId", "stockName","stockPrice","yearlyHigh","yearlyLow","address","sector","market"})
.targetType(Employee.class)
.targetType(StockInfo.class)
.build();
}
@Bean
public EmployeeItemProcessor processor() {
return new EmployeeItemProcessor();
public StockInfoProcessor stockInfoProcessor(){
return new StockInfoProcessor();
}
@Bean
@@ -82,7 +80,8 @@ public class SpringBatchConfig {
.name("stockInfoItemWriter")
.resource(new FileSystemResource(
"target/output.txt"))
.lineAggregator(new PassThroughLineAggregator<>()).build();
.lineAggregator(new PassThroughLineAggregator<>())
.build();
}
@Bean

View File

@@ -1,13 +0,0 @@
package com.javadevjournal.springbootbatch.model;
import lombok.Data;
@Data
public class Employee {
private String firstName;
private String lastName;
private String department;
public Employee() {
}
}

View File

@@ -1,22 +0,0 @@
package com.javadevjournal.springbootbatch.step;
import com.javadevjournal.springbootbatch.model.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
public class EmployeeItemProcessor
implements ItemProcessor<Employee, String> {
private static final Logger LOGGER =
LoggerFactory.getLogger(EmployeeItemProcessor.class);
@Override
public String process(Employee employee) throws Exception {
String greeting = "Hello " + employee.getFirstName() + " "
+ employee.getLastName() + " from " + employee.getDepartment()+"!";
LOGGER.info("converting '{}' into '{}'", employee, greeting);
return greeting;
}
}

View File

@@ -0,0 +1,24 @@
package com.javadevjournal.springbootbatch.step;
import com.javadevjournal.springbootbatch.model.StockInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import java.util.Date;
public class StockInfoProcessor
implements ItemProcessor<StockInfo, String> {
private static final Logger LOGGER =
LoggerFactory.getLogger(StockInfoProcessor.class);
@Override
public String process(StockInfo stockInfo) throws Exception {
System.out.println("Hello");
String message = stockInfo.getStockName() + " is trading at "
+ stockInfo.getStockPrice() + " on " + stockInfo.getMarket()+" at "+ new Date().toString()+ "!";
LOGGER.info("printing '{}' to output file", message);
return message;
}
}

View File

@@ -1,4 +0,0 @@
Jan, Sri, FCI
Chan, Sri, Powergrid
Vij, Ver, CDA
Sav, Sri, NTPC
1 Jan Sri FCI
2 Chan Sri Powergrid
3 Vij Ver CDA
4 Sav Sri NTPC

View File

@@ -0,0 +1,3 @@
1, Infy, 780.98, 1530.11, 453.44, Mumbai, Banking, BSE
1, TCS, 780.98, 1530.11, 453.44, Mumbai, Banking, BSE
1, Wipro, 780.98, 1530.11, 453.44, Mumbai, Banking, BSE
1 1 Infy 780.98 1530.11 453.44 Mumbai Banking BSE
1 1 Infy 780.98 1530.11 453.44 Mumbai Banking BSE
2 1 TCS 780.98 1530.11 453.44 Mumbai Banking BSE
3 1 Wipro 780.98 1530.11 453.44 Mumbai Banking BSE