使用postgres进行Spring批量配置

时间:2018-01-26 08:57:03

标签: postgresql spring-batch

我正在尝试使用PosgreSQL作为数据库的普通Spring批处理应用程序。

的pom.xml

        <!-- postgresql driver -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1201-jdbc41</version>
    </dependency>

配置文件:

@Configuration
@EnableBatchProcessing
public class batchConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
public DataSource dataSource;

@Bean(name="dataSource")
public DriverManagerDataSource dataSource(){
DriverManagerDataSource dataSource= new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/articles");
dataSource.setUsername("admin");
dataSource.setPassword("admin");    
return dataSource;
}

// tag::readerwriterprocessor[]
@Bean
public FlatFileItemReader<Personne> reader() {
    FlatFileItemReader<Personne> reader = new FlatFileItemReader<Personne>();
    reader.setResource(new ClassPathResource("listePersonne.csv"));
    reader.setLineMapper(new DefaultLineMapper<Personne>() {{
        setLineTokenizer(new DelimitedLineTokenizer() {{
            setNames(new String[] { "prenom", "nom" });
        }});
        setFieldSetMapper(new BeanWrapperFieldSetMapper<Personne>() {{
            setTargetType(Personne.class);
        }});
    }});
    return reader;
}

@Bean
public PersonItemProcessor processor() {
    return new PersonItemProcessor();
}

@Bean
public JdbcBatchItemWriter<Personne> writer() {
    JdbcBatchItemWriter<Personne> writer = new JdbcBatchItemWriter<Personne>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Personne>());
    writer.setSql("INSERT INTO personne (prenom, nom) VALUES (:prenom, :nom)");
    writer.setDataSource(dataSource);
    return writer;
}
// end::readerwriterprocessor[]

@Bean
public Step step1() {
    return stepBuilderFactory.get("step1")
            .<Personne, Personne> chunk(1)
            .reader(reader())
            .processor(processor())
            .writer(writer())
            .build();
}
// end::jobstep[]

@Bean
public Job importUserJob(JobCompletionNotificationListener listener) {
    return jobBuilderFactory.get("importUserJob")
            .incrementer(new RunIdIncrementer())
            .listener(listener)
            .flow(step1())
            .end()
            .build();
}

}

当我启动应用程序并没有破坏批处理而是我在日志文件中得到这个:

[INFO] 2018-01-26 09:04:00,658 org.springframework.batch.core.repository.support.JobRepositoryFactoryBean afterPropertiesSet - 没有数据库类型集,使用元数据指示:POSTGRES

[INFO] 2018-01-26 09:04:00,687 org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet - 未设置TaskExecutor,默认为同步执行程序

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

因为您使用的是Spring MVC,我假设您希望通过HTTP请求(例如curl http://localhost:8080/jobLauncher.html)启动批处理,您可以使用 您的控制器类中的jobLauncher可以启动您的工作。

以下是4.5.2 Running Jobs from within a Web Container of the Spring Batch Reference Guide.

的示例
@Controller
public class JobLauncherController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @RequestMapping("/jobLauncher.html")
    public void handle() throws Exception{
        jobLauncher.run(job, new JobParameters());
    }
}