Apama 10.15.1 | Developing Apama Applications | Developing Apama Applications in EPL | Defining Monitors | Spawning monitor instances | How spawning works
 
How spawning works
In a monitor, you spawn a monitor instance by specifying the spawn keyword followed by an action. When the correlator spawns a monitor instance, it does the following:
1. Creates a new instance of the monitor that is spawning.
2. Copies the following, if there are any, to the new monitor instance:
*Current values of the spawning monitor instance's global variables
*Any arguments declared in the action that is specified in the spawn statement
*Anything referred to indirectly by means of the copied variables and arguments
3. Executes the named action with the specified arguments in the new monitor instance.
The new monitor instance does not contain any active event listeners, stream listeners, streams or stream queries that were in the spawning monitor instance. For example, data held in local variables that are bound to a listener are not copied from the spawning monitor instance to the new monitor instance. The figure below illustrates this process:
Illustration of the spawning process
The figure shows a monitor that spawns when it receives a NewStock event. Initially, the monitor has one active event listener. When the event listener finds the first NewStock event, the monitor
1. Copies the name IBM to the chosenStock variable.
2. Spawns a monitor instance.
The spawned monitor instance duplicates the initial monitor instance's state. In this example, this means that the value of the chosenStock variable in the spawned monitor instance is IBM. When the initial monitor instance receives another NewStock event (the value of the name field is ATT), it again copies the stock's name to the chosenStock variable and spawns. The same occurs for the XRX event, resulting in three spawned monitor instances.
Each new monitor instance starts with no active event listeners. It then creates a new event listener for StockTick events of the chosen stock (see the sample code in the next topic). The initial monitor instance's event listener for NewTick events remains active after spawning. However, because the action to create a new StockTick event listener is executed only in the spawned monitor instances, the initial monitor instance continues to listen for only NewTick events.