springbatch excel第二次不起作用

时间:2018-05-09 10:24:56

标签: java mysql spring-boot spring-batch spring-batch-excel

我正在使用api来调用springbatch作业。当我第一次打电话它工作正常。第二次给出以下错误。

Caused by: java.lang.IllegalArgumentException: Sheet index (3) is out of range (0..2).

以下是我的代码.------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------

配置:

 @Configuration
@EnableBatchProcessing
@PropertySource("classpath:batch.properties")
public class ExcelFileToDatabaseJobConfig {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Bean
ItemReader<StudentDTO> excelStudentReader() throws Exception {
    System.out.println("inside excelStudentReader");
    PoiItemReader<StudentDTO> reader = new PoiItemReader<>();
    reader.setLinesToSkip(1);
    //reader.setResource(new FileSystemResource("C:/Users/lenovo/IdeaProjects/test/out/production/classes/Price Change.xlsx"));
    reader.setResource(new UrlResource("file:///C:/Users/lenovo/IdeaProjects/test/out/production/classes/inserttest.xlsx"));
    reader.setRowMapper(excelRowMapper());
    return reader;
}

private RowMapper<StudentDTO> excelRowMapper() {
    System.out.println("inside excelRowMapper");
    BeanWrapperRowMapper<StudentDTO> rowMapper = new BeanWrapperRowMapper<>();
    rowMapper.setTargetType(StudentDTO.class);
    return rowMapper;
}

@Bean
public JdbcBatchItemWriter<StudentDTO> writer(DataSource dataSource) {
    System.out.println("inside writer");
    return new JdbcBatchItemWriterBuilder<StudentDTO>()
            .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
            .sql("INSERT INTO tbl_item_master (mrp_rate,price,item_code) VALUES (:mrp_rate,:rate,:u_item_code)")
            //.sql("update ignore tbl_item_master set mrp_rate=:mrp_rate,price=:rate where item_code=:u_item_code")
            .dataSource(dataSource)
            .build();
}

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

@Bean
public Step step1(JdbcBatchItemWriter<StudentDTO> writer,CustomChunkListener customChunkListener,
                  CustomItemReaderListener customItemReaderListener,CustomItemWriterListener customItemWriterListener,
                  CustomStepListener customStepListener,CustomSkipListener customSkipListener) throws Exception {
    System.out.println("inside step1");
    return stepBuilderFactory.get("step1")
            .<StudentDTO, StudentDTO>chunk(3000)
            .reader(excelStudentReader())
            //.processor(processor())
            .writer(writer)
            .faultTolerant()
            .skip(Exception.class)
            .noRetry(Exception.class)
            .noRollback(Exception.class)
            .skipLimit(100)
            .listener(customItemWriterListener)
            .listener(customSkipListener)
            .build();
}

@Bean
public CustomStepListener customStepListener() {
    return new CustomStepListener();
}

@Bean
public CustomItemWriterListener itemWriterListener() {
    return new CustomItemWriterListener();
}

@Bean
public CustomItemReaderListener itemReaderListener() {
    return new CustomItemReaderListener();
}

@Bean
public CustomSkipListener customSkipListener() {
    return new CustomSkipListener();
}
}

控制器:

 @Controller
public class FileController1 {
    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job importUserJob;

    @RequestMapping(value = "/echofile", method = RequestMethod.POST, produces = {"application/json"})
      public @ResponseBody String echoFile(MultipartHttpServletRequest request,
                HttpServletResponse response) throws Exception {

       MultipartFile multipartFile = request.getFile("file");
        String path = new ClassPathResource("/").getURL().getPath();
        File fileToImport = new File(path + multipartFile.getOriginalFilename());
        //filePath = fileToImport.getAbsolutePath();
        OutputStream outputStream = new FileOutputStream(fileToImport);
        IOUtils.copy(multipartFile.getInputStream(), outputStream);
        outputStream.flush();
        outputStream.close();
        //Launch the Batch Job
        JobExecution jobExecution = jobLauncher.run(importUserJob,new JobParametersBuilder()
                .addString("fullPathFileName", fileToImport.getAbsolutePath()).addLong("time",System.currentTimeMillis()).toJobParameters());
        return "something";
    }
}

batch.properties:

spring.batch.job.enabled=false

0 个答案:

没有答案