Interface BeanConsumer<T>

Type Parameters:
T - The type of bean received by this consumer.
All Superinterfaces:
Consumer<T>, ThrowingConsumer<T>
All Known Subinterfaces:
BeanChannel<T>
All Known Implementing Classes:
ListBeanChannel

public interface BeanConsumer<T> extends ThrowingConsumer<T>
A lifecycle-aware consumer for use as a parser target, receiving deserialized beans one at a time.

Extends ThrowingConsumer so that ThrowingConsumer.acceptThrows(Object) supports checked exceptions directly — no wrapping in RuntimeException required for operations such as JDBC inserts.

The parser drives the full lifecycle:

  1. Calls begin() before the first element
  2. Calls ThrowingConsumer.acceptThrows(Object) for each parsed element
  3. If ThrowingConsumer.acceptThrows(Object) throws, calls onError(Exception):
    • If onError() absorbs the exception (doesn't rethrow), parsing continues to the next element (skip-and-continue / fault-tolerant ingestion)
    • If onError() rethrows, parsing stops and the exception propagates
  4. Always calls complete() after all elements (like finally) — whether parsing succeeded or onError() rethrew. Use this for resource cleanup (close connections, etc.)

For round-trip support (same property used for both serialization and parsing), use BeanChannel instead, which extends both BeanConsumer and BeanSupplier.

If a BeanConsumer is encountered during serialization (not parsing), the serializer will throw an IllegalArgumentException with a message recommending BeanSupplier or BeanChannel.

Example (DB-backed consumer with batch commits):

@Marshalled(factory=ItemConsumerFactory.class) public class ItemConsumer implements BeanConsumer<Item> { private Connection conn; private PreparedStatement stmt; private int count; @Override public void begin() throws Exception { conn = ds.getConnection(); conn.setAutoCommit(false); stmt = conn.prepareStatement("INSERT INTO items (name) VALUES (?)"); } @Override public void acceptThrows(Item item) throws Exception { stmt.setString(1, item.getName()); stmt.executeUpdate(); if (++count % 500 == 0) conn.commit(); // batch commit } @Override public void onError(Exception e) throws Exception { conn.rollback(); throw e; } @Override public void complete() throws Exception { conn.commit(); stmt.close(); conn.close(); } }

See Also:
  • Method Details

    • begin

      default void begin() throws Exception
      Called before the first element is accepted.

      Use this to open database connections, prepare statements, or perform other setup.

      Throws:
      Exception - If setup fails.
    • complete

      default void complete() throws Exception
      Always called after all elements have been processed (like finally).

      Use this to close database connections, statements, or release other resources. This method is called whether parsing succeeded or onError(Exception) rethrew.

      Throws:
      Exception - If cleanup fails.
    • onError

      default void onError(Exception e) throws Exception
      Called when ThrowingConsumer.acceptThrows(Object) throws an exception.

      The default implementation rethrows the exception, stopping parsing immediately. Override to absorb the exception for fault-tolerant ingestion (e.g., log and skip bad records). Note that complete() is always called afterward regardless.

      Parameters:
      e - The exception thrown by ThrowingConsumer.acceptThrows(Object).
      Throws:
      Exception - If the error cannot be recovered from. Rethrow e to stop parsing.