Apama 10.7.2 | Developing Apama Applications | Developing Apama Applications in EPL | Common EPL Patterns in Monitors | Reference counting
 
Reference counting
The following pattern is another example that you can use to keep a count of how many clients are using a particular service object, which in turn can be used to determine the lifetime of these service objects. The example subscription management mechanism is fairly sophisticated, possibly too sophisticated, but it provides the big advantage of separating the concerns by using two monitors. If you decide to change the subscription mechanism, you can do so simply by changing the ServiceManager monitor. There is no impact at all on the ServiceItem monitor.
The events:
package com.apamax.service;
event Subscribe {
   string toWhat;
   string originator;
}
event Unsubscribe {
   string fromWhat;
   string originator;
}
event CreateServiceItem {
   string what;
}
event DestroyServiceItem {
   string what;
}
The monitors:
monitor ServiceManager {
   dictionary <string, dictionary<string, integer>> items;
 
   action onload() {
      on all Subscribe() as s subscribe(s);
      on all Unsubscribe() as u unsubscribe(u);
   }

action subscribe(Subscribe s){  
dictionary < string, integer > subscriptions:= {};
if items.hasKey(s.toWhat) {
subscriptions :=
items[s.toWhat];
if subscriptions.hasKey(s.originator) {
subscriptions[s.originator] :=
subscriptions[s.originator] + 1;
         }
         else {
            subscriptions[s.originator] := 1;
         }
      }
      else {
         items[s.toWhat] := subscriptions;
         route CreateServiceItem(s.toWhat);
      }
   }
 
   action unsubscribe(Unsubscribe u) {
      if items.hasKey(u.fromWhat) {
         dictionary < string, integer > subscriptions :=
            items[u.fromWhat];
         if subscriptions.hasKey(u.originator) {
            if subscriptions[u.originator] <= 1 {
               subscriptions.remove(u.originator);
               if subscriptions.size() = 0 {
                  items.remove(u.fromWhat);
                  route DestroyServiceItem(u.fromWhat);
               }
            }
            else {
               subscriptions[u.originator] :=
                  subscriptions[u.originator] - 1;
            }
         }
         else {
            print "Unsubscribe failed: no originator: " +
               u.toString();
         }
      }
      else {
         print "Unsubscribe failed: no item: " + u.toString();
      }
   }
}
 
monitor ServiceItem {
   //...
 
   action onload() {
      on all CreateServiceItem() as c spawn createServiceItem(c);
   }
 
   action createServiceItem(CreateServiceItem c) {
   //...
      on all DestroyServiceItem() as d destroyServiceItem(d);
   }
 
   action destroyServiceItem(DestroyServiceItem d) {
   //...die;
   }
}