SpringBatch步骤没有被调用

时间:2018-05-08 10:28:30

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

我正在使用springbatch根据用户输入(文件)进行批量更新。但是由于哪个作家没有被调用,因此Step不会被调用。也没有得到任何错误或异常,但没有下面的工作成功完成是我的代码。

使用的技术:mysql,spring-batch,java,spring-boot,intellij idea

@Configuration
public class ExcelFileToDatabaseJobConfig 
{

@Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    @StepScope
    ItemReader<StudentDTO> excelStudentReader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) throws Exception{
        PoiItemReader<StudentDTO> reader = new PoiItemReader<>();

        try {
           System.out.println("inside excelStudentReader");
           reader.setLinesToSkip(1);
           System.out.println("pathToFile = " + pathToFile);
           reader.setResource(new ClassPathResource(pathToFile));
           //reader.setResource(new UrlResource("file:///D:/joannes/ee.xlsx"));
           reader.setRowMapper(excelRowMapper());
       }catch (Exception ex)
        {
            ex.printStackTrace();
        }
        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)throws Exception {
        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 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)throws Exception     {
        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,@Qualifier("excelStudentReader") ItemReader<StudentDTO> importReader)throws Exception {
        System.out.println("inside step1");
        return stepBuilderFactory.get("step1")
                .<StudentDTO, StudentDTO> chunk(3000)
                .reader(importReader)
                //.processor(processor())
                .writer(writer)
                .build();
    }

}

控制器类:

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

    @Autowired
    private Job importUserJob;


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

       MultipartFile multipartFile = request.getFile("file");
       /* Long size = multipartFile.getSize();
        String contentType = multipartFile.getContentType();
        InputStream stream = multipartFile.getInputStream();
        byte[] bytes = IOUtils.toByteArray(stream);
        FileUtils.writeByteArrayToFile(new File("D:/joannes/wow.xlsx"), bytes);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("fileoriginalsize", size);
        map.put("contenttype", contentType);
        map.put("base64", new String(Base64Utils.encode(bytes)));
        */

        String path = new ClassPathResource("/").getURL().getPath();//it's assumed you have a folder called tmpuploads in the resources folder
        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 map;
        return "something";
    }
}

0 个答案:

没有答案