Cilia/Getting Started Tutorial/Scheduler and Dispatcher

From Wiki Adele Team
Jump to: navigation, search

Using Scheduler and Dispatcher elements

In a previous lesson we have learned how to use different kind of schedulers to modify the behavior of mediators. In this lesson we will learn how to create new kinds of schedulers and dispatchers to obtain a special behavior when it does not exist in the Cilia available components.


Scheduler and Dispatcher Behavior

We will see now other capabilities of Cilia mediators. More particularly, how the other mediator elements (scheduler and dispatcher) could change the mediator’s behavior. We will extend the last mediation chain by introducing a third Mediator, the Decryptor mediator. This mediator decrypts messages encrypted by its predecessor mediator. However, this decryption process is realized in a batch way, the decryption logic is executed by blocks of three encrypted messages. The chain architecture is as follow:


Hello-chain-3.png

Encryptor Mediator Implementation

Processor Definition

The Encryptor class implements the processor of the Mediator. It takes the data content and then applies a DES encryption.

package fr.liglab.adele.cilia.components.mediators;

import fr.liglab.adele.cilia.Data;

/**
 * Data Decryption with DES
 *
 */
public class Decryptor {

   /**
    * Decrypts data content
    * @param data
    * @return
    */
   public Data process(Data data) {
      if (data != null) {
         byte[] dataToDecrypt = (byte[])data.getContent();
         String temp = SimpleChiper.getInstance().decrypt(dataToDecrypt);
         data.setContent(temp);
      }
      return data;
   }

}

The processor description is as follows:

  <processor name="EncryptorProcessor" classname="fr.liglab.adele.cilia.components.mediators.Encryptor">
    <cilia:method name="process" data.type="fr.liglab.adele.cilia.Data" />
  </processor>

Mediator Description

In this mediator we will use another scheduler, the counter-scheduler. Its responsibility is to count the occurrences of data arrivals, and when a specified number of occurrences has been reached it must start the mediator processing.

   
  <mediator-component name="DecryptorMediator" category="samples">
    <cilia:processor name="DecryptorProcessor" />
    <cilia:scheduler name="counter-scheduler" />
    <cilia:dispatcher name="multicast-dispatcher" />
  </mediator-component>

Encryptor - Decryptor Chain

Decryptor Mediator decrypts the messages sent by the Encryptor Mediator. However, the decryptor must have to do this task every three messages. In order to make this kind of synchronization (on data arrival) we have used a counter-scheduler as presented previously. This scheduler must be configured to indicate the condition to the processor, this configuration is made to the mediator instance (section scheduler) as is presented below.

This new Chain is described as follow:

<cilia>

  <chain id="HelloWorldChain-EncDec">

    <!-- Adapters instances definition -->
    <adapters>
      <adapter-instance type="gui-adapter" id="entryAdapter" />
      <adapter-instance type="console-adapter" id="exitAdapter">
        <property name="console.header" value="ENCRYPTOR.OUT:" />
      </adapter-instance>
    </adapters>

    <!-- Mediators instances definition -->
    <mediators>
      <mediator-instance type="HelloMediator" id="hello">
        <ports>
          <in-port name="in" />
          <out-port name="out" />
        </ports>
      </mediator-instance>
      <mediator-instance type="EncryptorMediator" id="encryptor">
        <ports>
          <in-port name="in" />
          <out-port name="out" />
        </ports>
      </mediator-instance>
      <mediator-instance type="DecryptorMediator" id="decryptor">
        <ports>
          <in-port name="in" />
          <out-port name="out" />
        </ports>
        <scheduler>
          <property name="count">
            <item key="max" value="(data.name=*)" /> <!-- All data will be counted -->
          </property>
          <property name="condition" value="(max=3)" /> <!-- Scheduler waits for three data occurrences -->
        </scheduler>
      </mediator-instance>
    </mediators>

    <!-- Bindings definition -->
    <bindings>
      <binding from="entryAdapter" to="hello:in" />
      <binding from="hello:out" to="encryptor:in" />
      <binding from="encryptor:out" to="decryptor:in" />
      <binding from="encryptor:out" to="exitAdapter" />
      <binding from="decryptor:out" to="exitAdapter" />
    </bindings>

  </chain>
</cilia>

Two main points to retain from this new chain:

  • The encryptor mediator can send multiple copies of its resulting process to different destinations.
  • The decryptor mediator does not process data received one by one but by block of 3 data.

Indeed, each mediator type is defined by three essential constituents:

  • Scheduler : performs the incoming data synchronization. The Scheduler’s synchronization logic (e.g. timer or counter) decides when to send collected data to the processor. Based on this, the scheduler triggers the data processing notification.
  • Processor : The processor implements the Mediator’s main data-processing algorithm. It is notified by the scheduler when the data are ready to be processed, it processes the data and passes the result to the dispatcher.
  • Dispatcher : The dispatcher receives the processed data from the processor. It decides where to deliver the data based on its internal logic and on the data content. The dispatcher’s destination choice is a logical one, as a consequence of the loosely-coupled relations between Mediators.

In the above example, encryptor mediator uses a multicast-dispatcher dispatcher, which as its name suggests, dispatches a copy of the resulting data to all recipients. The decryptor mediator uses counter-scheduler scheduler which starts the processing after predetermined number of data were collected for the mediator. The user can override the configuration properties of those internal constituents to change the mediator's behavior.

Execution of Chains

Download hello-mediator-1.0.0-SNAPSHOT.jar | encryption-mediators-1.0.0-SNAPSHOT.jar | tp2_3.dscilia


Example Resources

Source code in the SVN repository

svn checkout svn://svn.ligforge.imag.fr/svnroot/cilia/trunk/examples/cilia-tp2-projects

svn checkout https://svn.ligforge.imag.fr/svnroot/cilia/trunk/examples/cilia-tp2-projects

Browse the SVN code here.

Snapshot of source code

Get a snapshot of the SVN project here.

List of compiled artefacts

Encryptors Mediator bundle

Encryptor-Decryptor Chain File