如何在 Spring Batch 中使用 FlatFileItemWriter 写入标准输出?
我在 Spring Batch 项目的 beans 定义文件中配置了以下编写器:
<bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:/path/to/somefile"/>
<property name="lineAggregator">
<bean class="MyCustomLineAggregator"/>
</property>
</bean>
现在,我希望输出转到标准输出,而不是写入 /path/to/somefile ,原因是我想通过启动此作业命令行启动器并将输出通过管道传输到另一个 UNIX 程序。
我尝试将资源属性设置为“file:/dev/stdout”,但随后出现异常: org.springframework.batch.item.ItemStreamException:无法创建文件:[/dev/stdout]
我试图看看是否有一个开箱即用的资源可以处理这个问题,但我有点不知道哪个可以完成工作...
感谢您的帮助。
编辑:以下是我根据您的建议提出的解决方案:
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
public class StdoutWriter<T> implements ItemWriter<T> {
LineAggregator<T> lineAggregator;
public void setLineAggregator(LineAggregator<T> lineAggregator) {
this.lineAggregator = lineAggregator;
}
@Override
public void write(List<? extends T> items) throws Exception {
for (T item : items) {
System.out.println(lineAggregator.aggregate(item));
}
}
}
I have the following writer configured in my beans definition file of a spring batch project :
<bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:/path/to/somefile"/>
<property name="lineAggregator">
<bean class="MyCustomLineAggregator"/>
</property>
</bean>
Now, instead of writing to /path/to/somefile, I want the output to go to the stdout, reason being that I want to launch this job through the command-line launcher and pipe the output to another unix program.
I tried by setting the resource property to "file:/dev/stdout", but then I get the exception :
org.springframework.batch.item.ItemStreamException: Unable to create file: [/dev/stdout]
I tried to see if there was an out of the box resource that could handle this, but I'm a bit clueless on which one could do the job...
Thanks for your help.
EDIT : Below is the solution I came up with, following your advise :
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
public class StdoutWriter<T> implements ItemWriter<T> {
LineAggregator<T> lineAggregator;
public void setLineAggregator(LineAggregator<T> lineAggregator) {
this.lineAggregator = lineAggregator;
}
@Override
public void write(List<? extends T> items) throws Exception {
for (T item : items) {
System.out.println(lineAggregator.aggregate(item));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为没有任何现成的东西可以满足您的需求。创建您自己的
ItemWriter
来执行简单的System.out.println()
非常简单。然后,您可以使用CompositeItemWriter
如果您仍然需要该文件,请将两者捆绑在一起。或者,您可以使用自定义
ItemWriterListener
并在那里进行打印。I don't think there is anything out of the box that does what you are looking for. It is simple enough to create your own
ItemWriter
that does a simpleSystem.out.println()
. You can then use theCompositeItemWriter
to tie the two together if you still want the file.Alternately, you can throw on a custom
ItemWriterListener
and do the printing there.您可以使用另一种技巧来写入标准输出。本质上,您可以指定 /dev/stdout 作为输出文件资源。请务必在编写器中将
shouldDeleteIfExists
设置为false
并将appendAllowed
设置为true
,否则您将收到运行时错误。There's another hack you can use to write to stdout. Essentially, you can specify
/dev/stdout
as the output file resource. Be sure to setshouldDeleteIfExists
tofalse
andappendAllowed
totrue
within the writer or you'll get runtime errors.