Skip to main content

ItemWriter

Trait ItemWriter 

Source
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 data
  • open(): Initializes resources before writing starts
  • close(): 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§

Source

fn write(&self, items: &[O]) -> ItemWriterResult

Writes the given items.

§Parameters
  • items: A slice of items to write
§Returns
  • Ok(()) when items are successfully written
  • Err(BatchError) when an error occurs during writing

Provided Methods§

Source

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 succeeds
  • Err(BatchError) when an error occurs during flushing
Source

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 succeeds
  • Err(BatchError) when an error occurs during opening
Source

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 succeeds
  • Err(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>>).

The ?Sized bound makes this cover trait objects: dyn Trait is unsized, so without ?Sized the impl would not apply to them.

Implementors§

Source§

impl<O> ItemWriter<O> for OrmItemWriter<'_, O>

Available on crate feature orm only.
Source§

impl<O, W: Write> ItemWriter<O> for XmlItemWriter<O, W>
where O: Serialize,

Available on crate feature xml only.
Source§

impl<O: Serialize + Clone> ItemWriter<O> for MySqlItemWriter<'_, O>

Available on crate feature rdbc only.
Source§

impl<O: Serialize + Clone> ItemWriter<O> for PostgresItemWriter<'_, O>

Available on crate feature rdbc only.
Source§

impl<O: Serialize + Clone> ItemWriter<O> for SqliteItemWriter<'_, O>

Available on crate feature rdbc only.
Source§

impl<O: Serialize + Send + Sync> ItemWriter<O> for MongodbItemWriter<'_, O>

Available on crate feature mongodb only.
Source§

impl<O: Serialize, W: Write> ItemWriter<O> for CsvItemWriter<O, W>

Available on crate feature csv only.
Source§

impl<O: Serialize, W: Write> ItemWriter<O> for JsonItemWriter<O, W>

Available on crate feature json only.
Source§

impl<T, W1, W2> ItemWriter<T> for CompositeItemWriter<W1, W2>
where W1: ItemWriter<T>, W2: ItemWriter<T>,

Source§

impl<T: Debug> ItemWriter<T> for LoggerWriter<T>

Available on crate feature logger only.