-2

I want to create project with spring batch rest controller and dynamic input filename.

My code : Rest Controller

@RestController

public class 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";
}

}

The Job config:

@Configuration

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

}

When I write the url : http://localhost:8080/launchjob?fileName=djecc5cpt.csv The console print :

PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME =? and JOB_KEY =?]; nested exception is org.postgresql.util.PSQLException: ERROR: the relation "batch_job_instance" does not exist    Position: 39

Monoem Youneb
  • 95
  • 1
  • 12

1 Answers1

0

I do not have a request, normally the framework will create its tables

Spring Batch won't make the decision to create tables on your behalf in your production database. You need to make that decision and do it manually upfront. Otherwise, if you use Spring Boot, you can tell Spring Boot to do it for your by setting spring.batch.initialize-schema=always.

Please see https://stackoverflow.com/a/51891852/5019386 for a similar question/answer.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50