Apama 10.3 | Apama Documentation | Developing Apama Applications | Developing Apama Applications in EPL | Common EPL Patterns in Monitors | Using quit() to terminate event listeners
Using quit() to terminate event listeners
The example below demonstrates the use of quit() to terminate an event listener. This example is somewhat contrived in order to demonstrate a situation where it might be desirable to use quit(). Typically, other methods are often more appropriate, for example, you can use die to kill a monitor instance and you can specify and not to terminate an event listener.
The example shows a monitor that trades received orders by breaking them into smaller orders, which it might place concurrently (perhaps on several exchanges). The monitor listens for fills on these orders, and sums up the fills. (A real monitor might also send status on what the filled volume is for each child order together with the total volume filled for the order. The logic for this is not shown here.) When each order is completely filled the monitor terminates the Trade event listener for that order.
The events:
event OrderIn {integer id; ... }
event OrderOut {integer id; integer volume; ... }
event Trade {integer orderOutId; integer volume; ... }
The monitor:
monitor TradeOrderAsSeveralSmallerOrders {
   event PlacedOrderRecord {
      listener listener;
      integer volumeToTrade;
      integer volumeTraded;
   dictionary < integer, PlacedOrderRecord > records;
   action onload() {
      on all OrderIn() as theOrder spawn tradeOrder();
   action tradeOrder() {
      // some logic determining when and what volume to trade
      placeOrder( volume ); //called multiple times
   action placeOrder(integer volume) {
      PlacedOrderRecord r := new PlacedOrderRecord;
      integer id := integer.incrementCounter("orderId");
      r.listener := on all Trade(orderOutId=id) as t
      records[id] := r;
      r.volumeToTrade := volume;
      route OrderOut(id,volume,...);
   action processTrade(Trade t) {
      PlacedOrderRecord r := records[t.orderOutId];
      r.volumeTraded := r.volumeTraded + t.volume;
      if (r.volumeToTrade - r.volumeTraded) <= 0 {
As stated earlier, for real-world solutions there is generally a better option that using quit(). For example, the exchange(s) probably also send OrderComplete events. In this case you can change the on statement as follows:
on all Trade(orderOutId=id) as t and not OrderComplete(orderOutId=id)
Of course, you must be certain that the OrderComplete event can be received only after all trades for that order have been received.

Copyright © 2013-2018 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.