Apama 10.3.1 | Apama Documentation | Developing Apama Applications | Developing Apama Applications in EPL | Defining What Happens When Matching Events Are Found | Using variables | Using variables in listener actions
 
Using variables in listener actions
Suppose you use a local variable in a listener action, as in the following example:
monitor MyMonitor {
   
   integer x;
  
   action onload() {
     integer y := 10;
     on all StockTick(*,*) {
       log x.toString();
       log y.toString();
     }
     y := 5;
   }
}
In this example, x is a global variable, and y is a local variable. There are references to both variables in the listener action.
A reference to a global variable in a listener action is the same as a reference to a global variable anywhere else in the monitor. However, a reference to a local variable in a listener action causes the correlator to retain a copy of the local variable for use when the event listener triggers. The value held by this copy is the value that the local variable has when the correlator instantiates the event listener.
When the event listener triggers the correlator executes the listener action. This will be at some point in the future, and after the rest of the body of the enclosing action has been executed. Since the action has already been executed, any of the original local variables no longer exist. This is why the correlator retains a copy of the local variable to make available to the listener action when it is executed.
In the example above, when the event listener triggers and the correlator executes the listener action
*x has a value of 0, which is the value that the correlator automatically assigns
*y has a value of 10, which is the value it was set to when the event listener was instantiated
The value of y that the correlator retained when it instantiated the event listener is not affected by the subsequent statement (after the on statement) that sets the value of y to 5.
Note: For reference types (see also Reference types), retaining as a copy of the variable really means only retaining as a copy of its reference. Hence, if any code changes the contents of the referenced object(s) between event listener creation and event listener triggering, then this does affect the values used by the triggered event listener.

Copyright © 2013-2019 | 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.