Skip to content

Commit 210c0d0

Browse files
committed
batch test
1 parent 39f165e commit 210c0d0

File tree

9 files changed

+166
-15
lines changed

9 files changed

+166
-15
lines changed

springboot-batch-demo/lombok.config

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lombok.log.fieldname=logger

springboot-batch-demo/pom.xml

+12-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<groupId>org.springframework.boot</groupId>
3030
<artifactId>spring-boot-starter-batch</artifactId>
3131
</dependency>
32+
3233
<dependency>
3334
<groupId>org.springframework.boot</groupId>
3435
<artifactId>spring-boot-starter-web</artifactId>
@@ -39,21 +40,29 @@
3940
<artifactId>spring-boot-devtools</artifactId>
4041
<scope>runtime</scope>
4142
</dependency>
43+
4244
<dependency>
43-
<groupId>org.hsqldb</groupId>
44-
<artifactId>hsqldb</artifactId>
45-
<scope>runtime</scope>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-data-jpa</artifactId>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.postgresql</groupId>
51+
<artifactId>postgresql</artifactId>
4652
</dependency>
53+
4754
<dependency>
4855
<groupId>org.projectlombok</groupId>
4956
<artifactId>lombok</artifactId>
5057
<optional>true</optional>
5158
</dependency>
59+
5260
<dependency>
5361
<groupId>org.springframework.boot</groupId>
5462
<artifactId>spring-boot-starter-test</artifactId>
5563
<scope>test</scope>
5664
</dependency>
65+
5766
<dependency>
5867
<groupId>org.springframework.batch</groupId>
5968
<artifactId>spring-batch-test</artifactId>

springboot-batch-demo/src/main/java/org/batch/config/BatchConfiguration.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.batch.config;
22

3+
import java.util.List;
4+
import java.util.StringTokenizer;
5+
import java.util.UUID;
36
import javax.sql.DataSource;
47
import org.batch.domain.Person;
58
import org.batch.process.JobCompletionNotificationListener;
@@ -10,6 +13,11 @@
1013
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
1114
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
1215
import org.springframework.batch.core.launch.support.RunIdIncrementer;
16+
import org.springframework.batch.item.ItemReader;
17+
import org.springframework.batch.item.ItemWriter;
18+
import org.springframework.batch.item.NonTransientResourceException;
19+
import org.springframework.batch.item.ParseException;
20+
import org.springframework.batch.item.UnexpectedInputException;
1321
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
1422
import org.springframework.batch.item.database.JdbcBatchItemWriter;
1523
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
@@ -19,14 +27,15 @@
1927
import org.springframework.beans.factory.annotation.Autowired;
2028
import org.springframework.context.annotation.Bean;
2129
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Profile;
2231
import org.springframework.core.io.ClassPathResource;
2332

2433
/**
2534
* @author zacconding
2635
* @Date 2018-03-25
2736
* @GitHub : https://github.com/zacscoding
2837
*/
29-
38+
@Profile("csv")
3039
@Configuration
3140
@EnableBatchProcessing
3241
public class BatchConfiguration {
@@ -40,8 +49,9 @@ public class BatchConfiguration {
4049
// == tag :: reader writer process []
4150
@Bean
4251
public FlatFileItemReader<Person> reader() {
43-
return new FlatFileItemReaderBuilder<Person>().name("personItemReader").resource(new ClassPathResource("sample-data.csv")).delimited()
44-
.names(new String[] {"firstName", "lastName"}).fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
52+
return new FlatFileItemReaderBuilder<Person>().name("personItemReader")
53+
.resource(new ClassPathResource("sample-data.csv")).delimited()
54+
.names(new String[]{"firstName", "lastName"}).fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
4555
setTargetType(Person.class);
4656
}}).build();
4757
}
@@ -53,22 +63,25 @@ public PersonItemProcessor processor() {
5363

5464
@Bean
5565
public JdbcBatchItemWriter<Person> writer(DataSource ds) {
56-
return new JdbcBatchItemWriterBuilder<Person>().itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
57-
.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)").dataSource(ds).build();
66+
return new JdbcBatchItemWriterBuilder<Person>()
67+
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
68+
.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)").dataSource(ds).build();
5869
}
5970
// == end :: reader writer process []
6071

6172
// == tag :: jobstep[]
6273
@Bean
6374
public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
64-
return jobBuilderFactory.get("importUserJob").incrementer(new RunIdIncrementer()).listener(listener).flow(step1).end().build();
75+
return jobBuilderFactory.get("importUserJob").incrementer(new RunIdIncrementer()).listener(listener).flow(step1)
76+
.end().build();
6577
}
6678

6779
@Bean
6880
public Step step1(JdbcBatchItemWriter<Person> writer) {
69-
return stepBuilderFactory.get("step1").<Person, Person>chunk(2).reader(reader()).processor(processor()).writer(writer).build();
81+
return stepBuilderFactory.get("step1").<Person, Person>chunk(2)
82+
.reader(reader())
83+
.processor(processor())
84+
.writer(writer).build();
7085
}
7186
// == end :: job step[]
72-
73-
7487
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.batch.config;
2+
3+
import java.util.List;
4+
import java.util.StringTokenizer;
5+
import java.util.UUID;
6+
import java.util.concurrent.atomic.AtomicLong;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.batch.domain.Person;
9+
import org.batch.process.JobCompletionNotificationListener;
10+
import org.batch.process.PersonItemProcessor;
11+
import org.springframework.batch.core.Job;
12+
import org.springframework.batch.core.Step;
13+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
14+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
15+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
16+
import org.springframework.batch.core.launch.support.RunIdIncrementer;
17+
import org.springframework.batch.item.ItemReader;
18+
import org.springframework.batch.item.ItemWriter;
19+
import org.springframework.batch.item.NonTransientResourceException;
20+
import org.springframework.batch.item.ParseException;
21+
import org.springframework.batch.item.UnexpectedInputException;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.Profile;
26+
27+
/**
28+
* @GitHub : https://github.com/zacscoding
29+
*/
30+
@Slf4j
31+
@Profile("console")
32+
@Configuration
33+
@EnableBatchProcessing
34+
public class BatchSimpleConfiguration {
35+
36+
final AtomicLong readCounter = new AtomicLong(0);
37+
38+
@Autowired
39+
public JobBuilderFactory jobBuilderFactory;
40+
41+
@Autowired
42+
public StepBuilderFactory stepBuilderFactory;
43+
44+
@Bean
45+
public ItemReader<Person> randomPersonReader() {
46+
return new ItemReader<Person>() {
47+
@Override
48+
public Person read()
49+
throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
50+
51+
Person result = null;
52+
long readCount = readCounter.getAndIncrement();
53+
if (readCount % 2 == 0) {
54+
result = null;
55+
} else {
56+
String uuid = UUID.randomUUID().toString();
57+
StringTokenizer tokenizer = new StringTokenizer(uuid, "-");
58+
return new Person(tokenizer.nextToken(), tokenizer.nextToken());
59+
}
60+
61+
logger.info("[READER] read person : {}", result);
62+
return result;
63+
}
64+
};
65+
}
66+
67+
@Bean
68+
public ItemWriter<Person> consoleOutputWriter() {
69+
return new ItemWriter<Person>() {
70+
@Override
71+
public void write(List<? extends Person> list) throws Exception {
72+
System.out.println("[WRITER] persons : " + list.size());
73+
for (Person person : list) {
74+
System.out.println("[WRITER] : " + person.toString());
75+
}
76+
}
77+
};
78+
}
79+
80+
@Bean
81+
public PersonItemProcessor processor() {
82+
return new PersonItemProcessor();
83+
}
84+
85+
// == tag :: jobstep[]
86+
@Bean
87+
public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
88+
return jobBuilderFactory.get("importUserJob").incrementer(new RunIdIncrementer()).listener(listener)
89+
.flow(step1)
90+
.end().build();
91+
}
92+
93+
@Bean
94+
public Step step1() {
95+
return stepBuilderFactory.get("step1").<Person, Person>chunk(1)
96+
.reader(randomPersonReader())
97+
.processor(processor())
98+
//.writer(consoleOutputWriter())
99+
.build();
100+
}
101+
// == end :: job step[]
102+
103+
104+
}

springboot-batch-demo/src/main/java/org/batch/domain/Person.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.batch.domain;
22

3+
import javax.persistence.Entity;
4+
import javax.persistence.Table;
35
import lombok.AllArgsConstructor;
46
import lombok.Getter;
57
import lombok.NoArgsConstructor;

springboot-batch-demo/src/main/java/org/batch/process/JobCompletionNotificationListener.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public void afterJob(JobExecution jobExecution) {
3232
logger.info("@@ after job is called.. status : {}", jobExecution.getStatus());
3333
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
3434
logger.info("@@ Job finished ! Time to verity the results");
35-
jdbcTemplate.query("SELECT first_name, last_name FROM people", (rs, row) -> new Person(rs.getString(1), rs.getString(2)))
36-
.forEach(person -> logger.info("Found <{}> in the database", person));
35+
/*jdbcTemplate.query("SELECT first_name, last_name FROM people", (rs, row) -> new Person(rs.getString(1), rs.getString(2)))
36+
.forEach(person -> logger.info("Found <{}> in the database", person));*/
3737
}
3838
}
3939

springboot-batch-demo/src/main/java/org/batch/scheulder/JobLauncherScheduler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class JobLauncherScheduler {
2828
@Autowired
2929
private JobLauncher jobLauncher;
3030

31-
@Scheduled(fixedRate = 3000L)
31+
@Scheduled(fixedRate = 15000L)
3232
public void importUserScheduleTask() {
3333
try {
3434
jobLauncher.run(job, newExecution());
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1+
# ["csv","console"]
2+
spring.profiles.active=console
3+
14
spring.devtools.livereload.enabled=true
5+
6+
# Datasource
7+
spring.datasource.username=postgres
8+
spring.datasource.password=pass
9+
spring.datasource.url=jdbc:postgresql://192.168.79.130:5432/postgres
10+
spring.datasource.driver-class-name=org.postgresql.Driver
11+
12+
# Hibernate
13+
spring.jpa.hibernate.ddl-auto=create-drop
14+
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
15+
spring.jpa.properties.hibernate.format_sql=true
16+
17+
logging.level.org.hibernate.SQL=DEBUG
18+
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
19+
220
logging.level.org.batch=debug
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE PERSON (
2+
first_name varchar ,
3+
second_name varchar
4+
);

0 commit comments

Comments
 (0)