Spring批处理:JobScope在Spring 3.0.0中不可用

时间:2016-03-08 11:18:32

标签: spring maven spring-batch

根据Spring批处理documentationJobScope作为Spring批处理3.0的一部分引入。但是,看起来JobScope注释在Spring batch 3.0 jar中不可用。我在spring-batch-core中指定了pom.xml依赖项,如下所示:

<properties>
    <jdk.version>1.6</jdk.version>
    <spring.version>4.2.4.RELEASE</spring.version>
    <spring.batch.version>3.0.0.RELEASE</spring.batch.version>
</properties>

<dependencies>

    <!-- Spring Batch dependencies -->
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>${spring.batch.version}</version>
    </dependency>

当我将spring-batch-version更改为3.0.6时,会按预期找到JobScope注释。据我所知,JobScope是作为春季批次3.0的一部分引入的,因此应该可以在从3.0.0开始的任何弹簧批量罐中使用。

版本3.0.0中没有JobScope注释可用的具体原因或者我是否需要手动添加包含此注释的其他jar?我相信spring-batch-core依赖项应该提取所有额外的spring批量依赖项,我不需要明确指定它们。

我使用基于注释的配置来编写批处理作业。我需要JobScope注释来将作业参数后期绑定到我的bean。有没有办法在不使用JobScope的情况下执行此操作?

修改

我的作业配置文件如下:

@Configuration
@EnableBatchProcessing
public class FileLoaderConfigurationNoAbstractLoader {

    @Autowired
    private ResourcePatternResolver resourcePatternResolver;

    @Bean
    public Job job(JobBuilderFactory jobs, Step s1) {
        return jobs.get("FileLoader").incrementer(new RunIdIncrementer()).start(s1).build();
    }

    @Bean
    @StepScope
    @SuppressWarnings("rawtypes")
    public FlatFileItemReader reader(@Value("#{stepExecutionContext['fileName']}") String filePath,
            @Value("#{jobParameters['fieldSetMapperClass']}") String fieldSetMapperClass,
            @Value("#{jobParameters['processType']}") String processType, @Value("#{jobParameters['dataType']}") String dataType,
            FileLoaderCreator loader) {
        String path = filePath.substring(filePath.indexOf(":") + 1, filePath.length());
        return loader.getReader(path, fieldSetMapperClass, processType, dataType);
    }

    @Bean
    @Scope(value = "job", proxyMode = ScopedProxyMode.TARGET_CLASS)
    @SuppressWarnings("rawtypes")
    public ItemWriter writer(@Value("#{jobParameters['dataType']}") String dataType) {
        return new CollectionItemWriter(dataType);
    }

    @Bean
    @Scope(value = "job", proxyMode = ScopedProxyMode.TARGET_CLASS)
    @SuppressWarnings("rawtypes")
    public ItemProcessor processor(FileLoaderCreator loader, @Value("#{jobParameters['itemProcessorClass']}") String itemProcessorClass) {
        return loader.getItemProcessor(itemProcessorClass);
    }

    @Bean
    @Scope(value = "job", proxyMode = ScopedProxyMode.TARGET_CLASS)
    @SuppressWarnings("all")
    public Step readStep(StepBuilderFactory stepBuilderFactory, ItemReader reader, ItemWriter writer, ItemProcessor processor,
            TaskExecutor taskExecutor, FileLoaderCreator fileLoader, @Value("#{jobParameters['dataType']}") String dataType,
            @Value("#{jobParameters['processType']}") String processType) {

        final Step readerStep = stepBuilderFactory.get(dataType + " ReadStep:slave")
                .chunk(fileLoader.getCommitInterval(processType, dataType)).reader(reader).processor(processor).writer(writer)
                .taskExecutor(taskExecutor).throttleLimit(fileLoader.getThrottleLimit(processType, dataType)).build();

        final Step partitionedStep = stepBuilderFactory.get(dataType + " ReadStep:master").partitioner(readerStep)
                .partitioner(dataType + " ReadStep:slave", partitioner(fileLoader, null, null)).taskExecutor(taskExecutor).build();

        return partitionedStep;

    }

    @Bean
    @Scope(value = "job", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Partitioner partitioner(FileLoaderCreator fileLoader, @Value("#{jobParameters['processType']}") String processType,
            @Value("#{jobParameters['dataType']}") String dataType) {
        MultiResourcePartitioner partitioner = new MultiResourcePartitioner();
        Resource[] resources;
        try {
            resources = resourcePatternResolver.getResources("file:" + fileLoader.getPath(processType, dataType)
                    + fileLoader.getFilePattern(processType, dataType));
        } catch (IOException e) {
            throw new RuntimeException("I/O problems when resolving the input file pattern.", e);
        }
        partitioner.setResources(resources);
        return partitioner;
    }

    /*
     * Use this if you want the writer to have job scope (JIRA BATCH-2269).
     * Might also change the return type of writer to ListItemWriter for this to
     * work.
     */
    @SuppressWarnings("serial")
    @Bean
    public TaskExecutor taskExecutor() {
        return new SimpleAsyncTaskExecutor() {
            @Override
            protected void doExecute(final Runnable task) {
                // gets the jobExecution of the configuration thread
                final JobExecution jobExecution = JobSynchronizationManager.getContext().getJobExecution();
                super.doExecute(new Runnable() {
                    public void run() {
                        JobSynchronizationManager.register(jobExecution);

                        try {
                            task.run();
                        } finally {
                            JobSynchronizationManager.close();
                        }
                    }
                });
            }
        };
    }

    @Bean
    public FileLoaderCreator loader() {
        System.out.println("Creating loader only once ");
        return new FileLoaderCreator();
    }

    @Bean
    public JobResults jobResults() {
        return new JobResults();
    }
}

根据M.Deinum's建议,我使用了@Scope(value = "job", proxyMode = ScopedProxyMode.TARGET_CLASS)而不是JobScope注释,但遇到了以下异常:

java.lang.IllegalStateException: No Scope registered for scope name 'job'' exception

我尝试通过将以下bean添加到上述配置中来解决此问题:

    @Bean
    public JobScope jobScope() {
        JobScope scope = new JobScope();
        scope.setAutoProxy(false);
        return scope;
    }

这在我的java配置中的以下行给出了一个例外:

resources = resourcePatternResolver.getResources("file:" + fileLoader.getPath(processType, dataType)
                + fileLoader.getFilePattern(processType, dataType));

请注意,这对JobScope和spring batch 3.0.1完全正常。

1 个答案:

答案 0 :(得分:1)

  

我使用基于注释的配置来编写批处理作业。我需要   JobScope注释用于将作业参数后期绑定到我的   豆子。有没有办法在不使用JobScope的情况下执行此操作?

它应该在没有Jobscope的情况下工作,通过使用Stepscope,您可以访问作业参数,请参阅https://docs.spring.io/spring-batch/reference/html/configureStep.html#step-scope

如果您需要可用于所有bean的信息,您可以实现一个包含信息的简单spring bean(类型并发hashmap)

如果您需要为每个批处理运行保留的信息,您可以实现JobExecutionListener来放置/拉取JobExecutionContext中的信息并填充(前面提到的)简单bean

*)如果您需要重新启动