Spring Batch Application
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.javadevjournal.springbootbatch;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableBatchProcessing
|
||||
public class SpringBootBatchBasicApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootBatchBasicApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.javadevjournal.springbootbatch.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.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.javadevjournal.springbootbatch.listener.SpringBatchJobCompletionListener;
|
||||
import com.javadevjournal.springbootbatch.step.SBProcessor;
|
||||
import com.javadevjournal.springbootbatch.step.SBReader;
|
||||
import com.javadevjournal.springbootbatch.step.SBWriter;
|
||||
|
||||
@Configuration
|
||||
public class SpringBatchConfig {
|
||||
|
||||
@Autowired
|
||||
public JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
@Autowired
|
||||
public StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job processJob() {
|
||||
return jobBuilderFactory.get("javadevjournaljob")
|
||||
.incrementer(new RunIdIncrementer()).listener(listener())
|
||||
.flow(orderStep1()).end().build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step orderStep1() {
|
||||
return stepBuilderFactory.get("step1").<String, String> chunk(1)
|
||||
.reader(new SBReader()).processor(new SBProcessor())
|
||||
.writer(new SBWriter()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JobExecutionListener listener() {
|
||||
return new SpringBatchJobCompletionListener();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
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.launch.JobLauncher;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class SpringBatchJobController {
|
||||
|
||||
@Autowired
|
||||
JobLauncher jobLauncher;
|
||||
|
||||
@Autowired
|
||||
Job javadevjournaljob;
|
||||
|
||||
@GetMapping("/invokejob")
|
||||
public String invokeBatchJob() throws Exception {
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
|
||||
.toJobParameters();
|
||||
jobLauncher.run(javadevjournaljob, jobParameters);
|
||||
return "Batch job has been invoked";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.javadevjournal.springbootbatch.listener;
|
||||
|
||||
import com.javadevjournal.springbootbatch.step.SBWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
|
||||
|
||||
public class SpringBatchJobCompletionListener extends JobExecutionListenerSupport {
|
||||
Logger logger = LoggerFactory.getLogger(SpringBatchJobCompletionListener.class);
|
||||
|
||||
@Override
|
||||
public void afterJob(JobExecution jobExecution) {
|
||||
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
|
||||
logger.info("BATCH JOB COMPLETED SUCCESSFULLY");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.javadevjournal.springbootbatch.step;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
|
||||
public class SBProcessor implements ItemProcessor<String, String> {
|
||||
|
||||
@Override
|
||||
public String process(String data) throws Exception {
|
||||
return data.toUpperCase();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
|
||||
public class SBReader implements ItemReader<String> {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.javadevjournal.springbootbatch.step;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
|
||||
public class SBWriter implements ItemWriter<String> {
|
||||
Logger logger = LoggerFactory.getLogger(SBWriter.class);
|
||||
@Override
|
||||
public void write(List<? extends String> messages) throws Exception {
|
||||
for (String msg : messages) {
|
||||
logger.info("Writing the data \n" + msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
server.port=8080
|
||||
#h2 in-memory database
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=test
|
||||
spring.datasource.password=test
|
||||
|
||||
#logging
|
||||
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
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.javadevjournal.springbootbatch;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringBootBatchBasicApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user