Skip to main content

IntegrationEventHandler

Trait IntegrationEventHandler 

Source
pub trait IntegrationEventHandler: Send + Sync {
    // Required methods
    fn handle<'life0, 'async_trait>(
        &'life0 self,
        envelope: IntegrationEventEnvelope,
    ) -> Pin<Box<dyn Future<Output = Result<(), EventError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn event_patterns(&self) -> Vec<&'static str>;
    fn name(&self) -> &'static str;

    // Provided methods
    fn should_retry(&self) -> bool { ... }
    fn max_retries(&self) -> u32 { ... }
}
Expand description

§Pattern Matching

Handlers specify which events they’re interested in via event_patterns(). Patterns support:

  • Exact match: "sapiens.user.created"
  • Wildcard suffix: "sapiens.user.*" matches all user events
  • Global wildcard: "*" matches all events

§Example

ⓘ
struct EmailNotificationHandler {
    email_service: Arc<EmailService>,
}

#[async_trait]
impl IntegrationEventHandler for EmailNotificationHandler {
    async fn handle(&self, envelope: IntegrationEventEnvelope) -> Result<(), EventError> {
        match envelope.event_type.as_str() {
            "sapiens.user.created" => {
                // Deserialize and send welcome email
                let event: UserCreatedEvent = envelope.deserialize()?;
                self.email_service.send_welcome(&event.email).await?;
            }
            _ => {}
        }
        Ok(())
    }

    fn event_patterns(&self) -> Vec<&'static str> {
        vec!["sapiens.user.*"]
    }

    fn name(&self) -> &'static str {
        "EmailNotificationHandler"
    }
}

Required Methods§

Source

fn handle<'life0, 'async_trait>( &'life0 self, envelope: IntegrationEventEnvelope, ) -> Pin<Box<dyn Future<Output = Result<(), EventError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Handle an integration event envelope

Called when an event matching one of the patterns from event_patterns() is published to the bus.

Source

fn event_patterns(&self) -> Vec<&'static str>

Event patterns this handler is interested in

Patterns support wildcards:

  • "sapiens.user.created" - exact match
  • "sapiens.user.*" - matches all sapiens.user.* events
  • "*" - matches all events
Source

fn name(&self) -> &'static str

Handler name for logging and debugging

Provided Methods§

Source

fn should_retry(&self) -> bool

Whether this handler should be retried on failure

Source

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".

Implementors§