diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/SpringBootBatchBasicApplication.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/SpringBootBatchBasicApplication.java index 5035f6e..b3b2099 100644 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/SpringBootBatchBasicApplication.java +++ b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/SpringBootBatchBasicApplication.java @@ -3,12 +3,14 @@ package com.javadevjournal.springbootbatch; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; /** * @Author - Kunwar Vikas */ @SpringBootApplication @EnableBatchProcessing +@EnableScheduling public class SpringBootBatchBasicApplication { public static void main(String[] args) { diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/config/SpringBatchConfig.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/config/SpringBatchConfig.java index c479f57..2385bfc 100644 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/config/SpringBatchConfig.java +++ b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/config/SpringBatchConfig.java @@ -1,7 +1,7 @@ package com.javadevjournal.springbootbatch.config; -import com.javadevjournal.springbootbatch.listener.SPJobExecutionListener; -import com.javadevjournal.springbootbatch.listener.SPStepListener; +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 org.springframework.batch.core.Job; @@ -20,11 +20,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.javadevjournal.springbootbatch.listener.SpringBatchJobCompletionListener; -import com.javadevjournal.springbootbatch.step.SBProcessor; -import com.javadevjournal.springbootbatch.step.SBReader; -import com.javadevjournal.springbootbatch.step.SBWriter; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; +import org.springframework.scheduling.annotation.Scheduled; /** * @Author - Kunwar Vikas @@ -40,28 +38,37 @@ public class SpringBatchConfig { @Bean public Job processJob() { - return jobBuilderFactory.get("javadevjournaljob") + return jobBuilderFactory.get("stockpricesinfojob") .incrementer(new RunIdIncrementer()) - .listener(new SPJobExecutionListener()) - .flow(orderStep1()).end().build(); + .listener(new SpringBatchJobExecutionListener()) + .flow(StockPricesInfoStep()) + .end() + .build(); } - @Bean - public Step orderStep1() { + public Step StockPricesInfoStep() { return stepBuilderFactory.get("step1") - .listener(new SPStepListener()) - .chunk(10).reader(reader()) - .processor(processor()).writer(writer()).build(); + .listener(new SpringBatchStepListener()) + .chunk(10) + .reader(reader()) + .processor(processor()) + .writer(writer()) + .faultTolerant() + .retryLimit(3) + .retry(Exception.class) + .build(); } @Bean public FlatFileItemReader reader() { return new FlatFileItemReaderBuilder() - .name("employeeItemReader") - .resource(new ClassPathResource("csv/employees.csv")) - .delimited().names(new String[] {"firstName", "lastName","department"}) - .targetType(Employee.class).build(); + .name("stockInfoItemReader") + .resource(new ClassPathResource("csv/stockinfo.csv")) + .delimited() + .names(new String[] {"stockId", "stockName","stockPrice","yearlyHigh","yearlyLow","address","sector","market"}) + .targetType(Employee.class) + .build(); } @Bean @@ -72,7 +79,7 @@ public class SpringBatchConfig { @Bean public FlatFileItemWriter writer() { return new FlatFileItemWriterBuilder() - .name("greetingItemWriter") + .name("stockInfoItemWriter") .resource(new FileSystemResource( "target/output.txt")) .lineAggregator(new PassThroughLineAggregator<>()).build(); diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/controller/SpringBatchJobController.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/controller/SpringBatchJobController.java index 06ff810..8849f20 100644 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/controller/SpringBatchJobController.java +++ b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/controller/SpringBatchJobController.java @@ -3,12 +3,19 @@ package com.javadevjournal.springbootbatch.controller; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.JobParametersInvalidException; import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; +import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; +import org.springframework.batch.core.repository.JobRestartException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Date; + /** * @Author - Kunwar Vikas */ @@ -19,13 +26,34 @@ public class SpringBatchJobController { JobLauncher jobLauncher; @Autowired - Job javadevjournaljob; - + Job stockpricesinfojob; + + /** + * Invoke the batch job on-demand using rest APIs. + * @return + * @throws Exception + */ @GetMapping("/invokejob") public String invokeBatchJob() throws Exception { JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()) .toJobParameters(); - jobLauncher.run(javadevjournaljob, jobParameters); + jobLauncher.run(stockpricesinfojob, jobParameters); return "Batch job has been invoked"; } + + /** + * Schuled batch job every minute to load the stock prices and send notification + * @return + * @throws JobParametersInvalidException + * @throws JobExecutionAlreadyRunningException + * @throws JobRestartException + * @throws JobInstanceAlreadyCompleteException + */ + @Scheduled(fixedRate = 60000) + public String schedule() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException { + JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()) + .toJobParameters(); + jobLauncher.run(stockpricesinfojob, jobParameters); + return "Batch job has been invoked"; + } } \ No newline at end of file diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPChunkListener.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPChunkListener.java deleted file mode 100644 index 89b8a95..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPChunkListener.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.javadevjournal.springbootbatch.listener; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.batch.core.ChunkListener; -import org.springframework.batch.core.StepListener; -import org.springframework.batch.core.scope.context.ChunkContext; - -/** - * @Author - Kunwar Vikas - */ -public class SPChunkListener implements ChunkListener { - - Logger logger = LoggerFactory.getLogger(SPChunkListener.class); - - @Override - public void beforeChunk(ChunkContext chunkContext) { - logger.info("beforeChunk"); - } - - @Override - public void afterChunk(ChunkContext chunkContext) { - logger.info("afterChunk"); - } - - @Override - public void afterChunkError(ChunkContext chunkContext) { - logger.info("afterChunkError"); - } -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemProcessorListener.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemProcessorListener.java deleted file mode 100644 index dcd3769..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemProcessorListener.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.javadevjournal.springbootbatch.listener; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.batch.core.ItemProcessListener; - - -/** - * @Author - Kunwar Vikas - */ -public class SPItemProcessorListener implements ItemProcessListener { - - Logger logger = LoggerFactory.getLogger(SPItemProcessorListener.class); - - @Override - public void beforeProcess(String item) { - logger.info("beforeProcess"); - } - - @Override - public void afterProcess(String item, Number result) { - logger.info("afterProcess"); - } - - @Override - public void onProcessError(String item, Exception e) { - logger.info(" onProcessError"); - } -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemReadListener.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemReadListener.java deleted file mode 100644 index 027fe55..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemReadListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.javadevjournal.springbootbatch.listener; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.batch.core.ItemReadListener; - -/** - * @Author - Kunwar Vikas - */ -public class SPItemReadListener implements ItemReadListener { - - Logger logger = LoggerFactory.getLogger(SPItemReadListener.class); - - @Override - public void beforeRead() { - logger.info("Before reading an item"); - } - - @Override - public void afterRead(String item) { - logger.info("After reading an item: "+ item.toString()); - } - - @Override - public void onReadError(Exception ex) { - logger.error("Error occurred while reading an item!"); - } -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemWriteListener.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemWriteListener.java deleted file mode 100644 index 48e46e5..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPItemWriteListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.javadevjournal.springbootbatch.listener; - -import com.javadevjournal.springbootbatch.model.User; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.batch.core.ItemWriteListener; -import org.springframework.jdbc.core.JdbcTemplate; - -import javax.sql.DataSource; -import java.util.List; - -/** - * @Author - Kunwar Vikas - */ -public class SPItemWriteListener implements ItemWriteListener { - Logger logger = LoggerFactory.getLogger(SPItemWriteListener.class); - - public void beforeWrite(List items) { - logger.info("before write"); - } - - public void onWriteError(Exception exception, List items) { - logger.info("Error occurred when writing items!"); - } - public void afterWrite(List items) { - logger.info("after write"); - } -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPSkipListener.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPSkipListener.java deleted file mode 100644 index 8548b02..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPSkipListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.javadevjournal.springbootbatch.listener; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.batch.core.SkipListener; - -/** - * @Author - Kunwar Vikas - */ -public class SPSkipListener implements SkipListener { - - Logger logger = LoggerFactory.getLogger(SPSkipListener.class); - - @Override - public void onSkipInRead(Throwable t) { - logger.info("onSkipInRead"); - } - - @Override - public void onSkipInWrite(Number item, Throwable t) { - logger.info("onSkipInWrite"); - } - - @Override - public void onSkipInProcess(String item, Throwable t) { - logger.info("onWriteError"); - } -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPJobExecutionListener.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SpringBatchJobExecutionListener.java similarity index 81% rename from Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPJobExecutionListener.java rename to Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SpringBatchJobExecutionListener.java index 8e07a88..c1e9ebc 100644 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPJobExecutionListener.java +++ b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SpringBatchJobExecutionListener.java @@ -9,9 +9,9 @@ import org.springframework.batch.core.JobExecutionListener; /** * @Author - Kunwar Vikas */ -public class SPJobExecutionListener implements JobExecutionListener { +public class SpringBatchJobExecutionListener implements JobExecutionListener { - Logger logger = LoggerFactory.getLogger(SPJobExecutionListener.class); + Logger logger = LoggerFactory.getLogger(SpringBatchJobExecutionListener.class); public void beforeJob(JobExecution jobExecution) { logger.info("BEFORE BATCH JOB STARTS"); diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPStepListener.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SpringBatchStepListener.java similarity index 77% rename from Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPStepListener.java rename to Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SpringBatchStepListener.java index 65dbbd0..d30814c 100644 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SPStepListener.java +++ b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/listener/SpringBatchStepListener.java @@ -1,6 +1,5 @@ package com.javadevjournal.springbootbatch.listener; -import com.javadevjournal.springbootbatch.step.SBWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.batch.core.ExitStatus; @@ -10,9 +9,9 @@ import org.springframework.batch.core.StepExecutionListener; /** * @Author - Kunwar Vikas */ -public class SPStepListener implements StepExecutionListener { +public class SpringBatchStepListener implements StepExecutionListener { - Logger logger = LoggerFactory.getLogger(SPStepListener.class); + Logger logger = LoggerFactory.getLogger(SpringBatchStepListener.class); @Override public void beforeStep(StepExecution stepExecution) { diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/mapper/UserRowMapper.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/mapper/UserRowMapper.java deleted file mode 100644 index 9f0296e..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/mapper/UserRowMapper.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.javadevjournal.springbootbatch.mapper; - -import com.javadevjournal.springbootbatch.model.User; -import org.springframework.jdbc.core.RowMapper; - -import java.sql.ResultSet; -import java.sql.SQLException; - -public class UserRowMapper implements RowMapper { - public User mapRow(ResultSet rs, int rowNum) throws SQLException { - User user = new User(); - user.setFirstName(rs.getString("firstName")); - user.setMiddleName(rs.getString("middleName")); - user.setLastName(rs.getString("lastName")); - user.setCity(rs.getString("city")); - user.setId(rs.getInt("id")); - return user; - } -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/model/StockInfo.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/model/StockInfo.java new file mode 100644 index 0000000..2a96354 --- /dev/null +++ b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/model/StockInfo.java @@ -0,0 +1,16 @@ +package com.javadevjournal.springbootbatch.model; + +import lombok.Data; +import java.util.List; + +@Data +public class StockInfo { + private String stockId; + private String stockName; + private double stockPrice; + private double yearlyHigh; + private double yearlyLow; + private String address; + private String sector; + private String market; +} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/model/User.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/model/User.java deleted file mode 100644 index 07dab95..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/model/User.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.javadevjournal.springbootbatch.model; - -import lombok.Data; - -/** - * Author Kunwar Vikas - */ -@Data -public class User { - private int id; - private String firstName; - private String middleName; - private String lastName; - private String city; -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBProcessor.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBProcessor.java deleted file mode 100644 index 67b087c..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBProcessor.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.javadevjournal.springbootbatch.step; - -import org.springframework.batch.item.ItemProcessor; - -/** - * @Author - Kunwar Vikas - */ -public class SBProcessor implements ItemProcessor { - - @Override - public String process(String data) throws Exception { - return data.toUpperCase(); - } - -} diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBReader.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBReader.java deleted file mode 100644 index 5fe8e45..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBReader.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.javadevjournal.springbootbatch.step; - -import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.NonTransientResourceException; -import org.springframework.batch.item.ParseException; -import org.springframework.batch.item.UnexpectedInputException; - -/** - * @Author - Kunwar Vikas - */ -public class SBReader implements ItemReader { - - private String[] messages = { "javadevjournal.com", - "Welcome to Spring Batch Example", - "We use H2 Database for this example" }; - - private int count = 0; - - @Override - public String read() throws Exception, UnexpectedInputException, - ParseException, NonTransientResourceException { - - if (count < messages.length) { - return messages[count++]; - } else { - count = 0; - } - return null; - } - -} \ No newline at end of file diff --git a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBWriter.java b/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBWriter.java deleted file mode 100644 index 402a8ab..0000000 --- a/Spring-Boot-Batch/springbootbatch/src/main/java/com/javadevjournal/springbootbatch/step/SBWriter.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.javadevjournal.springbootbatch.step; - -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.batch.item.ItemWriter; - -/** - * @Author - Kunwar Vikas - */ -public class SBWriter implements ItemWriter { - Logger logger = LoggerFactory.getLogger(SBWriter.class); - @Override - public void write(List messages) throws Exception { - for (String msg : messages) { - logger.info("Writing the data \n" + msg); - } - } - -} \ No newline at end of file diff --git a/Spring-Boot-Batch/springbootbatch/src/main/resources/application.properties b/Spring-Boot-Batch/springbootbatch/src/main/resources/application.properties index c299011..3c08f26 100644 --- a/Spring-Boot-Batch/springbootbatch/src/main/resources/application.properties +++ b/Spring-Boot-Batch/springbootbatch/src/main/resources/application.properties @@ -10,4 +10,6 @@ log4j.rootCategory=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n -log4j.category.org.springframework.beans.factory=DEBUG \ No newline at end of file +log4j.category.org.springframework.beans.factory=DEBUG + +spring.batch.job.enabled=false \ No newline at end of file diff --git a/Spring-Boot-Batch/springbootbatch/src/main/resources/csv/stockinfo.csv b/Spring-Boot-Batch/springbootbatch/src/main/resources/csv/stockinfo.csv new file mode 100644 index 0000000..e69de29