pub trait EventHandler<E: DomainEvent>: Send + Sync {
// Required methods
fn handle<'life0, 'async_trait>(
&'life0 self,
envelope: EventEnvelope<E>,
) -> Pin<Box<dyn Future<Output = Result<(), EventError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn event_types(&self) -> Vec<&'static str>;
// Provided methods
fn name(&self) -> &'static str { ... }
fn should_retry(&self) -> bool { ... }
fn max_retries(&self) -> u32 { ... }
}Expand description
Trait for event handlers
Event handlers process domain events asynchronously. They can be used for side effects like sending notifications, updating read models, or triggering workflows.
§Example
ⓘ
use backbone_messaging::{EventHandler, EventEnvelope, EventError, DomainEvent};
use async_trait::async_trait;
struct EmailNotificationHandler;
#[async_trait]
impl<E: DomainEvent> EventHandler<E> for EmailNotificationHandler {
async fn handle(&self, envelope: EventEnvelope<E>) -> Result<(), EventError> {
// Send email notification
println!("Event {} occurred for {}", envelope.event_type, envelope.aggregate_id);
Ok(())
}
fn event_types(&self) -> Vec<&'static str> {
vec!["UserCreated", "OrderPlaced"]
}
}Required Methods§
Sourcefn handle<'life0, 'async_trait>(
&'life0 self,
envelope: EventEnvelope<E>,
) -> Pin<Box<dyn Future<Output = Result<(), EventError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle<'life0, 'async_trait>(
&'life0 self,
envelope: EventEnvelope<E>,
) -> Pin<Box<dyn Future<Output = Result<(), EventError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handle a domain event
This method is called when an event matching one of the types
returned by event_types() is published.
Sourcefn event_types(&self) -> Vec<&'static str>
fn event_types(&self) -> Vec<&'static str>
Event types this handler is interested in
Return an empty slice to receive all events.
Provided Methods§
Sourcefn should_retry(&self) -> bool
fn should_retry(&self) -> bool
Whether this handler should be retried on failure
Sourcefn max_retries(&self) -> u32
fn max_retries(&self) -> u32
Maximum retry attempts
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".