camel-bean
The camel-bean crate provides a bean/registry system for rust-camel, enabling dependency injection and business logic integration with routes defined in YAML DSL or Rust code.
Overview
The bean system allows you to:
- Define business logic as structured beans with typed methods
- Register beans in a central registry for lookup by name
- Invoke bean methods from routes with automatic parameter binding
- Integrate seamlessly with YAML DSL via the
beanstep type
This enables clean separation between route configuration and business logic, similar to Spring beans in Apache Camel.
Quick Start
- Add
camel-beanto yourCargo.toml:
[]
= "0.2.2"
= "0.2.2"
- Define a bean with handlers:
use ;
;
- Register and use the bean:
use BeanRegistry;
let mut registry = new;
registry.register;
// Invoke from code
registry.invoke.await?;
- Use in YAML DSL:
routes:
- id: "process-order"
from: "direct:orders"
steps:
- bean:
name: "orderService"
method: "process"
API Reference
BeanRegistry
The central registry for storing and retrieving beans.
use ;
let mut registry = new;
// Register a bean
registry.register;
// Get a bean
if let Some = registry.get
// Invoke a method directly
registry.invoke.await?;
// Registry information
registry.len // Number of registered beans
registry.is_empty // Whether registry is empty
BeanProcessor Trait
The trait that all beans must implement. The #[bean_impl] macro handles this automatically.
use async_trait;
use ;
use BeanProcessor;
Attributes
#[bean_impl]
Applied to an impl block to generate the BeanProcessor implementation:
#[handler]
Applied to methods within a #[bean_impl] block to make them invocable:
Parameter Binding
The bean system supports automatic parameter binding from the exchange to handler methods:
Supported Parameter Types
| Parameter Type | Source | Description |
|---|---|---|
body: T |
Message body | Deserialized from JSON if needed |
headers: Headers |
Exchange headers | All headers as a struct |
exchange: &mut Exchange |
Full exchange | Complete exchange object |
Example with Multiple Parameters
use ;
use Headers;
Error Handling
The bean system provides comprehensive error handling:
BeanError
use BeanError;
Error Conversion
BeanError automatically converts to CamelError:
Handler Return Types
Handlers can return:
Result<T, E>whereE: Display- for successful/failed processingT- simple successful response- Any type that implements
serde::Serializefor JSON response
Integration with camel-core
The bean system integrates with the core route controller:
use CamelContext;
use BeanRegistry;
let mut ctx = new;
let bean_registry = new;
// Register beans
bean_registry.register;
// Set in context
ctx.set_bean_registry;
This enables:
- Bean invocation from YAML DSL routes
- Bean discovery and management
- Integration with route lifecycle
Examples
See the following examples for complete usage:
examples/bean-demo- Comprehensive bean usage demonstrationexamples/yaml-dsl- YAML DSL integration example
Best Practices
- Use structured types: Define clear request/response types for handlers
- Error handling: Return descriptive error messages from handlers
- Validation: Validate input parameters before processing
- Async operations: Leverage async I/O for external service calls
- Bean naming: Use consistent naming conventions for beans
Type Safety
The bean system provides compile-time type safety:
- Handler signatures are validated at compile time
- Parameter binding is checked for supported types
- Return types must be serializable
- Method names are checked during route parsing
Performance Considerations
- Bean lookup is O(1) via HashMap
- Parameter binding uses zero-copy where possible
- JSON deserialization only when needed
- Minimal runtime overhead after initial registration
Advanced Usage
Dynamic Bean Registration
// Runtime bean registration
let mut registry = new;
// Register different implementations based on config
if cfg.feature_enabled else
Bean Composition
// Beans can use other beans
Future Enhancements
Planned features include:
- Constructor dependency injection
- Lifecycle callbacks
- Bean scopes (singleton, prototype)
- Conditional bean registration
- Integration with service discovery
License
Licensed under the same terms as rust-camel.