Apama 10.7.2 | Developing Apama Applications | Developing Apama Applications in EPL | Defining Event Listeners | Specifying completion event listeners
 
Specifying completion event listeners
 
Example using unmatched and completed
In some situations, you want to ensure that the correlator completes all work related to a particular event before your application performs some other work. In your event template, specify the completed keyword to accomplish this. For example:
on all completed A(f < 10.0) {}
Suppose an A event whose f field value is less than 10 arrives in the correlator. What happens is as follows:
1. If there are normal or unmatched event listeners whose event expression matches this A event, those event listeners trigger.
2. The correlator executes listener actions and then processes any routed events that result from those actions, and any routed events that result from processing the routed events, and so on until all routed events have been processed.
3. The completed event listener triggers.
A common situation in which the completed keyword is useful is when a piece of data comes into the system and that piece of data causes a cascade of event listeners to trigger. Each listener action updates some data. When all listener actions have been executed, you want to take a survey of the new state of things and do something in response.
For example, consider a pricing engine made up of many individual pricing engines. When a new piece of market data arrives all pricing engines update their prices and then the controller uses some metric to pick the best price, which it publishes. The controller should publish the new price only after all individual engines have updated their output. The controller achieves this by listening for all the updates but only publishing when the market data event causes the completed event listener to trigger. The EPL for this scenario follows.
// Request/return best price from *all* markets
event RequestSmartBestPrice{ string stock; integer id; }
event BestSmartPriceReply{ integer id; float price; }
 
//Request/return best price from individual market(s)
event RequestBestPrice{ string stock; integer id; }
event BestPriceReply{ integer id; float price; }
 
// Simple example: Assume 'best' is 'lowest' and no account
// is taken of 'side'.
monitor SmartPriceGetter {
RequestSmartBestPrice request;
sequence< float > prices;
 
action onload() {
on all RequestSmartBestPrice(*,*):request spawn getPrices();
}
 
action getPrices() {
on all BestPriceReply( request.id, * ) as reply
prices.append(reply.price);
on completed RequestSmartBestPrice( request.stock, request.id ) {
prices.sort();
route BestSmartPriceReply( request.id, prices[0]);
die;
}
      route RequestBestPrice( request.stock, request.id );
   }
}
You can use the completed keyword with an any() listener as shown below:
on completed any() {
print "A completed any() listener triggered!";
}
A completed any() event template will match against all events in the context once they are processed, regardless of their type.