简体   繁体   English

使用项目读取器和写入器编写多个平面文件Spring Batch

[英]Writing Multiple Flat Files with Item Readers And Writers Spring Batch

So I want to create 6 delimited reports , they all use the same data source and same table , the query is different for each , they all are also independent. 所以我想创建6个带分隔符的报表,它们都使用相同的数据源和相同的表,每个查询都不同,它们也都是独立的。

The way I have it setup right now I have 6 pojos , 6 row mappers , 6 item readers and 6 item writers and it works, is there a way simplify this without making it over complicated it,specially since the 6 item readers are the same except for the query. 我现在设置的方式有6个pojos,6个行映射器,6个项目读取器和6个项目写入器,并且可以正常工作,有没有一种方法可以简化此过程而不使其变得复杂,特别是因为6个项目读取器是相同的除了查询。

We have spring boot application which has configuration file like this: 我们有一个Spring Boot应用程序,它具有如下配置文件:

@Configuration
@EnableBatchProcessing(modular = true)
public class JobContextConfig {

    @Bean
    public ApplicationContextFactory job1Factory() {
        return new GenericApplicationContextFactory(Job1Config.class);
    }

    @Bean
    public ApplicationContextFactory job2Factory() {
        return new GenericApplicationContextFactory(Job2Config.class);
    }

    @Bean
    public ApplicationContextFactory job3Factory() {
        return new GenericApplicationContextFactory(Job3Config.class);
    }       
}

And application.properties with spring.batch.job.enabled: false so we control launch of jobs. 带有spring.batch.job.enabled: false application.properties ,因此我们控制作业的启动。

So we have separate context for each job, we start our jobs with launchers which react on amqp messages. 因此,我们对每个作业都有单独的上下文,我们使用对amqp消息做出反应的启动器来启动我们的作业。 Each job has configuration class which is isolated but we define reusable components in @JobScope and avoid duplication. 每个作业都有隔离的配置类,但是我们在@JobScope定义了可重用的组件,并避免了重复。

ie we have file import job and as last step it needs to delete file which is processed from directory. 即,我们有文件导入作业,并且在最后一步,它需要删除从目录处理的文件。 That is tasklet in @JobScope which means it is recreated for each job and it does cleanup, it is @Autowired in every job that needs it. 这是@JobScope中的tasklet,这意味着将为每个作业重新创建它并进行清理,在需要它的每个作业中将其自动@Autowired

The other idea which we used on import job but with dynamic mapping of columns in csv is that we used Factory which is @JobScope and based on some criteria, lets say job parameter it is creating RowFieldSetMapper to use for that instance of job. 我们在导入作业中使用但在csv中具有动态映射列的另一个想法是,我们使用了@JobScope Factory ,并基于某些条件,可以说作业参数是创建RowFieldSetMapper以用于该作业实例。 But we had same job and same everything except of input csv file structure so that was ok for us. 但是除了输入的csv文件结构之外,我们有相同的工作和所有其他事情,因此对我们来说还可以。

If you have similar but not completely different reader , processor , writer I would agree that duplication is ok because it would be easier to read, figure out and maintain separate jobs. 如果您有相似但不完全不同的readerprocessorwriter我会同意复制是可以的,因为这样会更易于阅读,查明和维护单独的作业。 Use common sense to judge what would be better, readability and maintainability or having complicated mechanism but with less code duplication. 使用常识来判断哪种方法更好,可读性和可维护性,或者具有复杂的机制但代码重复较少。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM