pub trait ItemWriter<O> {
// Required method
fn write(&self, items: &[O]) -> ItemWriterResult;
// Provided methods
fn flush(&self) -> ItemWriterResult { ... }
fn open(&self) -> ItemWriterResult { ... }
fn close(&self) -> ItemWriterResult { ... }
}Expand description
A trait for writing items.
This trait defines the contract for components that write items to a data destination. It is one of the fundamental building blocks of the batch processing pipeline.
§Design Pattern
This follows the Strategy Pattern, allowing different writing strategies to be interchangeable while maintaining a consistent interface.
§Lifecycle Methods
This trait includes additional lifecycle methods:
flush(): Flushes any buffered dataopen(): Initializes resources before writing startsclose(): Releases resources after writing completes
§Example
use spring_batch_rs::core::item::{ItemWriter, ItemWriterResult};
use spring_batch_rs::error::BatchError;
struct ConsoleWriter;
impl ItemWriter<String> for ConsoleWriter {
fn write(&self, items: &[String]) -> ItemWriterResult {
for item in items {
println!("{}", item);
}
Ok(())
}
}Required Methods§
Provided Methods§
Sourcefn flush(&self) -> ItemWriterResult
fn flush(&self) -> ItemWriterResult
Flushes any buffered data.
This method is called after a chunk of items has been written, and allows the writer to flush any internally buffered data to the destination.
§Default Implementation
The default implementation does nothing and returns Ok(()).
§Returns
Ok(())when the flush operation succeedsErr(BatchError)when an error occurs during flushing
Sourcefn open(&self) -> ItemWriterResult
fn open(&self) -> ItemWriterResult
Opens the writer.
This method is called before any items are written, and allows the writer to initialize any resources it needs.
§Default Implementation
The default implementation does nothing and returns Ok(()).
§Returns
Ok(())when the open operation succeedsErr(BatchError)when an error occurs during opening
Sourcefn close(&self) -> ItemWriterResult
fn close(&self) -> ItemWriterResult
Closes the writer.
This method is called after all items have been written, and allows the writer to release any resources it acquired.
§Default Implementation
The default implementation does nothing and returns Ok(()).
§Returns
Ok(())when the close operation succeedsErr(BatchError)when an error occurs during closing
Implementations on Foreign Types§
Source§impl<T, W: ItemWriter<T> + ?Sized> ItemWriter<T> for Box<W>
Allows any Box<W> where W: ItemWriter<T> to be used wherever
&dyn ItemWriter<T> is expected — including boxed concrete types
(Box<MyWriter>) and boxed trait objects (Box<dyn ItemWriter<T>>).
impl<T, W: ItemWriter<T> + ?Sized> ItemWriter<T> for Box<W>
Allows any Box<W> where W: ItemWriter<T> to be used wherever
&dyn ItemWriter<T> is expected — including boxed concrete types
(Box<MyWriter>) and boxed trait objects (Box<dyn ItemWriter<T>>).
The ?Sized bound makes this cover trait objects: dyn Trait is
unsized, so without ?Sized the impl would not apply to them.
fn write(&self, items: &[T]) -> ItemWriterResult
fn flush(&self) -> ItemWriterResult
fn open(&self) -> ItemWriterResult
fn close(&self) -> ItemWriterResult
Implementors§
impl<O> ItemWriter<O> for OrmItemWriter<'_, O>where
O: ActiveModelTrait + Send,
orm only.impl<O, W: Write> ItemWriter<O> for XmlItemWriter<O, W>where
O: Serialize,
xml only.impl<O: Serialize + Clone> ItemWriter<O> for MySqlItemWriter<'_, O>
rdbc only.impl<O: Serialize + Clone> ItemWriter<O> for PostgresItemWriter<'_, O>
rdbc only.impl<O: Serialize + Clone> ItemWriter<O> for SqliteItemWriter<'_, O>
rdbc only.impl<O: Serialize + Send + Sync> ItemWriter<O> for MongodbItemWriter<'_, O>
mongodb only.impl<O: Serialize, W: Write> ItemWriter<O> for CsvItemWriter<O, W>
csv only.impl<O: Serialize, W: Write> ItemWriter<O> for JsonItemWriter<O, W>
json only.impl<T, W1, W2> ItemWriter<T> for CompositeItemWriter<W1, W2>where
W1: ItemWriter<T>,
W2: ItemWriter<T>,
impl<T: Debug> ItemWriter<T> for LoggerWriter<T>
logger only.