簡體   English   中英

Spring Batch不會在塊流中同時調用ItemProcessor和ItemWriter

[英]Spring Batch doesn't call both ItemProcessor and ItemWriter in chunk-flow

我有一個春季批處理應用程序,以在samba服務器中獲取文件並在同一服務器上的其他文件夾中生成一個新文件。 但是,流中僅調用ItemReader。 問題是什么? 謝謝。

BatchConfiguration:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends BaseConfiguration {

    @Bean
    public ValeTrocaItemReader reader() {
        return new ValeTrocaItemReader();
    }

    @Bean
    public ValeTrocaItemProcessor processor() {
        return new ValeTrocaItemProcessor();
    }

    @Bean
    public ValeTrocaItemWriter writer() {
        return new ValeTrocaItemWriter();
    }

    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener) throws Exception {
        return jobBuilderFactory()
                .get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .repository(getJobRepository())
                .listener(listener)
                .start(this.step1())
                .build();
    }

    @Bean
    public Step step1() throws Exception {
        return stepBuilderFactory()
                .get("step1")
                .<ValeTroca, ValeTroca>chunk(10)
                .reader(this.reader())
                .processor(this.processor())
                .writer(this.writer())
                .build();
    }
}

BaseConfiguration:

public class BaseConfiguration implements BatchConfigurer {

    @Bean
    @Override
    public PlatformTransactionManager getTransactionManager() {
        return new ResourcelessTransactionManager();
    }

    @Bean
    @Override
    public SimpleJobLauncher getJobLauncher() throws Exception {
        final SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
        simpleJobLauncher.setJobRepository(this.getJobRepository());
        return simpleJobLauncher;
    }

    @Bean
    @Override
    public JobRepository getJobRepository() throws Exception {
        return new MapJobRepositoryFactoryBean(this.getTransactionManager()).getObject();
    }

    @Bean
    @Override
    public JobExplorer getJobExplorer() {
        MapJobRepositoryFactoryBean repositoryFactory = this.getMapJobRepositoryFactoryBean();
        return new SimpleJobExplorer(repositoryFactory.getJobInstanceDao(), repositoryFactory.getJobExecutionDao(),
                repositoryFactory.getStepExecutionDao(), repositoryFactory.getExecutionContextDao());
    }

    @Bean
    public MapJobRepositoryFactoryBean getMapJobRepositoryFactoryBean() {
        return new MapJobRepositoryFactoryBean(this.getTransactionManager());
    }

    @Bean
    public JobBuilderFactory jobBuilderFactory() throws Exception {
        return new JobBuilderFactory(this.getJobRepository());
    }

    @Bean
    public StepBuilderFactory stepBuilderFactory() throws Exception {
        return new StepBuilderFactory(this.getJobRepository(), this.getTransactionManager());
    }
}

ValeTrocaItemReader:

@Configuration
public class ValeTrocaItemReader implements ItemReader<ValeTroca>{

    @Value(value = "${url}")
    private String url;

    @Value(value = "${user}")
    private String user;

    @Value(value = "${password}")
    private String password;

    @Value(value = "${domain}")
    private String domain;

    @Value(value = "${inputDirectory}")
    private String inputDirectory;

    @Bean
    @Override
    public ValeTroca read() throws MalformedURLException, SmbException, IOException, Exception {
        File tempOutputFile = getInputFile();

        DefaultLineMapper<ValeTroca> lineMapper = new DefaultLineMapper<>();
        lineMapper.setLineTokenizer(new DelimitedLineTokenizer() {
            {
                setDelimiter(";");
                setNames(new String[]{"id_participante", "cpf", "valor"});
            }
        });
        lineMapper.setFieldSetMapper(
                new BeanWrapperFieldSetMapper<ValeTroca>() {
            {
                setTargetType(ValeTroca.class);
            }

        });

        FlatFileItemReader<ValeTroca> itemReader = new FlatFileItemReader<>();
        itemReader.setLinesToSkip(1);
        itemReader.setResource(new FileUrlResource(tempOutputFile.getCanonicalPath()));
        itemReader.setLineMapper(lineMapper);
        itemReader.open(new ExecutionContext());

        tempOutputFile.deleteOnExit();
        return itemReader.read();
    }

ItemProcessor示例:

public class ValeTrocaItemProcessor implements ItemProcessor<ValeTroca, ValeTroca> {

    @Override
    public ValeTroca process(ValeTroca item)  {
        //Do anything

        ValeTroca item2 = item;
        System.out.println(item2.getCpf());
        return item2;
    }

編輯: -春季啟動2.1.2.RELEASE-春季批處理4.1.1.RELEASE

查看您的配置,這里有一些注意事項:

  • BatchConfiguration看起來不錯。 這是一個典型的工作,只需一個面向塊的步驟。
  • 實際上, BaseConfiguration是使用@EnableBatchProcessing而不提供數據源時獲得的默認配置。 因此可以刪除此類
  • 添加@ConfigurationValeTrocaItemReader和標記方法read()@Bean是不正確的。 這意味着您在應用程序上下文中聲明了一個名為read的bean,其類型為ValeTroca 此外,您的自定義閱讀器使用FlatFileItemReader但與FlatFileItemReader相比沒有附加值。 您可以將您的閱讀器聲明為FlatFileItemReader並根據需要對其進行配置(資源,行映射器等)。 這也將避免在read方法中打開執行上下文的錯誤,該錯誤應在初始化閱讀器時或在ItemStream#open方法(如果閱讀器實現ItemStream

除此之外,我從您分享的內容中看不到為什么不調用處理器和編寫器。

已解決 :問題是,即使我沒有使用任何數據庫,Spring Batch雖然配置為在內存中具有JobRepository,但仍需要一個數據庫(通常為H2)來保存配置表,作業等。

在這種情況下,在pom.xml中禁用了JDBC且沒有H2的依賴關系。 剛剛將它們添加到項目中,問題就解決了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM