根據Spring批處理documentation,JobScope
作爲Spring批處理3.0的一部分引入。但是,它看起來像JobScope註釋在Spring批處理3.0罐中不可用。我指定我pom.xml
的spring-batch-core
依賴如下:Spring批處理:JobScope在Spring 3.0.0中不可用
<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
是作爲Spring批處理3.0的一部分引入的,因此應該可以在任何從3.0.0開始的彈簧批處理罐中使用。
是否有任何具體原因爲什麼JobScope
註釋在版本3.0.0中不可用或者我是否需要手動添加包含此註釋的其他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
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批處理3.0.1。
它被添加到3.0.1中。但使用'@ JobScope'與使用'@Scope(value =「job」,proxyMode = ScopedProxyMode.TARGET_CLASS)''相同。但爲什麼不簡單地使用3.0.6這是最新的3.0版本,爲什麼堅持使用舊版本? –
@ M.Deinum True。包含這個註釋的最低版本似乎是3.0.1。這是否意味着如果我必須使用基於註釋的作業配置,我不得不使用3.0.1或更高版本?我們公司有一個框架審批流程,目前批准的spring批處理版本是3.0.0。 – CKing
查看我編輯的評論... –