Developing Apama Applications > Developing Apama Applications in EPL > Implementing Parallel Processing > Enqueuing an event to a particular context
Enqueuing an event to a particular context
In a monitor, you can enqueue an event to a particular context, as described here, or you can enqueue an event to a sequence of contexts, described in the next topic. The format for enqueuing an event to a particular context is as follows:
enqueue EventExpression to ContextExpression;
*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 ContextExpression with any valid EPL expression that is of the context type. Typically, this is the name of a context variable.
This statement asynchronously sends an event to the specified context. The event goes to the back of the context’s input queue.
In the 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 event in the target context. The correlator might process the enqueued event before it finishes executing the action that sent the event. Or, the correlator might process the enqueued event some time after it completes executing the action that sent the event. The order is unpredictable. The order in which the target contexts receive the enqueued event is also unpredictable. For example:
action analyse(string symbol) {
   context c:=context(symbol);
   spawn submon(symbol) to c;
   com.apama.marketdata.Tick tick;
   log "Listening for "+symbol;
   on all com.apama.marketdata.Tick(symbol=symbol):tick {
      enqueue tick to c;
   }
   on com.apama.marketdata.Finished() {
      enqueue com.apama.marketdata.Finished() to c;
   }
}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 target context’s input queue. Consequently, it is possible for an enqueue...to operation to block the sending context from further processing if the input queue of the target context is full. Either an event that you enqueue to a particular context arrives on the target context’s input queue or the sending context waits for room on the target context’s input queue.
If you enqueue an event to a context that does not contain any monitor instances, the correlator discards the event because there are no listeners for it.
In a parallel application, when you specify enqueue and you do not specify a context, the correlator sends the event to all public contexts. To enqueue an event to only the main context you can simply use the enqueue statement if you are certain there are never other public contexts. If there are or there might be one or more other public contexts, then you must enqueue the event to a context reference for the main context. For example, you can obtain a reference to the main context by calling context.current() in a monitor instance in the main context. You can then pass that reference by spawning from that monitor instance to a monitor instance in another context. The format for doing this is as follows:
enqueue eventExpression to context_reference_to_main_context;
In some situations, for example when you change a single-context application to use parallel processing, you might want to explicitly enqueue an event to only the context that contains the monitor instance that contains the enqueue statement. To enqueue an event to only this context specify:
enqueue eventExpression to context.current()
You must create the context before you enqueue an event to the context. You cannot enqueue an event to a context that you have declared but not created. For example, the following code causes the correlator to terminate the monitor instance:
monitor m {
   context c;
   action onload()
   {
      enqueue A() to c;
   }
}
See also Generating events with the enqueue command.
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.