Apama Documentation : Developing Apama Applications : Developing Apama Applications in EPL : Defining Event Listeners : Terminating and changing event listeners
Terminating and changing event listeners
After the correlator creates an event listener, you cannot change it. Instead of changing an event listener, you terminate it and create a new one.
The example in Listening for events of different types looks for only one StockChoice event. The monitor would be more useful if it continued looking for subsequent StockChoice events, and on every new StockChoice event it changed the second event listener to look for stock ticks for the new company.
When the correlator creates an event listener, it copies from the action the value of any local variables. However, if the variable is of a reference type, changes to the object referred to by the value are seen by other listeners.
The steps and example below shows how to terminate an event listener with the quit() operation. See also, Specifying and not logic to terminate event listeners.
When you want to change an event listener, do the following:
1. Obtain a handle to the event listener you want to change.
2. Terminate that event listener with the quit() operation.
3. Create a new event listener to take its place.
The following sample monitor does just this.
// Definition of a type of event that the correlator will receive.
// These events represent stock ticks from a market data feed.
event StockTick {
   string name;
   float price;
}
 
// Definition of a type of event that describes the stock to process.
// These events come from a second live data feed.
event StockChoice {
   string name;
}
 
// The following simple monitor listens for two different event types.
 
monitor SimpleShareSearch {
   // A global variable to store the matching StockTick event:
   StockTick newTick;
 
   // A global variable to store the StockChoice event:
   StockChoice currentStock;
 
   // A handle to the second listener:
   listener l;
 
   // Record the latest StockChoice event and use its name field
   // to filter the StockTick events.
   action onload() {
     on all StockChoice(*):currentStock {
      l.quit();
      l := on all StockTick(currentStock.name, *):newTick processTick();
     }
   }
 
   action processTick() {
     log "StockTick event received" +
       " name = " + newTick.name +
       " Price = " + newTick.price.toString() at INFO;
   }
}
The differences between the example in Listening for events of different types and this example are as follows:
*The monitor in this example declares an additional global variable, l, whose type is listener.
*The initial on statement now specifies the all operator. After this event listener finds a StockChoice event, it watches for the next StockChoice event.
*The onload() action specifies a new listener action. Each time the first event listener finds a StockChoice event, the listener action:
*Terminates the second event listener by calling the l.quit() method. Of course, upon finding the first StockChoice event there is no second event listener to terminate. This is not a problem as in this case the l.quit() method does not do anything.
*Creates a new event listener to seek StockTick events for the company named in the StockChoice event just detected.
*Stores a handle to the new event listener in the l global variable. The first event listener uses this handle when it needs to terminate the second event listener.
Copyright © 2013-2016 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback