Repeating steps n times in Spring Batch JobBuilder?

I use Spring Batch in order to copy data between the same collection in different Mongo databases. As given in the following batch config, I use 2 steps; first delete the existing records and then insert records based on the query. I am sending 2 jobParameters from a service as “collectionName” and “query” and get these parameters from reader and writers.

BatchConfing:

@Bean
public Job job(JobRepository jobRepository,
           @Qualifier("transactionManager") PlatformTransactionManager transactionManager,
           @Qualifier("sourceMongoTemplate") MongoTemplate sourceMongoTemplate,
           @Qualifier("targetMongoTemplate") MongoTemplate targetMongoTemplate,
           @Qualifier("deleteTasklet") DeleteTasklet deleteTasklet) {

    return new JobBuilder("job", jobRepository)
            .incrementer(new RunIdIncrementer())
            .start(deleteStep(jobRepository, transactionManager, deleteTasklet))
            .next(insertData(jobRepository, transactionManager, sourceMongoTemplate, targetMongoTemplate))
            .listener(new MigrationJobCompletionListener("job"))
            .build();
}

@Bean
public Step deleteStep(JobRepository jobRepository,
                           PlatformTransactionManager transactionManager,
                           DeleteTasklet deleteTasklet) {

    return new StepBuilder("deleteStep", jobRepository)
            .tasklet(deleteTasklet, transactionManager)
            .listener(new MigrationStepListener("deleteStep"))
            .build();
}

@Bean
public Step insertStep(JobRepository jobRepository,
                   PlatformTransactionManager transactionManager,
                   MongoTemplate sourceMongoTemplate,
                   MongoTemplate targetMongoTemplate) {

    return new StepBuilder("insertStep", jobRepository)
            .<Document, Document>chunk(DEFAULT_CHUNK_SIZE, transactionManager)
            .startLimit(DEFAULT_LIMIT_SIZE)
            .reader(new MigrationItemReader(sourceMongoTemplate))
            .processor(new MigrationItemProcessor())
            .writer(new DataInsertionWriter(targetMongoTemplate))
            .listener(new MigrationStepListener("insertStep"))
            .build();
}

Now I am trying to process multiple collection by sending collectionName” – “query” pairs from service and then iterate each pairs in the job method. How to build such solution and is there a better solution for this scenario? As I have only several collections (maximum 4 or 5), I think currently no need to use paralel execution and can process each collection one by one.

Here is a sample pair creation in the service for this purpose:

Service:

public void runJob() {
    Map<String, String> pairs = new HashMap<>();
    pairs.put("employee", "{}");
    pairs.put("department", "{}");

    // convert to JSON string
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonPairs = objectMapper.writeValueAsString(pairs);

    // set JobParameters with properly encoded JSON string
    JobParameters jobParameters = new JobParametersBuilder()
            .addString("collectionsAndQueries", jsonPairs)
            .toJobParameters();

    // Launch the job with the built JobParameters
    batchJobLauncher.launchJob(jobParameters);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật