Skip to content

Commit

Permalink
104 added Spring Boot Batch example
Browse files Browse the repository at this point in the history
  • Loading branch information
iuliana committed Jul 30, 2017
1 parent 52a7caa commit b2707f5
Show file tree
Hide file tree
Showing 24 changed files with 307 additions and 13 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ ext {
starterJms : "org.springframework.boot:spring-boot-starter-artemis:$bootVersion",
starterRabbitmq : "org.springframework.boot:spring-boot-starter-amqp:$bootVersion",
starterThyme : "org.springframework.boot:spring-boot-starter-thymeleaf:$bootVersion",
starterSecurity : "org.springframework.boot:spring-boot-starter-security:$bootVersion"
starterSecurity : "org.springframework.boot:spring-boot-starter-security:$bootVersion",
starterBatch : "org.springframework.boot:spring-boot-starter-batch:$bootVersion"

]

Expand Down Expand Up @@ -141,6 +142,7 @@ ext {
poi : "org.apache.poi:poi:$poiVersion",
io : "commons-io:commons-io:2.5",
]

web = [
tiles : "org.apache.tiles:tiles-jsp:$tilesVersion",
jstl : "jstl:jstl:1.2",
Expand Down
2 changes: 1 addition & 1 deletion chapter04/boot-simple/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter04/boot-web/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter05/aspectj-boot/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter06/spring-boot-jdbc/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter08/boot-jpa/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter09/boot-jta/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter12/boot-amqp/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter12/boot-jms/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter12/boot-rest/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter15/boot-jmx/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
2 changes: 1 addition & 1 deletion chapter16/singer-webapp-boot/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M1)
:: Spring Boot :: (v.2.0.0.M3)
22 changes: 22 additions & 0 deletions chapter18/batch-boot/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/libs-milestone" }
}

dependencies {
classpath boot.springBootPlugin
}
}

apply plugin: 'org.springframework.boot'

dependencies {
compile boot.starterBatch, db.hsqldb
testCompile testing.junit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.apress.prospring5.ch18;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
* Created by iuliana.cosmina on 7/30/17.
*/
@SpringBootApplication
public class Application {

private static Logger logger = LoggerFactory.getLogger(Application.class);

public static void main(String... args) throws Exception {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
assert (ctx != null);
logger.info("Application started...");

System.in.read();
ctx.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.apress.prospring5.ch18;

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.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.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;

import javax.sql.DataSource;

/**
* Created by iuliana.cosmina on 7/30/17.
*/
@Configuration
@EnableBatchProcessing
public class BatchConfig {

@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;

@Autowired DataSource dataSource;

@Autowired SingerItemProcessor itemProcessor;

@Bean
public Job job(JobExecutionStatsListener listener) {
return jobs.get("singerJob").listener(listener) .flow(step1())
.end()
.build();
}

@Bean
protected Step step1() {
return steps.get("step1")
.<Singer, Singer>chunk(10)
.reader(itemReader())
.processor(itemProcessor)
.writer(itemWriter())
.build();
}

@Bean
public ItemReader itemReader() {
FlatFileItemReader<Singer> itemReader = new FlatFileItemReader<>();
itemReader.setResource(new ClassPathResource("support/test-data.csv"));
itemReader.setLineMapper(new DefaultLineMapper<Singer>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "firstName", "lastName", "song" });
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Singer>() {{
setTargetType(Singer.class);
}});
}});
return itemReader;
}

@Bean
public ItemWriter itemWriter() {
JdbcBatchItemWriter<Singer> itemWriter = new JdbcBatchItemWriter<>();
itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
itemWriter.setSql("INSERT INTO SINGER (first_name, last_name, song) VALUES (:firstName, :lastName, :song)");
itemWriter.setDataSource(dataSource);
return itemWriter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.apress.prospring5.ch18;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Component
public class JobExecutionStatsListener extends JobExecutionListenerSupport {

public static Logger logger = LoggerFactory.getLogger(JobExecutionStatsListener.class);

private final JdbcTemplate jdbcTemplate;

@Autowired
public JobExecutionStatsListener(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public void afterJob(JobExecution jobExecution) {
if(jobExecution.getStatus() == BatchStatus.COMPLETED) {
logger.info(" --> Singers were saved to the database. Printing results ...");
jdbcTemplate.query("SELECT first_name, last_name, song FROM SINGER",
(rs, row) -> new Singer(rs.getString(1),
rs.getString(2), rs.getString(3))).forEach(
singer -> logger.info(singer.toString())
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.apress.prospring5.ch18;


public class Singer {
private String firstName;
private String lastName;
//best song :D
private String song;

public Singer() {
}

public Singer(String firstName, String lastName, String song) {
this.firstName = firstName;
this.lastName = lastName;
this.song = song;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {
return firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLastName() {
return lastName;
}

public String getSong() {
return song;
}

public void setSong(String song) {
this.song = song;
}

@Override
public String toString() {
return "firstName: " + firstName + ", lastName: " + lastName + ", song: " + song;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.apress.prospring5.ch18;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

@Component("itemProcessor")
public class SingerItemProcessor implements ItemProcessor<Singer, Singer> {
private static Logger logger = LoggerFactory.getLogger(SingerItemProcessor.class);

@Override
public Singer process(Singer singer) throws Exception {
String firstName = singer.getFirstName().toUpperCase();
String lastName = singer.getLastName().toUpperCase();
String song = singer.getSong().toUpperCase();

Singer transformedSinger = new Singer();
transformedSinger.setFirstName(firstName);
transformedSinger.setLastName(lastName);
transformedSinger.setSong(song);

logger.info("Transformed singer: " + singer + " Into: " + transformedSinger);

return transformedSinger;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.datasource.schema=classpath:support/*.sql


7 changes: 7 additions & 0 deletions chapter18/batch-boot/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_____
/ _ \ _____________ ____ ______ ______
/ /_\ \\____ \_ __ \_/ __ \ / ___// ___/
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.2.0.0.M3)
21 changes: 21 additions & 0 deletions chapter18/batch-boot/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>

<logger name="com.apress.prospring5.ch18" level="debug"/>

<logger name="org.springframework" level="info"/>

<root level="info">
<appender-ref ref="console" />
</root>
</configuration>
8 changes: 8 additions & 0 deletions chapter18/batch-boot/src/main/resources/support/singer.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP TABLE SINGER IF EXISTS;

CREATE TABLE SINGER (
singer_id BIGINT IDENTITY NOT NULL PRIMARY KEY,
first_name VARCHAR(20),
last_name VARCHAR(20),
song VARCHAR(100)
);
4 changes: 4 additions & 0 deletions chapter18/batch-boot/src/main/resources/support/test-data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
John,Mayer,Helpless
Eric,Clapton,Change The World
John,Butler,Ocean
BB,King,Chains And Things
Loading

0 comments on commit b2707f5

Please sign in to comment.