SpringBatch step is not getting called

1.3k Views Asked by At

I am using springbatch to do a batch update based on user input(file). But Step is not getting called due to which writer is not getting called. Also not getting any error or exception but the job completed successfully without Below is my code.

Technologies used: 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 Class:

@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

There are 0 best solutions below