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
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:
- Calls
begin()before the first element - Calls
ThrowingConsumer.acceptThrows(Object)for each parsed element - If
ThrowingConsumer.acceptThrows(Object)throws, callsonError(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
- If
- Always calls
complete()after all elements (likefinally) — whether parsing succeeded oronError()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):
See Also:
BeanSupplier- Serialize-only lifecycle interfaceBeanChannel- Round-trip lifecycle interface (extends both)BeanFactory- Universal factory for DI framework integration- Marshalling
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidbegin()Called before the first element is accepted.default voidcomplete()Always called after all elements have been processed (likefinally).default voidCalled whenThrowingConsumer.acceptThrows(Object)throws an exception.Methods inherited from interface org.apache.juneau.commons.function.ThrowingConsumer
accept, acceptThrows
-
Method Details
-
begin
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
Always called after all elements have been processed (likefinally).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
Called whenThrowingConsumer.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 byThrowingConsumer.acceptThrows(Object).- Throws:
Exception- If the error cannot be recovered from. Rethroweto stop parsing.
-