camel-builder
Fluent route builder API for rust-camel
Overview
camel-builder provides a fluent, type-safe API for building routes in rust-camel. Inspired by Apache Camel's Java DSL, it allows you to define integration flows using method chaining with compile-time safety.
This is the primary way to define routes in rust-camel applications.
Routes can also be defined via YAML using camel-dsl. See the camel-dsl crate for details.
Features
- Fluent API: Intuitive method chaining for route definition
- Type Safety: Compile-time verification of route structure
- All EIP Patterns: Support for filter, split, multicast, aggregate, and more
- Error Handling: Configure error handlers per route
- Circuit Breaker: Built-in resilience pattern support
- Concurrency Control: Sequential or concurrent processing modes
- Route Lifecycle: Configure auto-startup and startup order
Installation
Add to your Cargo.toml:
[]
= "0.2"
Usage
Basic Route
use RouteBuilder;
use Value;
let route = from
.set_header
.log
.to
.build
.unwrap;
Filter and Branching
let route = from
.filter
.log
.to
.end_filter
.to
.build
.unwrap;
Content-Based Router (Choice)
use Value;
let route = from
.choice
.when
.to
.end_when
.when
.to
.end_when
.otherwise
.to
.end_otherwise
.end_choice
.build
.unwrap;
Message Transformation
use Body;
let route = from
.set_header
.map_body
.to
.build
.unwrap;
Splitter Pattern
use ;
let route = from
.split
.log
.to
.end_split
.to
.build
.unwrap;
Multicast Pattern
let route = from
.multicast
.parallel
.to
.to
.to
.end_multicast
.to
.build
.unwrap;
Aggregate Pattern
use AggregatorConfig;
use Duration;
// Size-based completion (basic)
let route = from
.aggregate
.to
.build
.unwrap;
// Size OR timeout completion (v2)
let route = from
.aggregate
.to
.build
.unwrap;
AggregatorConfig Builder Methods
| Method | Description |
|---|---|
correlate_by(header) |
Correlate exchanges by header value |
complete_when_size(n) |
Complete when bucket reaches n exchanges |
complete_on_timeout(duration) |
Complete on inactivity timeout (resets on each exchange) |
complete_on_size_or_timeout(n, duration) |
Complete on either condition (first wins) |
force_completion_on_stop(bool) |
Force-complete all buckets when route stops |
discard_on_timeout(bool) |
Discard incomplete exchanges on timeout instead of emitting |
strategy(strategy) |
Custom aggregation strategy (Fn(Exchange, Exchange) -> Exchange) |
max_buckets(n) |
Maximum concurrent correlation buckets |
bucket_ttl(duration) |
TTL for idle buckets |
Script Step
Execute a script that can modify the Exchange in-place (headers, properties, body):
from
.script
.to
.route_id
.build?;
The language must be registered in the CamelContext. On error, all modifications are rolled back.
Error Handling
use ErrorHandlerConfig;
let route = from
.error_handler
.process
.to
.build
.unwrap;
Shorthand route-level exception clauses:
use CamelError;
use Duration;
let route = from
.route_id
.dead_letter_channel
.on_exception
.retry
.with_backoff
.handled_by
.end_on_exception
.on_exception
.retry
.end_on_exception
.to
.build
.unwrap;
Circuit Breaker
use CircuitBreakerConfig;
let route = from
.circuit_breaker
.to
.build
.unwrap;
Concurrency Control
// Concurrent processing (up to 16 in-flight)
let route = from
.concurrent
.process
.build
.unwrap;
// Sequential processing (ordered)
let route = from
.sequential
.process
.build
.unwrap;
Route Configuration
let route = from
.route_id
.auto_startup
.startup_order
.to
.build
.unwrap;
Marshal/Unmarshal
use RouteBuilder;
let route = from
.unmarshal
.marshal
.to
.route_id
.build
.unwrap;
This converts the message body from JSON to XML using the built-in data formats.
Builder Methods
| Method | Description |
|---|---|
from(uri) |
Start route from endpoint |
to(uri) |
Send to endpoint |
process(fn) |
Custom processing |
set_header(k, v) |
Set static header |
set_header_fn(k, fn) |
Set dynamic header |
set_body(v) |
Set static body |
set_body_fn(fn) |
Set dynamic body |
transform(v) |
Alias for set_body |
map_body(fn) |
Transform body |
unmarshal(format) |
Unmarshal body using a data format |
marshal(format) |
Marshal body using a data format |
filter(pred) |
Conditional routing |
choice() |
Open content-based router block |
when(pred) |
Open when-clause (inside choice) |
end_when() |
Close when-clause |
otherwise() |
Open fallback branch (inside choice) |
end_otherwise() |
Close fallback branch |
end_choice() |
Close content-based router block |
split(config) |
Split messages |
multicast() |
Parallel routing |
wire_tap(uri) |
Side-channel copy |
aggregate(config) |
Combine messages |
log(msg, level) |
Log message |
stop() |
Stop processing |
script(language, source) |
Execute a script that can modify headers, properties, and body |
error_handler(cfg) |
Set explicit error handler config |
dead_letter_channel(uri) |
Set DLC for shorthand error mode |
on_exception(pred) |
Open shorthand exception clause |
end_on_exception() |
Close shorthand exception clause |
circuit_breaker(cfg) |
Set circuit breaker |
concurrent(n) |
Enable concurrency |
sequential() |
Force sequential |
route_id(id) |
Set route ID |
auto_startup(bool) |
Auto-start control |
startup_order(n) |
Startup priority |
Documentation
License
Apache-2.0
Contributing
Contributions are welcome! Please see the main repository for details.