简体   繁体   English

Spring Batch不会在块流中同时调用ItemProcessor和ItemWriter

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

I have a spring batch application to get a file in samba server and generate a new file in a different folder on the same server. 我有一个春季批处理应用程序,以在samba服务器中获取文件并在同一服务器上的其他文件夹中生成一个新文件。 However, only ItemReader is called in the flow. 但是,流中仅调用ItemReader。 What is the problem? 问题是什么? Thanks. 谢谢。

BatchConfiguration: 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: 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: 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();
    }

Sample of ItemProcessor: 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;
    }

EDIT: - Spring boot 2.1.2.RELEASE - Spring batch 4.1.1.RELEASE 编辑: -春季启动2.1.2.RELEASE-春季批处理4.1.1.RELEASE

Looking at your configuration, here are a couple of notes: 查看您的配置,这里有一些注意事项:

  • BatchConfiguration looks good. BatchConfiguration看起来不错。 That's a typical job with a single chunk-oriented step. 这是一个典型的工作,只需一个面向块的步骤。
  • BaseConfiguration is actually the default configuration you get when using @EnableBatchProcessing without providing a datasource. 实际上, BaseConfiguration是使用@EnableBatchProcessing而不提供数据源时获得的默认配置。 So this class can be removed 因此可以删除此类
  • Adding @Configuration on ValeTrocaItemReader and marking the method read() with @Bean is not correct. 添加@ConfigurationValeTrocaItemReader和标记方法read()@Bean是不正确的。 This means your are declaring a bean named read of type ValeTroca in your application context. 这意味着您在应用程序上下文中声明了一个名为read的bean,其类型为ValeTroca Moreover, your custom reader uses a FlatFileItemReader but has no added value compared to a FlatFileItemReader . 此外,您的自定义阅读器使用FlatFileItemReader但与FlatFileItemReader相比没有附加值。 You can declare your reader as a FlatFileItemReader and configure it as needed (resource, line mapper, etc ). 您可以将您的阅读器声明为FlatFileItemReader并根据需要对其进行配置(资源,行映射器等)。 This will also avoid the mistake of opening the execution context in the read method, which should be done when initializaing the reader or in the ItemStream#open method if the reader implements ItemStream 这也将避免在read方法中打开执行上下文的错误,该错误应在初始化阅读器时或在ItemStream#open方法(如果阅读器实现ItemStream

Other than that, I don't see from what you shared why the processor and writer are not called. 除此之外,我从您分享的内容中看不到为什么不调用处理器和编写器。

SOLVED : The problem was that even though I'm not using any databases, the spring batch, although configured to have the JobRepository in memory, needs a database (usually H2) to save the configuration tables, jobs, etc. 已解决 :问题是,即使我没有使用任何数据库,Spring Batch虽然配置为在内存中具有JobRepository,但仍需要一个数据库(通常为H2)来保存配置表,作业等。

In this case, the dependencies of JDBC and without H2 in pom.xml were disabled. 在这种情况下,在pom.xml中禁用了JDBC且没有H2的依赖关系。 Just added them to the project and the problem was solved! 刚刚将它们添加到项目中,问题就解决了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring Batch中调用ItemReader,ItemProcessor,ItemWriter的构造函数时? - When constructors for ItemReader, ItemProcessor, ItemWriter are called in Spring Batch? Spring 批处理 - 如何将多个项目的列表从输入传递到 ItemReader、ItemProcessor 和 ItemWriter - Spring Batch- how to pass list of multiple items from input to ItemReader, ItemProcessor and ItemWriter Spring Batch - ItemWriter 正在写入由 ItemReader 读取的相同对象,但不是通过 ItemProcessor 处理后返回的对象 - Spring Batch - ItemWriter is writing same object read by ItemReader but not the one returned after processing through ItemProcessor 从引导 Spring 批量应用 ItemWriter 的 @BeforeStep 方法调用 ItemProcessor 的 @BeforeStep 方法之前 - From boot Spring Batch application @BeforeStep method of the ItemWriter calling before of @BeforeStep method of the ItemProcessor Spring批处理多线程ItemWriter - Spring batch multithreaded ItemWriter Spring 批量通配符ItemWriter - Spring Batch Wildcard ItemWriter 在 ItemProcessor (Spring Batch) 中休眠没有会话 - Hibernate no session in ItemProcessor (Spring Batch) Spring 批处理 GZIP ItemWriter/ItemReader - Spring batch GZIP ItemWriter/ItemReader spring batch:没有ItemWriter的Tasklet - spring batch : Tasklet without ItemWriter Spring Batch Json定制ItemWriter - Spring Batch Json custom ItemWriter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM