mrmadou
mrmadou

Reputation: 101

Spring batch configuration with postgres

I am trying a plain Spring batch app using PosgreSQL as the database.

pom.xml

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

the config file:

@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();
}

}

When I start the app didnt runthe batch and instead i get this in the log file :

[INFO] 2018-01-26 09:04:00,658 org.springframework.batch.core.repository.support.JobRepositoryFactoryBean afterPropertiesSet - No database type set, using meta data indicating: POSTGRES

[INFO] 2018-01-26 09:04:00,687 org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet - No TaskExecutor has been set, defaulting to synchronous executor

how can I fix this please ?

Upvotes: 1

Views: 7090

Answers (1)

chao_chang
chao_chang

Reputation: 778

Because you are using Spring MVC,I assume you want to let your batch launched by an HTTP request(say curl http://localhost:8080/jobLauncher.html),you can use a jobLauncher in your controller class to launch your job.

Below is an example from 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());
    }
}

Upvotes: 0

Related Questions