pub trait Lifecycle: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn start<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), CamelError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn stop<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), CamelError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn as_metrics_collector(&self) -> Option<Arc<dyn MetricsCollector>> { ... }
fn status(&self) -> ServiceStatus { ... }
}Expand description
Lifecycle trait for background services.
This trait follows Apache Camel’s Service pattern but uses a different name to avoid confusion with tower::Service which is the core of rust-camel’s request processing.
§Why &mut self?
The start() and stop() methods require &mut self to ensure:
- Exclusive access: Prevents concurrent start/stop operations on the same service
- Safe state transitions: Services can safely mutate their internal state
- No data races: Compile-time guarantee of single-threaded access to service state
This design choice trades flexibility for safety - services cannot be started/stopped concurrently, which simplifies implementation and prevents race conditions.
Required Methods§
Provided Methods§
Sourcefn as_metrics_collector(&self) -> Option<Arc<dyn MetricsCollector>>
fn as_metrics_collector(&self) -> Option<Arc<dyn MetricsCollector>>
Optional: expose MetricsCollector for auto-registration
Sourcefn status(&self) -> ServiceStatus
fn status(&self) -> ServiceStatus
Current status of the service.