ItemWriter

ItemWriter 在功能上类似于 ItemReader,但其操作相反。仍然需要找到、打开和关闭资源,但不同之处在于,ItemWriter 写出数据,而不是读入数据。对于数据库或队列,这些操作可能是插入、更新或发送。输出序列化的格式特定于每个批处理作业。

ItemWriter is similar in functionality to an ItemReader but with inverse operations. Resources still need to be located, opened, and closed but they differ in that an ItemWriter writes out, rather than reading in. In the case of databases or queues, these operations may be inserts, updates, or sends. The format of the serialization of the output is specific to each batch job.

ItemReader 相似,ItemWriter 也是一个相当通用的接口,如下面的接口定义所示:

As with ItemReader, ItemWriter is a fairly generic interface, as shown in the following interface definition:

public interface ItemWriter<T> {

    void write(Chunk<? extends T> items) throws Exception;

}

ItemReader 上的 read 类似,write 提供了 ItemWriter 的基本约定。只要它处于打开状态,它就会尝试写入作为输入传入的项列表。由于通常希望将项“分批”到一个区块,然后再输出,所以该接口接受项列表,而不是项本身。在写出列表后,可以在返回前执行任何必要的刷新。例如,如果写入到 Hibernate DAO,则可以进行多次写入调用,每次调用一个项。然后,编写器可以在返回前调用 Hibernate 会话上的 flush

As with read on ItemReader, write provides the basic contract of ItemWriter. It attempts to write out the list of items passed in as long as it is open. Because it is generally expected that items are 'batched' together into a chunk and then output, the interface accepts a list of items, rather than an item by itself. After writing out the list, any flushing that may be necessary can be performed before returning from the write method. For example, if writing to a Hibernate DAO, multiple calls to write can be made, one for each item. The writer can then call flush on the hibernate session before returning.