Stream lifetime
When considering the lifecycle of a stream, first reflect on how the stream is created. A from statement is similar to an on statement, in that both create stream listeners. When creating the stream listener, a listener variable can be assigned to refer to the stream listener. The listener variable can then be used (at a later time) to quit the stream listener.8
When creating a stream query and assigning it to a stream variable, the stream variable can be used (at a later time) to quit the stream query.
Once created, a stream (and the stream query supplying it) remains in existence until any of the following occur:
It is quit.
All of its downstream connections are removed.
Removal of an upstream stream means that the stream (stream query) can generate no more output.
The above statements sound rather complicated but are quite straightforward. Consider the following code example:
01. event Temperature { string sensorId; float temperature; }
02. event Quit { string what; }
01. using com.apama.aggregates.mean;
02. monitor StreamLifetimes {
03. action onload() {
04. float temperature;
05. stream<Temperature> temperatures := all Temperature(sensorId="S001");
06. stream<float> meanTs := from t in temperatures within 60.0
07. select mean(t.temperature);
08. listener freezing := from t in meanTs where t < 0.0 select t: temperature {
09. print "It's freezing! The temperature is " + temperature.toString();
10. }
11. listener boiling := from t in meanTs where t > 100.0 select t: temperature {
12. print "It's boiling! The temperature is " + temperature.toString();
13. }
14. on Quit("temperatures") { temperatures.quit(); }
15. on Quit("meanTs") { meanTs.quit(); }
16. on Quit("freezing") { freezing.quit(); }
17. on Quit("boiling") { boiling.quit(); }
18. }
19. }
In this example, the stream network consists of two streams (declared on lines 5 and 6-7) and two stream listeners (declared on lines 8-10 and 11-13). The stream variables temperatures and meanTs refer to the two streams, and the listener variables freezing and boiling refer to the two stream listeners. Let's take a look at what happens when quit() is called on each of the listener and stream variables:
If
freezing.quit() is called, then only the stream listener referred to by
freezing becomes inactive. Similarly, if
boiling.quit() is called, then only the stream listener referred to by
boiling becomes inactive.
If
meanTs.quit() is called, then all of the streams, stream queries and stream listeners will become inactive. This is because the
meanTs query is the only downstream connection for the
temperatures stream, and once
meanTs is quit, the two stream listeners for
freezing and
boiling can no longer produce any output.
Finally, if
temperatures.quit() is called, then there would be no further input to the stream query for
meanTs. However, items in the window of the stream query may remain within the window for up to 60.0 seconds after the
temperatures stream is quit. Hence the
meanTs stream query, and any queries/listeners downstream of it, will remain active until all items in the
meanTs stream query window have expired (been ejected from the window).
8 This is identical to an EPL on statement, where a listener variable can be used to quit a standard event listener.