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>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.springsource.org/milestone</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>

View File

@ -1,5 +1,9 @@
package hello;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
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.bootstrap.SpringApplication;
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
@Configuration
@EnableBatchProcessing
@ -30,7 +36,16 @@ import org.springframework.jdbc.core.JdbcTemplate;
public class BatchConfiguration {
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
@ -42,20 +57,14 @@ public class BatchConfiguration {
public ItemReader<Person> reader() {
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
reader.setResource(new ClassPathResource("sample-data.csv"));
reader.setLineMapper(new DefaultLineMapper<Person>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(new String[] { "firstName", "lastName" });
}
});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
{
setTargetType(Person.class);
}
});
}
});
reader.setLineMapper(new DefaultLineMapper<Person>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "firstName", "lastName" });
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setTargetType(Person.class);
}});
}});
return reader;
}
@ -75,16 +84,22 @@ public class BatchConfiguration {
@Bean
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
Job job = jobs.get("importUserJob").incrementer(new RunIdIncrementer()).flow(s1)
.end().build();
return job;
return jobs.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(s1)
.end()
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Person> reader,
ItemWriter<Person> writer, ItemProcessor<Person, Person> processor) {
return stepBuilderFactory.get("step1").<Person, Person> chunk(10).reader(reader)
.processor(processor).writer(writer).build();
return stepBuilderFactory.get("step1")
.<Person, Person> chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
}

View File

@ -24,22 +24,31 @@ public class Person {
private String lastName;
private String firstName;
public void setFirstName(final String firstName) {
this.firstName = firstName;
public Person() {
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
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 lastName = person.getLastName().toUpperCase();
final Person transformedPerson = new Person();
transformedPerson.setFirstName(firstName);
transformedPerson.setLastName(lastName);
final Person transformedPerson = new Person(firstName, lastName);
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