Bootstrappify initial implementation

This commit is contained in:
Dave Syer 2013-05-16 16:26:46 +01:00
parent ee95d7cbbb
commit 851d331b2d
6 changed files with 32 additions and 106 deletions

View File

@ -16,39 +16,16 @@
<dependencies>
<dependency>
<groupId>org.springframework.bootstrap</groupId>
<artifactId>spring-bootstrap-starter</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
<artifactId>spring-bootstrap-batch-starter</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
</dependency>
</dependencies>
<properties>
<start-class>hello.Application</start-class>
<spring.framework.version>4.0.0.BUILD-SNAPSHOT</spring.framework.version>
<start-class>hello.BatchConfiguration</start-class>
</properties>
<build>

View File

@ -1,37 +0,0 @@
package hello;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class Application {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new AnnotationConfigApplicationContext(BatchConfiguration.class);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
jobLauncher.run(ctx.getBean(Job.class), new JobParametersBuilder().toJobParameters());
JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
List<Person> results = jdbcTemplate.query("select first_name, last_name from PEOPLE", new RowMapper<Person>() {
@Override
public Person mapRow(final ResultSet rs, int rowNum) throws SQLException {
return new Person() {{
setFirstName(rs.getString(1));
setLastName(rs.getString(2));
}};
}
});
for (Person person : results) {
System.out.println("Found <" + person + "> in the database.");
}
}
}

View File

@ -17,23 +17,20 @@ 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.bootstrap.SpringApplication;
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
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.datasource.embedded.EmbeddedDatabaseBuilder;
@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class BatchConfiguration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().
addScript("classpath:org/springframework/batch/core/schema-drop-hsqldb.sql").
addScript("classpath:org/springframework/batch/core/schema-hsqldb.sql").
addScript("classpath:person.sql").
build();
public static void main(String[] args) {
SpringApplication.run(BatchConfiguration.class, args);
}
@Bean
@ -45,14 +42,20 @@ 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(){{
reader.setLineMapper(new DefaultLineMapper<Person>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(new String[] { "firstName", "lastName" });
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>(){{
}
});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
{
setTargetType(Person.class);
}});
}});
}
});
}
});
return reader;
}
@ -72,23 +75,16 @@ public class BatchConfiguration {
@Bean
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
Job job = jobs.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(s1)
.end()
.build();
Job job = jobs.get("importUserJob").incrementer(new RunIdIncrementer()).flow(s1)
.end().build();
return job;
}
@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

@ -1,9 +0,0 @@
log4j.rootCategory=TRACE, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p %t [%c] - <%m>%n
#log4j.org.springframework.batch=DEBUG
#log4j.org.springframework.integration=DEBUG
#log4j.org.hsqldb=DEBUG

View File

@ -1,6 +1,5 @@
handlers = java.util.logging.ConsoleHandler
.level = ALL
.formatter = java.util.logging.SimpleFormatter
org.springframework.batch.level = FINE
org.springframework.integration.level = FINE
org.hsqldb.level = FINE