Developing Apama Applications > Developing Apama Applications in Java > Defining Event Expressions > Specifying temporal sequencing > Chaining listeners
Chaining listeners
You can chain listeners, as follows:
// Code within the monitor class
 
public void onLoad() {
   EventExpression eventExpr = new EventExpression("NewsItem(*, *)");
   eventExpr.addMatchListener(matchListener1);
}
 
// Code within the first Match Listener class – matchListener1
public void match(MatchEvent event) {
 
   // Arbitrary additional code …
   EventExpression eventExpr = new EventExpression("Tick(*, *)");
   eventExpr.addMatchListener(matchListener2);
}
 
// Code within the second Match Listener class – matchListener2
 
public void match(MatchEvent event) {
   System.out.println("Detected a NewsItem followed"
     + " by a Tick event, both regarding any company.");
}
The Java code above shows how to set up a listener to seek the first event, and then once that is located, start searching for the second. This programming style is particularly appropriate when further actions need to be taken at each stage of the event detection, in this case between detecting the NewsItem and seeking the Tick.
It is also the only way in which the event templates can be ‘linked’ together. If the desired effect was to locate ‘any’ first NewsItem and then seek a Tick specifically for the same company mentioned in the NewsItem, you could amend the example as follows,
// Code within the monitor class
public void onLoad() {
   EventExpression eventExpr
     = new EventExpression("NewsItem(*, *):n");
   eventExpr.addMatchListener(matchListener1);
}
 
// Code within the first Match Listener class – matchListener1
public void match(MatchEvent event) {
 
   NewsItem newsItem = (NewsItem)event.getMatchingEvents().get("n");
   EventExpression eventExpr
     = new EventExpression("Tick(\"" + newsItem.name + "\", *)");
   eventExpr.addMatchListener(matchListener2);
}
 
// Code within the second Match Listener class – matchListener2
public void match(MatchEvent event) {
   System.out.println("Detected a NewsItem, followed"
     + " by an Tick event regarding the same company.");
}
Note how the above code seeks out a NewsItem on any company, but then extracts the actual NewsItem event detected, and uses its name parameter to create the event template for seeking the Tick event.
Copyright © 2013-2015 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.
Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.