Bootstrappify initial implementation
This commit is contained in:
parent
ee95d7cbbb
commit
851d331b2d
|
@ -16,39 +16,16 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.bootstrap</groupId>
|
<groupId>org.springframework.bootstrap</groupId>
|
||||||
<artifactId>spring-bootstrap-starter</artifactId>
|
<artifactId>spring-bootstrap-batch-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>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hsqldb</groupId>
|
<groupId>org.hsqldb</groupId>
|
||||||
<artifactId>hsqldb</artifactId>
|
<artifactId>hsqldb</artifactId>
|
||||||
<version>2.2.9</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<start-class>hello.Application</start-class>
|
<start-class>hello.BatchConfiguration</start-class>
|
||||||
<spring.framework.version>4.0.0.BUILD-SNAPSHOT</spring.framework.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -17,45 +17,48 @@ import org.springframework.batch.item.file.FlatFileItemReader;
|
||||||
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
|
||||||
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
|
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.context.annotation.EnableAutoConfiguration;
|
||||||
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.datasource.embedded.EmbeddedDatabaseBuilder;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableBatchProcessing
|
@EnableBatchProcessing
|
||||||
|
@EnableAutoConfiguration
|
||||||
public class BatchConfiguration {
|
public class BatchConfiguration {
|
||||||
|
|
||||||
@Bean
|
public static void main(String[] args) {
|
||||||
public DataSource dataSource() {
|
SpringApplication.run(BatchConfiguration.class, args);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
||||||
return new JdbcTemplate(dataSource);
|
return new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
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(){{
|
{
|
||||||
setNames(new String[]{"firstName", "lastName"});
|
setLineTokenizer(new DelimitedLineTokenizer() {
|
||||||
}});
|
{
|
||||||
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>(){{
|
setNames(new String[] { "firstName", "lastName" });
|
||||||
setTargetType(Person.class);
|
}
|
||||||
}});
|
});
|
||||||
}});
|
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
|
||||||
|
{
|
||||||
|
setTargetType(Person.class);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemProcessor<Person, Person> processor() {
|
public ItemProcessor<Person, Person> processor() {
|
||||||
return new PersonItemProcessor();
|
return new PersonItemProcessor();
|
||||||
|
@ -69,26 +72,19 @@ public class BatchConfiguration {
|
||||||
writer.setDataSource(dataSource);
|
writer.setDataSource(dataSource);
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
public Job importUserJob(JobBuilderFactory jobs, Step s1) {
|
||||||
Job job = jobs.get("importUserJob")
|
Job job = jobs.get("importUserJob").incrementer(new RunIdIncrementer()).flow(s1)
|
||||||
.incrementer(new RunIdIncrementer())
|
.end().build();
|
||||||
.flow(s1)
|
|
||||||
.end()
|
|
||||||
.build();
|
|
||||||
return job;
|
return job;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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")
|
return stepBuilderFactory.get("step1").<Person, Person> chunk(10).reader(reader)
|
||||||
.<Person,Person> chunk(10)
|
.processor(processor).writer(writer).build();
|
||||||
.reader(reader)
|
|
||||||
.processor(processor)
|
|
||||||
.writer(writer)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
|
@ -1,6 +1,5 @@
|
||||||
handlers = java.util.logging.ConsoleHandler
|
handlers = java.util.logging.ConsoleHandler
|
||||||
.level = ALL
|
.level = ALL
|
||||||
.formatter = java.util.logging.SimpleFormatter
|
|
||||||
org.springframework.batch.level = FINE
|
org.springframework.batch.level = FINE
|
||||||
org.springframework.integration.level = FINE
|
org.springframework.integration.level = FINE
|
||||||
org.hsqldb.level = FINE
|
org.hsqldb.level = FINE
|
Loading…
Reference in New Issue