Developing Apama Applications > Developing Apama Applications in EPL > Implementing Parallel Processing > Enqueuing an event to a sequence of contexts
Enqueuing an event to a sequence of contexts
In a monitor, you can enqueue an event to a sequence of contexts. The format for doing this is as follows:
enqueue EventExpression to ContextSequenceExpression;
*Replace EventExpression with any valid EPL expression that is of an event type. Unlike the general enqueue statement, you cannot specify a string representation of an event. For example, you cannot enqueue &TIME pseudo-ticks.
*Replace ContextSequenceExpression with any valid EPL expression that resolves to sequence<context>.
This statement asynchronously sends a copy of an event to each context in the specified sequence. The event goes to the back of the input queue of each context. You cannot enqueue an event whose type is defined inside a monitor.
In each target context, the correlator can immediately process the enqueued event. The correlator does not need to finish executing the action that enqueued the event before it processes the enqueued events in the target contexts. The correlator might process an enqueued event before it finishes executing the action that sent the event. Or, the correlator might process an enqueued event some time after it completes executing the action that sent the event. The order is unpredictable.
The following example uses the sequence type:
action analyse(string symbol) {
   context c1:=context(symbol + “-1”);
   context c2:=context(symbol + “-2”);
   context c3:=context(symbol + “-3”);
 
   spawn submon(symbol) to c1;
   spawn submon(symbol) to c2;
   spawn submon(symbol) to c3;
   sequence <context> ctxs := [ c1, c2, c3 ];
 
   com.apama.marketdata.Tick tick;
   log "Listening for "+symbol;
   on all com.apama.marketdata.Tick(symbol=symbol):tick {
      enqueue tick to ctxs;
   }
   on com.apama.marketdata.Finished() {
      enqueue com.apama.marketdata.Finished() to ctxs;
   }
}
action submon(string symbol) {
   ...
}
The following example uses the values() method on a dictionary of contexts to obtain a sequence of contexts:
action analyse(string symbol) {
   context c1:=context(symbol + “-1”);
   context c2:=context(symbol + “-2”);
   context c3:=context(symbol + “-3”);
 
   spawn submon(symbol) to c1;
   spawn submon(symbol) to c2;
   spawn submon(symbol) to c3;
 
   dictionary <string, context>
      ctxs := [ “c1”: c1, “c2”: c2, “c3”: c3 ];
 
   com.apama.marketdata.Tick tick;
   log "Listening for "+symbol;
   on all com.apama.marketdata.Tick(symbol=symbol):tick {
      enqueue tick to ctxs.values();
   }
   on com.apama.marketdata.Finished() {
      enqueue com.apama.marketdata.Finished() to ctxs.values();
   }
}
action submon(string symbol) {
...
}
The enqueue...to statement does not place the event on the special enqueued events queue. Instead, it puts the event on the end of the input queue of each target context. Consequently, it is possible for an enqueue...to operation to block the sending context from further processing if the input queue of a target context is full. The sending context does not continue beyond an enqueue...to statement until the event has been placed on the input queues of all target contexts.
If one of the contexts in the sequence does not contain any monitor instances the correlator ignores the enqueued event in that sequence because there are no listeners for it.
If one of the contexts in the sequence has not been created before you enqueue an event to it then the correlator terminates the monitor instance.
In a parallel application, when you specify enqueue and you do not specify a context, the correlator sends the event to all public contexts, including the main context.
Consider the following two code fragments:
for c in mySequence {
   enqueue myEvent to c;
}
 
enqueue myEvent to mySequence;
Execution of each of these fragments is typically equivalent. However, you cannot rely on equivalence. When the correlator executes the first fragment, it always delivers the event to the contexts according to their order in the sequence. When the correlator executes the second fragment it can deliver the event to contexts in any order. For example, if a context's input queue is full this can affect the order in which the correlator delivers the event to the contexts.
Copyright © 2013 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or Terracotta Inc., San Francisco, CA, USA, and/or Software AG (Canada) Inc., Cambridge, Ontario, Canada, and/or, Software AG (UK) Ltd., Derby, United Kingdom, and/or Software A.G. (Israel) Ltd., Or-Yehuda, Israel and/or their licensors.