Thursday 6 March 2014

Using Shared Variables in Message Broker

Variables
The types of variables varies with Scope,Lifetime,Shared characteristics
Scope – Range (node level,flow level..)
Lifetime – Time (Lifetime for one thread)
Shared Variable:
Shared
Shared variables can be used to implement an in-memory cache in the message flow, see Optimizing message flow response times. Shared variables have a long lifetime and are visible to multiple messages passing through a flow, see Long-lived variables. Shared variables exist for the lifetime of the execution group process, the lifetime of the flow or node, or the lifetime of the node SQL that declares the variable (whichever is the shortest). Shared variables are initialized when the first message passes through the flow or node after each broker startup.
ATOMIC option of the BEGIN ... END statement. The BEGIN ATOMIC construct is useful when a number of changes must be made to a shared variable and it is important to prevent other instances seeing the intermediate states of the data.
As Shared variables can be used under one execution group for multiple instances,if we are using counter,then there is a possibility that one thread tries to overwrite the other thread.This will produce unexpected results.To avoid this,we need to make it Atomic.

For LongLived variables –We can use database.But Write access will be slow.But persistence and Transaction is good.
you cannot share variables across execution groups.
Shared variables advantages:
Hence we go for in-memory cache.R/W access is fast in the expense of shorted persistence and no transaction.
Access is direct; that is, there is no need to use a special function (SELECT) to get data, or special statements (INSERT, UPDATE, or DELETE) to modify data. You can refer to the data directly in expressions.

Long-lived data types have an extended lifetime beyond that of a single message passing through a node. Long-lived data types are shared between threads and exist for the life of a message flow
ESQL ROW Datatype
The ROW data type holds a tree structure. 
To store a message tree in a shared variable, use the ROW data type.
Shared Variable usage code:
Without Atomicity
DECLARE CountSHARED INTEGER0;
 DECLARE mySharedRowSHARED ROW;
CREATE COMPUTEMODULE SharedVariables_test_Compute
      CREATE FUNCTIONMain() RETURNS BOOLEAN
      BEGIN
            -- CALL CopyMessageHeaders();
             
       SET OutputRoot.XMLNSC.SHARE.COUNT  = Count;
     
     SET mySharedRow= THE
   (SELECT ITEM Name FROM InputRoot.XMLNSC.Student.Name[] );
  
   SET OutputRoot.XMLNSC.SHARE.Result = mySharedRow;
   SET Count=Count+1;
            RETURN TRUE;
      END;

With Atomicity
If we want to use Atomic block only in the counter increment part (Write part) but not the Read part of some Shared variable,then only the Write portion can be given as Atomic.
DECLARE CountSHARED INTEGER0;
 DECLARE mySharedRowSHARED ROW;
CREATE COMPUTEMODULE SharedVariables_test_Compute
      CREATE FUNCTIONMain() RETURNS BOOLEAN
      BEGIN ATOMIC
            -- CALL CopyMessageHeaders();
             
       SET OutputRoot.XMLNSC.SHARE.COUNT  = Count;
     
     SET mySharedRow= THE
   (SELECT ITEM Name FROM InputRoot.XMLNSC.Student.Name[] );
  
   SET OutputRoot.XMLNSC.SHARE.Result = mySharedRow;
   SET Count=Count+1;
            RETURN TRUE;
      END;


No comments:

Post a Comment