英文:
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("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());
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论