AsyncItemWriter - 将自定义 class 解包到 Future 时遇到 ClassCastException
AsyncItemWriter - Facing ClassCastException when unwrapping custom class into Future
我正在编写一个 spring 批处理作业来读取一些数据并将其写入文件。数据被分成 3 个块,每个块在合并之前都需要写入一个单独的文件。读取和聚合工作正常。
但是,当异步编写器抛出错误时 -- java.lang.ClassCastException: class package.Custom cannot be cast to class java.util.concurrent.Future
我的自定义 DTO 似乎没有展开到 Future 对象中。
这是我的删节代码
@Bean
public Step workerStep() throws Exception {
return stepBuilderFactory.get(WORKER_BEAN)
.<Custom, Custom>chunk(CHUNK_SIZE)
.reader(reader(null))
.writer(writer())
.taskExecutor(threadPoolExecutor)
.throttleLimit(THROTTLE_LIMIT)
.build();
}
@Bean
@StepScope
public CustomReader reader(@Value("#{stepExecutionContext['currentMod']}") Long currentMod) {
if ( ... some logic ... ) {
return null;
}
return new CustomReader();
}
@Bean
@StepScope
public AsyncItemWriter writer() throws Exception {
AsyncItemWriter<Custom> asyncItemWriter = new AsyncItemWriter<>();
asyncItemWriter.setDelegate(delegateWriter(null));
asyncItemWriter.afterPropertiesSet();
return asyncItemWriter;
}
@Bean(destroyMethod=EMPTY)
@StepScope
public CustomWriter<Custom> delegateWriter(@Value("#{stepExecutionContext['currentMod']}") Long currentMod) {
CustomWriter<Custom> customWriter = new CustomWriter<>();
customWriter.setLineAggregator(new DelimitedLineAggregator<Custom>() {
{
setDelimiter(COMMA);
setFieldExtractor(new BeanWrapperFieldExtractor<Custom>() {
{
setNames( ... get names logic ... );
}
});
}
});
customWriter.setResource(new FileSystemResource(name));
// ... header call-back logic ...
customWriter.setAppendAllowed(true);
customWriter.setShouldDeleteIfEmpty(true);
customWriter.setShouldDeleteIfExists(true);
return customWriter;
}
@Bean
public TaskExecutor taskExecutor() {
return new SimpleAsyncTaskExecutor(THREAD_NAME_PREFIX);
}
AsyncItemWriter
需要包裹在 Future
中的项目(参见 Javadoc), and this is typically done by an AsyncItemProcessor
. The AsyncItemWriter
and AsyncItemProcessor
are used in conjunction to implement a fork/join scenario, see Asynchronous Processors 部分。
我正在编写一个 spring 批处理作业来读取一些数据并将其写入文件。数据被分成 3 个块,每个块在合并之前都需要写入一个单独的文件。读取和聚合工作正常。
但是,当异步编写器抛出错误时 -- java.lang.ClassCastException: class package.Custom cannot be cast to class java.util.concurrent.Future
我的自定义 DTO 似乎没有展开到 Future 对象中。 这是我的删节代码
@Bean
public Step workerStep() throws Exception {
return stepBuilderFactory.get(WORKER_BEAN)
.<Custom, Custom>chunk(CHUNK_SIZE)
.reader(reader(null))
.writer(writer())
.taskExecutor(threadPoolExecutor)
.throttleLimit(THROTTLE_LIMIT)
.build();
}
@Bean
@StepScope
public CustomReader reader(@Value("#{stepExecutionContext['currentMod']}") Long currentMod) {
if ( ... some logic ... ) {
return null;
}
return new CustomReader();
}
@Bean
@StepScope
public AsyncItemWriter writer() throws Exception {
AsyncItemWriter<Custom> asyncItemWriter = new AsyncItemWriter<>();
asyncItemWriter.setDelegate(delegateWriter(null));
asyncItemWriter.afterPropertiesSet();
return asyncItemWriter;
}
@Bean(destroyMethod=EMPTY)
@StepScope
public CustomWriter<Custom> delegateWriter(@Value("#{stepExecutionContext['currentMod']}") Long currentMod) {
CustomWriter<Custom> customWriter = new CustomWriter<>();
customWriter.setLineAggregator(new DelimitedLineAggregator<Custom>() {
{
setDelimiter(COMMA);
setFieldExtractor(new BeanWrapperFieldExtractor<Custom>() {
{
setNames( ... get names logic ... );
}
});
}
});
customWriter.setResource(new FileSystemResource(name));
// ... header call-back logic ...
customWriter.setAppendAllowed(true);
customWriter.setShouldDeleteIfEmpty(true);
customWriter.setShouldDeleteIfExists(true);
return customWriter;
}
@Bean
public TaskExecutor taskExecutor() {
return new SimpleAsyncTaskExecutor(THREAD_NAME_PREFIX);
}
AsyncItemWriter
需要包裹在 Future
中的项目(参见 Javadoc), and this is typically done by an AsyncItemProcessor
. The AsyncItemWriter
and AsyncItemProcessor
are used in conjunction to implement a fork/join scenario, see Asynchronous Processors 部分。