Spring Batch 5.x – 未注册名为 ‘job’ 的范围。

huangapple go评论61阅读模式
英文:

Spring Batch 5.x - No Scope registered for scope name 'job'

问题

我正在尝试将Spring Boot 2代码迁移到Boot 3,但在Spring Batch作业中遇到以下问题。

"java.lang.IllegalStateException: 未注册作用域名称 'job' 的作用域"

以下是代码。在Spring Batch 5中,JobScope/StepScopes的使用方式是否有区别?我无法从文档中理解这一点。

@Configuration
@EnableBatchProcessing
public class ProductFetcherJobConfiguration {

    private static int PRODUCT_CHUNK_SIZE = 20;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Bean
    public Job productFetcherJob(JobRepository jobRepository) {
        return new JobBuilder("productFetcherJob", jobRepository)
                .incrementer(new RunIdIncrementer())
                .start(productLinkListFetcher(jobRepository, this.transactionManager))
                .next(downloadProductDocuments(jobRepository, this.transactionManager, productWebDownloadReader(), productWebDownloadProcessor(), productWebDownloadWriter()))
                .listener(productFetcherJobListener())
                .build();
    }

    @Bean
    public Step productLinkListFetcher(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("productLinkListFetcher", jobRepository)
                .tasklet(productLinkListFetcherTask(), transactionManager)
                .build();
    }

    @Bean
    public Step downloadProductDocuments(JobRepository jobRepository, PlatformTransactionManager transactionManager, ItemReader<String> productWebDownloadReader,
                              ItemProcessor<String, String> productWebDownloadProcessor,
                              ItemWriter<String> productWebDownloadWriter) {
        return new StepBuilder("downloadProductDocuments", jobRepository).<String, String> chunk(PRODUCT_CHUNK_SIZE, transactionManager)
                .reader(productWebDownloadReader)
                .processor(productWebDownloadProcessor)
                .writer(productWebDownloadWriter)
                .listener(productWebChunkListener())
                .build();
    }

    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager();
    }

    @Bean
    @StepScope
    public ProductDownloadChunkListener productWebChunkListener() {
        return new ProductDownloadChunkListener();
    }

    @Bean
    @StepScope
    public ProductListFetcherTask productLinkListFetcherTask() {
        return new ProductListFetcherTask(cacheManager());
    }

    @Bean
    @StepScope
    public ProductDownloadReader productWebDownloadReader() {
        ProductDownloadReader productWebDownloadReader = new ProductDownloadReader();
        productWebDownloadReader.setName("productWebDownloadReader");
        return productWebDownloadReader;
    }

    @Bean
    @StepScope
    public ProductDownloadProcessor productWebDownloadProcessor() {
        return  new ProductDownloadProcessor(cacheManager());
    }

    @Bean
    @StepScope
    public ProductDownloadWriter productWebDownloadWriter() {
        return  new ProductDownloadWriter(cacheManager());
    }

    @Bean
    @JobScope
    public ProductFetcherJobListener productFetcherJobListener() {
        return new ProductFetcherJobListener(cacheManager());
    }

}
英文:

I am trying to migrate Spring Boot 2 code to Boot 3, I am getting the below issue with Spring Batch jobs.

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

Below is the code. Is there a difference in Spring Batch 5, how JobScope/StepScopes are to be used? I couldn't understand this from the documentation.

@Configuration
@EnableBatchProcessing
public class ProductFetcherJobConfiguration {
private static int PRODUCT_CHUNK_SIZE = 20;
@Autowired
private PlatformTransactionManager transactionManager;
@Bean
public Job productFetcherJob(JobRepository jobRepository) {
return new JobBuilder(&quot;productFetcherJob&quot;, jobRepository)
.incrementer(new RunIdIncrementer())
.start(productLinkListFetcher(jobRepository, this.transactionManager))
.next(downloadProductDocuments(jobRepository, this.transactionManager, productWebDownloadReader(), productWebDownloadProcessor(), productWebDownloadWriter()))
.listener(productFetcherJobListener())
.build();
}
@Bean
public Step productLinkListFetcher(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder(&quot;productLinkListFetcher&quot;, jobRepository)
.tasklet(productLinkListFetcherTask(), transactionManager)
.build();
}
@Bean
public Step downloadProductDocuments(JobRepository jobRepository, PlatformTransactionManager transactionManager, ItemReader&lt;String&gt; productWebDownloadReader,
ItemProcessor&lt;String, String&gt; productWebDownloadProcessor,
ItemWriter&lt;String&gt; productWebDownloadWriter) {
return new StepBuilder(&quot;downloadProductDocuments&quot;, jobRepository).&lt;String, String&gt; chunk(PRODUCT_CHUNK_SIZE, transactionManager)
.reader(productWebDownloadReader)
.processor(productWebDownloadProcessor)
.writer(productWebDownloadWriter)
.listener(productWebChunkListener())
.build();
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
@Bean
@StepScope
public ProductDownloadChunkListener productWebChunkListener() {
return new ProductDownloadChunkListener();
}
@Bean
@StepScope
public ProductListFetcherTask productLinkListFetcherTask() {
return new ProductListFetcherTask(cacheManager());
}
@Bean
@StepScope
public ProductDownloadReader productWebDownloadReader() {
ProductDownloadReader productWebDownloadReader = new ProductDownloadReader();
productWebDownloadReader.setName(&quot;productWebDownloadReader&quot;);
return productWebDownloadReader;
}
@Bean
@StepScope
public ProductDownloadProcessor productWebDownloadProcessor() {
return  new ProductDownloadProcessor(cacheManager());
}
@Bean
@StepScope
public ProductDownloadWriter productWebDownloadWriter() {
return  new ProductDownloadWriter(cacheManager());
}
@Bean
@JobScope
public ProductFetcherJobListener productFetcherJobListener() {
return new ProductFetcherJobListener(cacheManager());
}
}

答案1

得分: 1

在Spring Batch 5中,JobScope/StepScopes的使用方式是否有区别?

不,行为上没有区别。然而,Spring Batch与Spring Boot 3的配置方式存在差异。请参阅迁移指南:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#spring-batch-changes

您正在使用@EnableBatchProcessing,这意味着Spring Batch的自动配置未启用(包括批处理范围的注册)。如果要使用@EnableBatchProcessing,您需要手动配置Spring Batch,包括批处理范围的注册。

英文:

> Is there a difference in Spring Batch 5, how JobScope/StepScopes are to be used?

No, there is no difference in behaviour. However, there is a difference in the way Spring Batch is configured with Spring Boot 3. See migration guide here: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#spring-batch-changes

You are using @EnableBatchProcessing, which means the auto-configuration of Spring Batch is not activated (including batch scopes registration). If you want to use @EnableBatchProcessing, you need to configure Spring Batch manually, including batch scope registration.

huangapple
  • 本文由 发表于 2023年6月2日 02:09:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定