Processing Multiple Monitor Instances Simultaneously (Contexts and Parallelism)
When you start a correlator it has a single main context.
To implement parallel processing:
-
Define a new context (in the monitor you want to spawn) using this syntax:
context("contextName"). -
In the spawn statement, specify that it should be
to context("contextName"). -
In the spawned monitor, subscribe to the channels that the data is sent on (as we did for single context processing).
-
To pass events between monitor instances in different contexts, use the
send...toEPL command, instead of theroutecommand.
The send...to command sends the event to a particular context or a channel, and thus to any contexts subscribed to that channel. The send...to command places the sent event at the back of the target context’s queue (whereas the route command places it at the front of the queue). Any active listeners will eventually receive events that are sent (using send...to) once the events make their way to the head of the input queue alongside normal events.
route command to send events, not the send...to command.The SensorMonitor in your current program is an example of a monitor which can implement parallel processing because it performs calculations for a number of events (Temperature or Pressure) that are of the same type, but that have different identifiers (Sensor IDs).
In this lesson, you will update your program so that the correlator executes multiple instances of the SensorMonitor concurrently. To implement this behavior, you’ll create a new context for each Sensor ID and create a new monitor instance in each context. You’ll then use send...to statements (instead of route statements) to pass the Alert events between contexts.
Create a new context for each sensor
-
Open the
SensorMonitor.monfile. -
Update the spawn statement to spawn to a new context, which is created using the
contexttype. Change the SensorMonitor’s spawn statement to include theto contextoption:spawn monitorSensor(sensor.sensorId, sensor.targetTemperature, sensor.targetPressure) to context (sensor.sensorId);InfoIn this line of code,context(...)creates a new context and spawns a new monitor instance into that context for eachAddSensorevent. -
Move
monitor.subscribe("Factory1")into themonitorSensoraction. Each context needs to individually subscribe to any channels it wants to receive events on. -
Save your changes.
Send Alert events to all contexts
-
In the
monitorSensor()action, update the temporal listener so that it sends theAlertevents to theAlertManagerchannel instead of routing them.Change:
route Alert("TEMP_PRESSURE_RISE", sensorId, temperature.temperature, pressure.pressure);To:
send Alert("TEMP_PRESSURE_RISE", sensorId, temperature.temperature, pressure.pressure) to "AlertManager"; -
In the
AlertManagermonitor, subscribe to theAlertManagerchannel.action onload () { monitor.subscribe("AlertManager"); on all Alert() as a { print a.toString(); } } -
Save your changes and run the project.
You should see Alert messages for the S001 and S003 sensors.
You can import the completed solution for this lesson:
In the Apama Samples Git repository, navigate to the tutorials/EPL_Fundamentals_Completed directory.