Path: redb-route/redb.Route/Definitions/SplitMulticastDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/SplitMulticastDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Expressions;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Scope-opener definition for the Splitter EIP.
/// Splits an exchange into sub-exchanges via a splitter function,
/// then passes each sub-exchange through the child pipeline (Outputs).
/// Inherits the leaf DSL from ;
/// close with .
///
public class SplitDefinition : RouteDefinitionBase, IRouteScope, ICompositeScope
{
private readonly Func>? _splitter;
private readonly Func>? _asyncSplitter;
private bool _parallel;
private bool _stopOnException = true;
private TimeSpan _timeout;
private int _maxDegreeOfParallelism;
private Func? _aggregationStrategy;
/// Captured source when the splitter was built from an expression instance; null otherwise.
public IExpression? SourceExpression { get; }
internal SplitDefinition(Func> splitter)
{
ArgumentNullException.ThrowIfNull(splitter);
_splitter = splitter;
}
internal SplitDefinition(IExpression expression)
: this(ex =>
{
var val = expression.Evaluate(ex);
return val switch
{
IEnumerable e => e,
System.Collections.IEnumerable e => e.Cast(),
_ => throw new InvalidOperationException(
$"Expression did not evaluate to an enumerable; got {val?.GetType().Name ?? "null"}."),
};
})
{
SourceExpression = expression;
}
internal SplitDefinition(Func> asyncSplitter)
{
ArgumentNullException.ThrowIfNull(asyncSplitter);
_asyncSplitter = asyncSplitter;
}
// ── Options ────────────────────────────────────────────────────────────────
/// Enables parallel processing of split parts.
public SplitDefinition Parallel(bool value = true) { _parallel = value; return this; }
/// Apache Camel alias for .
public SplitDefinition ParallelProcessing(bool value = true) => Parallel(value);
/// Sets stop-on-exception behaviour (default: true).
public SplitDefinition StopOnException(bool value = true) { _stopOnException = value; return this; }
/// Sets maximum degree of parallelism (0 = cpu count).
public SplitDefinition MaxParallelism(int value) { _maxDegreeOfParallelism = value; return this; }
/// Sets a timeout for the entire split operation.
public SplitDefinition Timeout(TimeSpan timeout) { _timeout = timeout; return this; }
/// Sets the pair-wise aggregation strategy.
public SplitDefinition AggregationStrategy(Func strategy)
{
_aggregationStrategy = strategy;
return this;
}
// ── Navigation ─────────────────────────────────────────────────────────────
/// Closes this split scope and returns the parent route definition.
public IRouteDefinition EndSplit()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndSplit() called without a parent route."));
///
public IRouteDefinition End() => EndSplit();
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
IProcessor body = NodePipeline.Body(context, Outputs);
if (_asyncSplitter is not null)
return new StreamingSplitterProcessor(_asyncSplitter, body, _stopOnException);
return new SplitterProcessor(
_splitter!,
body,
parallelProcessing: _parallel,
maxDegreeOfParallelism: _maxDegreeOfParallelism,
aggregationStrategy: _aggregationStrategy,
stopOnException: _stopOnException,
timeout: _timeout);
}
}
///
/// Scope-opener definition for the Multicast EIP.
/// Sends the exchange to all child endpoints (fan-out).
/// Each output is compiled as one target processor for .
/// Inherits the leaf DSL from ;
/// close with .
///
public class MulticastDefinition : RouteDefinitionBase, IRouteScope, ICompositeScope
{
private bool _parallel = true;
private bool _stopOnException;
private TimeSpan _timeout;
private int _maxDegreeOfParallelism;
private Func? _aggregationStrategy;
internal MulticastDefinition() { }
// ── Options ────────────────────────────────────────────────────────────────
/// Sets sequential processing (parallel = false).
public MulticastDefinition Sequential() { _parallel = false; return this; }
/// Enables parallel processing (default).
public MulticastDefinition Parallel(bool value = true) { _parallel = value; return this; }
/// Apache Camel alias for .
public MulticastDefinition ParallelProcessing(bool value = true) => Parallel(value);
/// Sets stop-on-exception behaviour (default: false).
public MulticastDefinition StopOnException(bool value = true) { _stopOnException = value; return this; }
/// Sets maximum degree of parallelism.
public MulticastDefinition MaxParallelism(int value) { _maxDegreeOfParallelism = value; return this; }
/// Sets a timeout for the entire multicast operation.
public MulticastDefinition Timeout(TimeSpan timeout) { _timeout = timeout; return this; }
/// Sets the pair-wise aggregation strategy.
public MulticastDefinition AggregationStrategy(Func strategy)
{
_aggregationStrategy = strategy;
return this;
}
// ── Navigation ─────────────────────────────────────────────────────────────
/// Closes this multicast scope and returns the parent route definition.
public IRouteDefinition EndMulticast()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndMulticast() called without a parent route."));
///
public IRouteDefinition End() => EndMulticast();
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
var multicast = new MulticastProcessor(
parallelProcessing: _parallel,
aggregationStrategy: _aggregationStrategy,
stopOnException: _stopOnException,
timeout: _timeout,
maxDegreeOfParallelism: _maxDegreeOfParallelism);
foreach (var output in Outputs)
multicast.AddTarget(NodePipeline.Node(context, output));
return multicast;
}
}