Controlling Activity Execution Within the Engine

In some cases an activity implementer may wish to have a greater degree of control over the execution of an activity. There is an extension mechanism in place through which an activity can install a custom handler to manage its execution or custom pipes for output if less total control is required. The extension mechanism is the configureContext(Context) method provided by the uk.org.ogsadai.engine.Activity base class. This method is called after object construction, but before the call to setContext, to permit an activity to modify the context prior to execution thereby allowing it to govern almost any aspect of its execution. A note of caution is required however - the context should be treated as write-only at this point due to the fact that its state cannot be guaranteed, and so could differ between two consecutive executions.

Custom Output Pipes

Installing a custom output pipe for an activity is done as follows:

  1. An instance of the desired pipe type must be created. The pipe's constructor will probably require an uk.org.ogsadai.engine.ActivityHandler so that it can initiate processing when a block is requested. For example:
    mOutput = 
      new CachePipe((ActivityHandler)context.get(EngineImpl.HANDLERS + 
      toString()));
    
  2. The pipe instance must then be entered into the following section of the context:
    context.put(EngineImpl.PIPES + mOutputs[0], mOutput);
    

That is all that is necessary to install a custom pipe.

RunAhead Activities

To make the engine assign a RunAheadHandler, the activity must convince the engine that it is an output activity. This can be done as follows:

  1. Do not publish any outputs - this will make the engine think the activity is an output activity
  2. Use the method described above to add the necessary outputs to the context but with the additional line:
    context.put(EngineImpl.SYNCHRONOUS + mOutputs[0], mOutput);
    

    This line should normally be used when using a RunAheadHandler in this way because the output isn't processed by the uk.org.ogsadai.engine.ActivityHandler base class. Without it the engine will not attach a deliverToResponse activity to the output if it is not used elsewhere in the request.

Custom Activity Handlers

Installing a custom handler follows the same pattern as other customizations. During the configureContext method the following should be done:

  1. An instance of the handler to use should be created.
  2. The handler should register itself as the handler for the activity during creation.
  3. If custom pipes are required they should be added as described.

Warning: when using non-threaded custom handlers for activities that do not publish any outputs you must be extremely careful that the processBlock method will not block indefinitely while waiting on consumption of a block. This case is rare and will only occur when using a buffer output pipe that blocks on a call to put when the buffer is full. In this case it is possible that the engine will not have initiated the activity that should be consuming from the pipe and the request could hang.