没有应用程序上下文的Spring Batch StepScope

时间:2014-04-21 18:21:16

标签: spring-batch

我在不使用ApplicationContext的情况下摆弄Spring Batch。

现在我遇到了障碍,似乎没有办法获得有步骤的实施。它甚至可能吗?

在使用分区步骤时出现问题,不会为每个分区(从属)步骤创建阅读器,可以在https://github.com/hello-spring-batch/hello-spring-batch-usecase-multiresourcepartitioner找到带有测试的源

package de.hello.spring.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.partition.support.MultiResourcePartitioner;
import org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

public class JobFactory {

    public static Job createJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
    JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(jobRepository);
    StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(jobRepository, transactionManager);

    ItemReader reader = new CustomFlatFileItemReader();
    ItemWriter writer = new CustomItemWriter();

    Step slaveStep = stepBuilderFactory
        .get("slaveStep")
        .chunk(5)
        .reader(reader)
        .writer(writer)
        .listener(reader)
        .build();

    MultiResourcePartitioner partitioner = new CustomMultiResourcePartitioner();
    SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
    taskExecutor.setConcurrencyLimit(4);
    TaskExecutorPartitionHandler taskExecutorPartitionHandler = new TaskExecutorPartitionHandler();
    taskExecutorPartitionHandler.setTaskExecutor(taskExecutor);
    taskExecutorPartitionHandler.setStep(slaveStep);

    Step masterStep = stepBuilderFactory
        .get("masterStep")
        .partitioner("slaveStep", partitioner)
        .partitionHandler(taskExecutorPartitionHandler)
        .listener(partitioner)
        .build();

    return jobBuilderFactory
        .get("job1")
        .start(masterStep)
        .build();
    }
}
  • CustomMultiResourcePartitioner有一个@BeforeStep方法,用于从stepExecution.getJobExecution.getJobParameters获取目录路径
  • CustomFlatFileItemWriter有一个@BeforeStep方法用于获取实际文件名(由分区程序创建)
  • CustomItemWriter只是一个Logger

0 个答案:

没有答案