Apama 10.15.0 | Developing Apama Applications | Developing Apama Applications in EPL | Defining Event Listeners | Specifying the on statement
 
Specifying the on statement
You specify an on statement to define an event listener. The format of an on statement is as follows:
[listener identifier := ] on event_expr [ coassignment ] listener_action;
Syntax description
Syntax Element
Description
identifier
Optionally, you can specify a variable of type listener and assign the new event listener to that variable. This gives you a handle to the event listener — if you want to terminate it you can call the quit() method on the listener.
event_expr
The event expression identifies the event or pattern of events that you want to match. An event expression is made up of one or more event templates and zero or more event operators.
coassignment
Optionally, you can coassign the matching event to a variable. Use the as operator to implicitly declare the variable in the scope of the following listener_action, or the : assignment operator to coassign to a local variable or monitor variable of the same event type. Coassignments are part of event templates. Each event template can have a coassignment, so there can be multiple coassignments in a listener.
Note: 
Coassignment is not always possible for all operations. For more details, see Specifying the 'or' or 'xor' operator in event expressions.
listener_action
The statement or block that you want the correlator to perform when the event listener triggers.
Examples
In the following example, the event expression contains one event template: StockTick(*,*). The asterisks indicate that the values of the StockTick event's two fields are not relevant when matching. When this event listener detects a StockTick event, the listener triggers and causes the correlator to execute the processTick() listener action.
on StockTick(*,*) processTick();
Following is an example that implicitly declares the newTick variable in the scope of the listener_action.
on StockTick(*,*) as newTick {
processTick(newTick);
}
Following is an example that explicitly coassigns the matching event to the newTick variable. The newTick variable must be a StockTick event type variable. Coassignment simply assigns the event to the variable.
on StockTick(*,*):newTick processTick();
The next example begins with the declaration of a listener variable. The statement assigns the event listener to the l variable.
listener l := on StockTick(*,*) as newTick processTick();
Suppose that after finding a matching event, the listener action includes specification of an on statement. For example:
listener l := on StockTick(*,*) as newTick {
on StockTick(newTick.symbol, > newTick.value) as risingTick {
processRisingTick();
}
}
The correlator creates an entirely new event listener to handle the nested on statement. This new event listener is completely independent of the enclosing event listener. For example, the enclosing event listener does not wait for the nested event listener to find a matching event.