春季批Jobparamaters与文件名

时间:2019-03-11 15:41:22

标签: java spring-boot spring-batch

我想使用spring batch rest控制器和动态输入文件名创建项目。

我的代码:休息控制器

@RestController

公共类FileNameController {

@Autowired
JobLauncher jobLauncher;

@Autowired
Job job;

@RequestMapping("/launchjob")
public String handle(@RequestParam("fileName") String fileName) throws Exception {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    try {
        JobParameters jobParameters = new JobParametersBuilder()
                                                    .addString("input.file.name", fileName)
                                                    .addLong("time", System.currentTimeMillis())
                                                    .toJobParameters();
        jobLauncher.run(job, jobParameters);
    } catch (Exception e) {
        logger.info(e.getMessage());
    }

    return "Done";
}

}

作业配置:

@Configuration

@EnableBatchProcessing 公共类JobConfig {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;


 Filetasklet ft=new Filetasklet();
 Logger log = LoggerFactory.getLogger(this.getClass().getName());


private String pathFile = urlCoffreFort + "\\" + ft.getFileName();

// => Configuration of Job  
        @Bean
        public Job job() throws IOException {
                    return jobBuilderFactory.get("job")
                    .incrementer(new RunIdIncrementer())
                    .flow(step1())
                    .end()
                    .build();
        }

        //###### Steps          
        // => Step cecStep1
        @Bean
        public Step step1() throws IOException {
            return stepBuilderFactory.get("fileDecrypt")
                    .<Person, String>chunk(100)
                    .reader(reader1())
                    .processor(processor1FileDecrypt())
                    .writer(writer1())
                    .faultTolerant()
                    .skip(Exception.class)
                    .skipLimit(100)
                    .build();
        }
        // ####### readers 

        // => reader1()
        @Bean
        public FlatFileItemReader<Person> reader1() throws IOException{

            return new FlatFileItemReaderBuilder<CSCivique>().name("personItemReader")
                                        .resource(new ClassPathResource(pathFile))
                                        .delimited()
                                        .delimiter(";")
                                        .names(new String[] { "id",  "nomNaissance", "prenom" })
                                        .targetType(CSCivique.class)
                                        .build();
        }

        // ######Processors             

        @Bean
        public PersonItemProcessor1FileDecrypt processor1FileDecrypt() {
            return new PersonItemProcessor1FileDecrypt();
        }

        // ######Writers 

        @Bean
        public FlatFileItemWriter<String> writer1() {
            return new FlatFileItemWriterBuilder<String>().name("greetingItemWriter")
                    .resource(new FileSystemResource("sav/greetings.csv"))
                    .lineAggregator(new PassThroughLineAggregator<>()).build();
        }

}

当我写网址时: http://localhost:8080/launchjob?fileName=djecc5cpt.csv 控制台打印:

PreparedStatementCallback;错误的SQL语法[从BATCH_JOB_INSTANCE中选择SELECT JOB_INSTANCE_ID,JOB_NAME,其中JOB_NAME =?和JOB_KEY =?];嵌套的异常是org.postgresql.util.PSQLException:错误:该关系“ batch_job_instance”不存在 位置:39

1 个答案:

答案 0 :(得分:0)

  

我没有请求,通常框架会创建其表

Spring Batch不会决定在您的生产数据库中代表您创建表。 需要做出决定并手动进行。否则,如果您使用Spring Boot,则可以通过设置spring.batch.initialize-schema=always来告诉Spring Boot为您执行此操作。

有关类似的问题/答案,请参见https://stackoverflow.com/a/51891852/5019386