Skip to content

Commit 06a705e

Browse files
committed
Remove CountDownLatch to simplify
Resolves #8
1 parent 427f2c9 commit 06a705e

File tree

5 files changed

+46
-45
lines changed

5 files changed

+46
-45
lines changed

README.adoc

+10
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,20 @@ The first method defines the job and the second one defines a single step. Jobs
120120

121121
In this job definition, you need an incrementer because jobs use a database to maintain execution state. You then list each step, of which this job has only one step. The job ends, and the Java API produces a perfectly configured job.
122122

123+
The `listener()` method lets you hook into the engine and detect when the job is complete, triggering the verification of results.
124+
123125
In the step definition, you define how much data to write at a time. In this case, it writes up to ten records at a time. Next, you configure the reader, processor, and writer using the injected bits from earlier.
124126

125127
NOTE: chunk() is prefixed `<Person,Person>` because it's a generic method. This represents the input and output types of each "chunk" of processing, and lines up with `ItemReader<Person>` and `ItemWriter<Person>`.
126128

129+
`src/main/java/hello/JobCompletionNotificationListener.java`
130+
[source,java]
131+
----
132+
include::/complete/src/main/java/hello/JobCompletionNotificationListener.java[]
133+
----
134+
135+
This code listens for when a job is `BatchStatus.COMPLETED`, and then uses `JdbcTemplate` to inspect the results.
136+
127137

128138
== Make the application executable
129139

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
package hello;
22

3-
import java.sql.ResultSet;
4-
import java.sql.SQLException;
5-
import java.util.List;
6-
import java.util.concurrent.CountDownLatch;
7-
83
import org.springframework.boot.SpringApplication;
94
import org.springframework.boot.autoconfigure.SpringBootApplication;
10-
import org.springframework.context.ApplicationContext;
11-
import org.springframework.jdbc.core.JdbcTemplate;
12-
import org.springframework.jdbc.core.RowMapper;
135

146
@SpringBootApplication
157
public class Application {
168

179
public static void main(String[] args) throws Exception {
18-
ApplicationContext ctx = SpringApplication.run(Application.class, args);
19-
20-
CountDownLatch countDownLatch = ctx.getBean(CountDownLatch.class);
21-
22-
countDownLatch.await();
23-
24-
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
25-
@Override
26-
public Person mapRow(ResultSet rs, int row) throws SQLException {
27-
return new Person(rs.getString(1), rs.getString(2));
28-
}
29-
});
30-
31-
for (Person person : results) {
32-
System.out.println("Found <" + person + "> in the database.");
33-
}
34-
10+
SpringApplication.run(Application.class, args);
3511
}
3612
}

complete/src/main/java/hello/BatchConfiguration.java

-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package hello;
22

3-
import java.util.concurrent.CountDownLatch;
4-
53
import javax.sql.DataSource;
64

75
import org.springframework.batch.core.Job;
@@ -29,16 +27,6 @@
2927
@EnableBatchProcessing
3028
public class BatchConfiguration {
3129

32-
@Bean
33-
public CountDownLatch countDownLatch() {
34-
return new CountDownLatch(1);
35-
}
36-
37-
@Bean
38-
public JobExecutionListener jobExecutionListener(CountDownLatch countDownLatch) {
39-
return new JobCompletionNotificationListener(countDownLatch);
40-
}
41-
4230
// tag::readerwriterprocessor[]
4331
@Bean
4432
public ItemReader<Person> reader() {

complete/src/main/java/hello/JobCompletionNotificationListener.java

+29-7
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,52 @@
1515
*/
1616
package hello;
1717

18-
import java.util.concurrent.CountDownLatch;
18+
import java.sql.ResultSet;
19+
import java.sql.SQLException;
20+
import java.util.List;
21+
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
1924

2025
import org.springframework.batch.core.BatchStatus;
2126
import org.springframework.batch.core.JobExecution;
2227
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
23-
import org.springframework.util.Assert;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.jdbc.core.JdbcTemplate;
30+
import org.springframework.jdbc.core.RowMapper;
31+
import org.springframework.stereotype.Component;
2432

2533
/**
2634
* @author Michael Minella
2735
*/
36+
@Component
2837
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
2938

30-
private CountDownLatch countDownLatch;
39+
private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class);
3140

32-
public JobCompletionNotificationListener(CountDownLatch countDownLatch) {
33-
Assert.notNull(countDownLatch);
41+
private final JdbcTemplate jdbcTemplate;
3442

35-
this.countDownLatch = countDownLatch;
43+
@Autowired
44+
public JobCompletionNotificationListener(JdbcTemplate jdbcTemplate) {
45+
this.jdbcTemplate = jdbcTemplate;
3646
}
3747

3848
@Override
3949
public void afterJob(JobExecution jobExecution) {
4050
if(jobExecution.getStatus() == BatchStatus.COMPLETED) {
41-
countDownLatch.countDown();
51+
log.info("!!! JOB FINISHED! Time to verify the results");
52+
53+
List<Person> results = jdbcTemplate.query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
54+
@Override
55+
public Person mapRow(ResultSet rs, int row) throws SQLException {
56+
return new Person(rs.getString(1), rs.getString(2));
57+
}
58+
});
59+
60+
for (Person person : results) {
61+
log.info("Found <" + person + "> in the database.");
62+
}
63+
4264
}
4365
}
4466
}

complete/src/main/java/hello/PersonItemProcessor.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package hello;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import org.springframework.batch.item.ItemProcessor;
47

58
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
69

10+
private static final Logger log = LoggerFactory.getLogger(PersonItemProcessor.class);
11+
712
@Override
813
public Person process(final Person person) throws Exception {
914
final String firstName = person.getFirstName().toUpperCase();
1015
final String lastName = person.getLastName().toUpperCase();
1116

1217
final Person transformedPerson = new Person(firstName, lastName);
1318

14-
System.out.println("Converting (" + person + ") into (" + transformedPerson + ")");
19+
log.info("Converting (" + person + ") into (" + transformedPerson + ")");
1520

1621
return transformedPerson;
1722
}

0 commit comments

Comments
 (0)