eventsapama

How can I have multiple contexts handle events in Apama


I am trying to define a monitor in which I receieve events and then handle them on multiple contexts (roughly equating to threads if I understand correctly) I know I can write

spawn myAction() to myNewContext; 

and this will run that action in the new context.

However I want to have an action which will respond to an event when it comes into my monitor:

on all trigger() as t {
  doMyThing()
}

on all otherTrigger() as ot {
  doMyOtherThing()
}

Can I define my on all in a way that uses a specific context? Something like

on all trigger() as t in myContext {
  doMyThing()
}

on all otherTrigger() as t in myOtherContext {
  doMyOtherThing()
}

If not what is the best way to define this in Apama EPL? Also could I have multiple contexts handling the same events when they arrive, round robin style?


Solution

  • Apama events from external receivers (ie the outside world) are delivered only to public contexts, including the 'main' context. So depending on your architecture, you can either spawn your action to a public context

    // set the receivesInput parameter to true to make this context public
    spawn myAction() to context("myContext", true);
    
    ...
    
    action myAction() {
        on all trigger() as t {
            doMyThing();
        }
    }
    

    or, spawn your action to a private context and set up an event forwarder in a public context, usually the main context (which will always exist)

    spawn myAction() to context("myNewContext");
    on all trigger() as t {
        send t to "myChannel"; // forward all trigger events to the "myChannel" channel
    }
    
    ...
    
    action myAction() {
        monitor.subscribe("myChannel"); // receive all events delivered to the "myChannel" channel
        on all trigger() as t {
            doMyThing();
        }
    }
    

    Spawning to a private context and leveraging the channels system is generally the better design as it only sends events to contexts that care about them