forked from Apress/pro-spring-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
307 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
25 changes: 25 additions & 0 deletions
25
chapter18/batch-boot/src/main/java/com/apress/prospring5/ch18/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
chapter18/batch-boot/src/main/java/com/apress/prospring5/ch18/BatchConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
chapter18/batch-boot/src/main/java/com/apress/prospring5/ch18/JobExecutionStatsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
chapter18/batch-boot/src/main/java/com/apress/prospring5/ch18/Singer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
chapter18/batch-boot/src/main/java/com/apress/prospring5/ch18/SingerItemProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
chapter18/batch-boot/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spring.datasource.schema=classpath:support/*.sql | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
_____ | ||
/ _ \ _____________ ____ ______ ______ | ||
/ /_\ \\____ \_ __ \_/ __ \ / ___// ___/ | ||
/ | \ |_> > | \/\ ___/ \___ \ \___ \ | ||
\____|__ / __/|__| \___ >____ >____ > | ||
========\/|__|===============\/=====\/=====\/ | ||
:: Spring Boot :: (v.2.0.0.M3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
4
chapter18/batch-boot/src/main/resources/support/test-data.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.