Apama 10.3.1 | Apama Documentation | Developing Apama Applications | Developing Apama Applications in Java | Overview of Apama JMon Applications | About event listeners and match listeners | Example of a MatchListener
 
Example of a MatchListener
The following example illustrates this functionality:
import com.apama.jmon.*;
public class Simple implements Monitor, MatchListener {
 
   /**
   * No argument constructor used by the jmon framework on
   * application loading
   */
   public Simple() {}
   /**
   * Implementation of the Monitor interface onLoad method. Sets up
   * a single event expression looking for all Tick events
   * with a trade price of greater than 10.0. This class instance
   * is added as a match listener to the event expression.
   */
   public void onLoad() {
     EventExpression eventExpr = new EventExpression("Tick(*, >10.0)");
     eventExpr.addMatchListener(this);
   }
 
   /**
   * Implementation of the MatchListener interface match method.
   * Prints out
   * a message when the listener triggers
   */
   public void match(MatchEvent event) {
     System.out.println("Pattern detected");
   }
}
This example illustrates several new concepts.
Consider the onLoad method. Firstly it creates an event expression object variable. This object, of type com.apama.jmon.EventExpression, represents an event, or sequence of events, to look for. The constructor of an EventExpression is passed a string that defines the actual event expression.
As the syntax of an event expression will be illustrated in the next section it is enough to say that this event expression is specifying "the first Tick event whose price parameter is greater than the value 10.0".
Then, a match listener is registered with the newly created event expression object. A match listener can be any object that implements the com.apama.jmon.MatchListener interface and defines the match(MatchEvent event) method. For the sake of simplicity, the Simple monitor class has here been written to also implement the MatchListener interface, and therefore the statement,
eventExpr.addMatchListener(this);
is passing this as the reference to a suitable MatchListener.
Once a match listener has been registered with an event expression the correlator creates a listener entity to start looking for the specified event expression.
Listeners are asynchronous. Hence the match method may be invoked at any time subsequent to the activation of the listener, but always after all Java code in the current method finishes executing. Therefore in this case all Java statements in the onLoad method would finish being executed before match is called after a match.

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.