Package org.apache.juneau.commons.function
Large-Dataset Streaming
The following interfaces enable serialization and parsing of large datasets without loading all elements into memory. They integrate directly into the Juneau marshalling framework so any serializer or parser works transparently.
BeanSupplier- Serialize-side lifecycle interface; extendsIterableBeanConsumer- Parse-side lifecycle interface; extendsThrowingConsumerBeanChannel- Round-trip interface; extends bothBeanSupplierandBeanConsumerListBeanChannel- Built-in in-memoryBeanChannelbacked by anArrayListBeanFactory- Universal factory interface for DI-framework-managed instantiation
Lifecycle
Both interfaces follow the same three-phase lifecycle driven by the framework:
| Phase | BeanSupplier (serialization) | BeanConsumer (parsing) |
|---|---|---|
| Setup | BeanSupplier#begin() — open cursor, execute query | BeanConsumer#begin() — open connection, prepare statement |
| Transfer | BeanSupplier#iterator() — yield one bean per call | BeanConsumer#acceptThrows(Object) — receive one bean per call |
| Error | BeanSupplier#onError(Exception) — rollback / log; rethrow to stop | BeanConsumer#onError(Exception) — rollback / log; absorb to skip-and-continue |
| Cleanup | BeanSupplier#complete() — close cursor, connection | BeanConsumer#complete() — final commit, close statement |
complete() is always called — even when onError() rethrows — so it is safe to
use for resource cleanup in all cases.
BeanSupplier — Serializing from a database cursor
The following example streams a large Employee table directly from the database to the
HTTP response as a JSON array, without loading any rows into memory. A Spring-injected
DataSource is supplied via a factory registered with BeanFactory.
Bean class with factory annotation
Spring REST endpoint
The serializer calls begin() before iterating, serializes each Employee bean
as it arrives from the cursor, and always calls complete() afterward to close the
cursor and connection — regardless of whether serialization succeeded or failed.
BeanConsumer — Parsing into a database table
The following example accepts a large JSON array of Employee beans in an HTTP request
body and bulk-inserts them into the database via JDBC, committing every 500 rows. On error
it rolls back and rethrows; on completion it performs a final commit and closes resources.
BeanConsumer with batch commits
Parsing via parseToBeanConsumer
Spring REST endpoint
The onError() default rethrows, stopping parsing immediately. Override it to absorb
the exception (log-and-skip) for fault-tolerant ingestion of partially-invalid input:
Fault-tolerant ingestion (skip bad records)
BeanConsumer<Employee>
BeanChannel — Round-trip marshalling on a single property
BeanChannel extends both
BeanSupplier and
BeanConsumer, allowing the same property to drive
both serialization and parsing. The implementation itself determines direction at runtime.
In-memory channel — ListBeanChannel
Database-backed channel
Spring Integration via BeanFactory and BeanStore
Spring-managed beans (e.g. a DataSource) can be injected into streaming implementations
without constructor scanning or reflection. Register the factories as Spring beans, then wire
the SpringBeanStore into the Juneau context so the marshaller can resolve them at
runtime.
Spring configuration
Wiring the SpringBeanStore into a REST resource
With this wiring in place, any class annotated with
@BeanType(factory=EmployeeSupplier.Factory.class) is automatically instantiated by
retrieving the factory from the Spring ApplicationContext and calling
BeanFactory#create(). No manual construction or
injection is needed in individual REST methods.
Supporting Annotations
| Annotation | Target | Purpose |
|---|---|---|
@BeanType(factory=X.class) |
Class | Specifies the BeanFactory class used to instantiate this type during parsing. The factory is resolved from the BeanStore. |
@BeanProp(factory=X.class) |
Bean property | Specifies a property-level factory for instantiating the value of a specific bean property. |
@BeanProp(elementType=Y.class) |
Bean property | Declares the element type for generic streaming properties (Stream<Y>, BeanSupplier<Y>, etc.) overcoming Java type erasure. Also supports narrowing to concrete implementation types. |
Supplier<T> single-value unwrapping
Supplier<T> (the standard JDK interface) is treated as a
single-value lazy wrapper. Serializers call get() and serialize the result
transparently. Nested Supplier chains are unwrapped recursively up to a depth of 10.
Note: BeanSupplier is not unwrapped — it
is treated as an Iterable sequence, not a single-value wrapper.
-
ClassDescriptionBeanChannel<T>A lifecycle-aware round-trip interface that combines both
BeanSupplier(for serialization) andBeanConsumer(for parsing) on the same property or class.BeanConsumer<T>A lifecycle-aware consumer for use as a parser target, receiving deserialized beans one at a time.BeanFactory<T>A universal factory interface used with@Marshalled (factory=X.class) for any bean type the framework needs to instantiate.Sentinel class used as the default value for@BeanType(factory=...)and@BeanProp(factory=...)when no factory is specified.BeanSupplier<T>A lifecycle-aware producer for use as a serializer source, providing beans lazily one at a time.Consumer2<A,B> A functional interface representing an operation that accepts two arguments and returns no result.Consumer3<A,B, C> A functional interface representing an operation that accepts three arguments and returns no result.Consumer4<A,B, C, D> A functional interface representing an operation that accepts four arguments and returns no result.Consumer5<A,B, C, D, E> A functional interface representing an operation that accepts five arguments and returns no result.Function2<A,B, R> A functional interface representing a function that accepts two arguments and produces a result.Function3<A,B, C, R> A functional interface representing a function that accepts three arguments and produces a result.Function4<A,B, C, D, R> A functional interface representing a function that accepts four arguments and produces a result.Function5<A,B, C, D, E, R> A functional interface representing a function that accepts five arguments and produces a result.A simple in-memoryBeanChannelimplementation backed by anArrayList.Memoizer<T>A thread-safe supplier that caches the result of the first call and supports resetting the cache.ASupplierwhose value may be null, with Optional-like null-safe query methods.A functional interface identical toRunnablebut allows theSnippet.run()method to throw checked exceptions.Supplier factories: memoizing wrappers moved from the formerUtils.A functional interface representing an operation that accepts a single argument, returns no result, and may throw a checked exception.ThrowingConsumer2<A,B> A functional interface representing an operation that accepts two arguments, returns no result, and may throw a checked exception.ThrowingConsumer3<A,B, C> A functional interface representing an operation that accepts three arguments, returns no result, and may throw a checked exception.ThrowingConsumer4<A,B, C, D> A functional interface representing an operation that accepts four arguments, returns no result, and may throw a checked exception.ThrowingConsumer5<A,B, C, D, E> A functional interface representing an operation that accepts five arguments, returns no result, and may throw a checked exception.ThrowingFunction<T,R> A functional interface representing a function that accepts one argument, produces a result, and may throw a checked exception.ThrowingFunction2<A,B, R> A functional interface representing a function that accepts two arguments, produces a result, and may throw a checked exception.ThrowingFunction3<A,B, C, R> A functional interface representing a function that accepts three arguments, produces a result, and may throw a checked exception.ThrowingFunction4<A,B, C, D, R> A functional interface representing a function that accepts four arguments, produces a result, and may throw a checked exception.ThrowingFunction5<A,B, C, D, E, R> A functional interface representing a function that accepts five arguments, produces a result, and may throw a checked exception.A functional interface representing a supplier of results that may throw a checked exception.Tuple1<A>Represents an immutable tuple containing a single value.Tuple2<A,B> Represents an immutable tuple containing two values.Tuple3<A,B, C> Represents a simple tuple of 3 objects.Tuple4<A,B, C, D> Represents a simple tuple of 4 objects.Tuple5<A,B, C, D, E> Represents a simple tuple of 5 objects.