Skip to main content

coreon_eip/
recipient_list.rs

1//! RecipientList — compute recipient URIs from the exchange at runtime and
2//! send to each, sequentially.
3
4use async_trait::async_trait;
5use coreon_core::{CamelContext, Exchange, Processor, Result};
6use std::sync::{Arc, Weak};
7
8pub type RecipientFn = dyn Fn(&Exchange) -> Vec<String> + Send + Sync;
9
10pub struct RecipientList {
11    recipients: Arc<RecipientFn>,
12    ctx: Weak<CamelContext>,
13}
14
15impl RecipientList {
16    pub fn new<F>(f: F, ctx: &Arc<CamelContext>) -> Arc<Self>
17    where
18        F: Fn(&Exchange) -> Vec<String> + Send + Sync + 'static,
19    {
20        Arc::new(Self {
21            recipients: Arc::new(f),
22            ctx: Arc::downgrade(ctx),
23        })
24    }
25}
26
27#[async_trait]
28impl Processor for RecipientList {
29    async fn process(&self, exchange: &mut Exchange) -> Result<()> {
30        let ctx = self
31            .ctx
32            .upgrade()
33            .ok_or_else(|| coreon_core::CamelError::NotRunning)?;
34        let uris = (self.recipients)(exchange);
35        for uri in uris {
36            let mut copy = exchange.clone();
37            ctx.send(&uri, &mut copy).await?;
38        }
39        Ok(())
40    }
41}