如何正确地将syncitemwriter添加到一个步骤中?
在春季批处理作业中,如下所示,我尝试使用asyncwriter,
@Bean
public Step readWriteStep() throws Exception {
return stepBuilderFactory.get("readWriteStep")
.listener(listener)
.<Data, Data>chunk(10)
.reader(dataItemReader())
.writer(dataAsyncWriter())
.build();
}
@Bean
public AsyncItemWriter<Data> dataAsyncWriter() throws Exception {
AsyncItemWriter<Data> asyncItemWriter = new AsyncItemWriter<>();
asyncItemWriter.setDelegate(dataItemWriter);
asyncItemWriter.afterPropertiesSet();
return asyncItemWriter;
}
如果我尝试这样的Intellij抱怨:
Required type: ItemWriter <? super Data>
Provided: AsyncItemWriter <Data>
当我更改。data,data,data&gt; chunk(10)
to 时。 &lt; data,future&lt; data&gt;&gt;块(10)
Intellij不会发出任何警告,但是当我运行作业时,我会得到以下例外:
java.lang.ClassCastException: Data cannot be cast to class java.util.concurrent.Future Data is in unnamed module of loader 'app';
java.util.concurrent.Future is in module java.base of loader 'bootstrap'
对于第一个和第二个参数是什么? 。&lt; data,data&gt;块(10)
?
这两个参数是处理器所需的两个参数,第二个参数是处理器回馈什么?
如何解决这个问题?
In a Spring Batch Job like the following I'm trying to use an AsyncWriter
@Bean
public Step readWriteStep() throws Exception {
return stepBuilderFactory.get("readWriteStep")
.listener(listener)
.<Data, Data>chunk(10)
.reader(dataItemReader())
.writer(dataAsyncWriter())
.build();
}
@Bean
public AsyncItemWriter<Data> dataAsyncWriter() throws Exception {
AsyncItemWriter<Data> asyncItemWriter = new AsyncItemWriter<>();
asyncItemWriter.setDelegate(dataItemWriter);
asyncItemWriter.afterPropertiesSet();
return asyncItemWriter;
}
If I try like this intelliJ complains:
Required type: ItemWriter <? super Data>
Provided: AsyncItemWriter <Data>
When I change .<Data, Data>chunk(10)
to .<Data, Future<Data>>chunk(10)
intelliJ does not make any warning, but when I run the Job, I get the following Exception:
java.lang.ClassCastException: Data cannot be cast to class java.util.concurrent.Future Data is in unnamed module of loader 'app';
java.util.concurrent.Future is in module java.base of loader 'bootstrap'
For what is the first and the second parameter here? .<Data, Data>chunk(10)
?
Are these two parameters for what the processor takes and the second what the processor is giving back?
How do I solve this Problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您更改步骤定义以使用以下内容,则应编译您的示例:
也就是说,我不确定在运行时可以正常工作,因为
asyncitemWriter
有望从其封闭式中解开构造项目未来
,其中这些未来
s由 asyncitempropersorsor 。换句话说,
asyncitemWriter
和asyncitemProcessor
应结合使用此模式来使用。这是两者的快速示例:Your example should compile if you change the step definition to use the following:
That said, I'm not sure this will work correctly at runtime because the
AsyncItemWriter
is expected to unwrap items from their enclosingFuture
s, where theseFuture
s are created by an AsyncItemProcessor.In other words,
AsyncItemWriter
andAsyncItemProcessor
should be used in conjunction for this pattern to work. Here is a quick example with both of them: