Logging isn't enough to see the outcome. Removed logging and instead use JdbcTemplate to query DB.

This commit is contained in:
Greg Turnquist 2013-05-16 15:14:19 -04:00
parent fe3548edf1
commit ea6920389b
5 changed files with 56 additions and 33 deletions

View File

@ -43,6 +43,11 @@
<url>http://repo.springsource.org/snapshot</url> <url>http://repo.springsource.org/snapshot</url>
<snapshots><enabled>true</enabled></snapshots> <snapshots><enabled>true</enabled></snapshots>
</repository> </repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.springsource.org/milestone</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories> </repositories>
<pluginRepositories> <pluginRepositories>
<pluginRepository> <pluginRepository>

View File

@ -1,5 +1,9 @@
package hello; package hello;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Job;
@ -19,10 +23,12 @@ import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.bootstrap.SpringApplication; import org.springframework.bootstrap.SpringApplication;
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration; import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
@Configuration @Configuration
@EnableBatchProcessing @EnableBatchProcessing
@ -30,7 +36,16 @@ import org.springframework.jdbc.core.JdbcTemplate;
public class BatchConfiguration { public class BatchConfiguration {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(BatchConfiguration.class, args); ApplicationContext ctx = SpringApplication.run(BatchConfiguration.class, args);
List<Person> results = ctx.getBean(JdbcTemplate.class).query("SELECT first_name, last_name FROM people", new RowMapper<Person>() {
@Override
public Person mapRow(ResultSet rs, int row) throws SQLException {
return new Person(rs.getString(1), rs.getString(2));
}
});
for (Person person : results) {
System.out.println("Found <" + person + "> in the database.");
}
} }
@Bean @Bean
@ -42,20 +57,14 @@ public class BatchConfiguration {
public ItemReader<Person> reader() { public ItemReader<Person> reader() {
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>(); FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
reader.setResource(new ClassPathResource("sample-data.csv")); reader.setResource(new ClassPathResource("sample-data.csv"));
reader.setLineMapper(new DefaultLineMapper<Person>() { reader.setLineMapper(new DefaultLineMapper<Person>() {{
{ setLineTokenizer(new DelimitedLineTokenizer() {{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(new String[] { "firstName", "lastName" }); setNames(new String[] { "firstName", "lastName" });
} }});
}); setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
{
setTargetType(Person.class); setTargetType(Person.class);
} }});
}); }});
}
});
return reader; return reader;
} }
@ -75,16 +84,22 @@ public class BatchConfiguration {
@Bean @Bean
public Job importUserJob(JobBuilderFactory jobs, Step s1) { public Job importUserJob(JobBuilderFactory jobs, Step s1) {
Job job = jobs.get("importUserJob").incrementer(new RunIdIncrementer()).flow(s1) return jobs.get("importUserJob")
.end().build(); .incrementer(new RunIdIncrementer())
return job; .flow(s1)
.end()
.build();
} }
@Bean @Bean
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader, public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) { ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
return stepBuilderFactory.get("step1").<Person, Person> chunk(10).reader(reader) return stepBuilderFactory.get("step1")
.processor(processor).writer(writer).build(); .<Person, Person> chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
} }
} }

View File

@ -24,7 +24,16 @@ public class Person {
private String lastName; private String lastName;
private String firstName; private String firstName;
public void setFirstName(final String firstName) { public Person() {
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName; this.firstName = firstName;
} }
@ -32,14 +41,14 @@ public class Person {
return firstName; return firstName;
} }
public void setLastName(final String lastName) {
this.lastName = lastName;
}
public String getLastName() { public String getLastName() {
return lastName; return lastName;
} }
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override @Override
public String toString() { public String toString() {
return "firstName: " + firstName + ", lastName: " + lastName; return "firstName: " + firstName + ", lastName: " + lastName;

View File

@ -29,9 +29,7 @@ public class PersonItemProcessor implements ItemProcessor<Person, Person> {
final String firstName = person.getFirstName().toUpperCase(); final String firstName = person.getFirstName().toUpperCase();
final String lastName = person.getLastName().toUpperCase(); final String lastName = person.getLastName().toUpperCase();
final Person transformedPerson = new Person(); final Person transformedPerson = new Person(firstName, lastName);
transformedPerson.setFirstName(firstName);
transformedPerson.setLastName(lastName);
System.out.println("Converting (" + person + ") into (" + transformedPerson + ")"); System.out.println("Converting (" + person + ") into (" + transformedPerson + ")");

View File

@ -1,4 +0,0 @@
handlers = java.util.logging.ConsoleHandler
.level = ALL
java.util.logging.ConsoleHandler.level = FINE
org.springframework.jdbc.core.level = FINE