Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

Configure ItemWriter, ItemReader, Step and Job dynamically in Spring Batch

I'm working on process which uses Spring Integration and Spring Batch

1)Using Spring integration I will poll remote sftp dir to get different csv files as Message

2)Message which carries csv file as payload is sent downstream to Transformer which will transform Message to JobLaunchRequest

3)Spring batch reads csv files and dumps into DB

Question: For each csv file I need to configure (ItemReader, ItemWriter, Step, Job) So with that into consideration if I have to deal with 10 different csv files do I have to configure all 4 beans listed above for each csv? CSVs differs in HeaderNames and HeaderCount and each csv has different JPA Entity

Eventually I will have 40 @Bean Configurations which ideally I think is bad Can anyone suggest me if this is how spring batch is made to work or there is other way to make it one common dynamic bean for different CSVs

Here is code:

IntegartionFlow:

 @Bean
public IntegrationFlow integrationFlow(JobLaunchingGateway jobLaunchingGateway) {
    return IntegrationFlows.from(Sftp.inboundAdapter(sftpSessionFactory)
                    .remoteDirectory("/uploads")
                    .localDirectory(new File("C:\Users\DELL\Desktop\local"))
                    .patternFilter("*.csv")
                    .autoCreateLocalDirectory(true)
            , c -> c.poller(Pollers.fixedRate(1000).taskExecutor(taskExecutor()).maxMessagesPerPoll(1)))
            .transform(fileMessageToJobRequest())
            .handle(jobLaunchingGateway)
            .log(LoggingHandler.Level.WARN, "headers.id + ': ' + payload")
            .route(JobExecution.class, j -> j.getStatus().isUnsuccessful() ? "jobFailedChannel" : "jobSuccessfulChannel")
            .get();
}

Transformer:

 @Transformer
public JobLaunchRequest toRequest(Message<File> message) {
    JobParametersBuilder jobParametersBuilder =
            new JobParametersBuilder();

    jobParametersBuilder.addString(fileParameterName,
            message.getPayload().getAbsolutePath());
    jobParametersBuilder.addLong("key.id", System.currentTimeMillis());

    return new JobLaunchRequest(job, jobParametersBuilder.toJobParameters());
}

Batch Job:

 @Bean
public Job vendorMasterBatchJob(Step vendorMasterStep) {
    return jobBuilderFactory.get("vendorMasterBatchJob")
            .incrementer(new RunIdIncrementer())
            .start(vendorMasterStep)
            .listener(deleteInputFileJobListener)
            .build();
}

Batch Step:

  @Bean
public Step vendorMasterStep(FlatFileItemReader<ERPVendorMaster> vendorMasterReader,
                             JpaItemWriter<ERPVendorMaster> vendorMasterWriter) {
    return stepBuilderFactory.get("vendorMasterStep")
            .<ERPVendorMaster, ERPVendorMaster>chunk(chunkSize)
            .reader(vendorMasterReader)
            .writer(vendorMasterWriter)
            .faultTolerant()
            .skipLimit(Integer.MAX_VALUE)
            .skip(RuntimeException.class)
            .listener(skipListener)
            .build();
}

ItemWriter:

 @Bean
public JpaItemWriter<ERPVendorMaster> vendorMasterWriter() {
    return new JpaItemWriterBuilder<ERPVendorMaster>()
            .entityManagerFactory(entityManagerFactory)
            .build();
}

ItemReader:

 @Bean
@StepScope
public FlatFileItemReader<ERPVendorMaster> vendorMasterReader(@Value("#{jobParameters['input.file.name']}") String fileName) {
    return new FlatFileItemReaderBuilder<ERPVendorMaster>()
            .name("vendorMasterItemReader")
            .resource(new FileSystemResource(fileName))
            .linesToSkip(1)
            .delimited()
            .names(commaSeparatedVendorMasterHeaderValues.split(","))
            .fieldSetMapper(new BeanWrapperFieldSetMapper<ERPVendorMaster>() {{
                setConversionService(stringToDateConversionService());
                setTargetType(ERPVendorMaster.class);
            }})
            .build();
}

I'm very new to Spring boot any help will be appreciated

Thanks

question from:https://stackoverflow.com/questions/65626409/configure-itemwriter-itemreader-step-and-job-dynamically-in-spring-batch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...