Message Correlation in Workflow

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

There are many situations in Business when some Business Process has to be orchestrated by multiple incoming messages. For all of you who are not very familiar with system integration, here are few words about it.
Imagine your need to implement an application which is driven by multiple events. For example when event e1 and event e2 are received then some next step in the application has to be performed.
In this case you need to implement some kind of event listener which is able to correlate events. This is important, because there might be multiple instances of e1 and e2 and the system needs to know which e1 corresponds to e2.
Products like BizTalk Server offer such solution which is described here. But sometimes you don’t want to implement this without buying a big server. In this cases you can use Workflow Foundation.
Correlation in be used in many use cases. In fact it is used when ever the application needs to wait on different events, which contextually contain in the message or some kind of dedicated context the business relevant information.
In this post I will show step by step how to implement this scenario.

To do this I have created the workflow with a parallel activity. Then I used two receive activities. The left one should receive the message (event) called Trade and the right one receives a message (event) called Allocation.
This workflow host will fire a new instance of this workflow for every Trade (left messages) and Allocation(right message) which do not correlate to each other. If two messages arrives which correlate to each other then the workflow
will continue execution at the “Action” activity. Otherwise it will simply wait to the next correlating message. Depending on host and configuration, which is used, workflow instance might remain in memory or it can be unloaded in persistence store.

image

Further, I do not want to explore the business meaning of this workflow. The business meaning is at this point simply not important.
It is important, that both events derive from the base class called ObjecBase and share the same property TradeID.
This property will be later used for correlation between messages (events). It means every message with the same TradeID will belong together. Here is definition of required classes, which define the
messages which will be correlated.


    [
DataContract
    [KnownType(typeof(TradeCapture))]
    [KnownType(typeof(Allocation))]
    [KnownType(typeof(Deal))]
    public class ObjectBase
    {
        [DataMember]
        public string TradeId { get; set; } 
   
}

 

    [DataContract]
    public class Allocation : ObjectBase
    {     

    }

 

   [DataContract]
    public class TradeCapture : ObjectBase
    {
     

    }

To setup the correlation, select ReceiveTrade activity and then click on stencil CorrelatesOn.

image image

Following dialog will appear:

image

In this dialog we have to define the query which will be used for content based correlation. This means basically that Workflow Runtime will look in the message (this is why it is called content based) to find the part of content which will be used for correlation. In this case this is property TradeId. You don’t have to enter the expression shown below. There is a simple property wizard which helps.The value ‘key’ is any word which describes the key name of the property expression.

As next we define a variable of type CorrelationToken and select the stencil “CorrelatesWith”.

image

In dialog shown above-right we need to set the name of variable which will store the correlation token value. In this case this is variable with the name “handle”. This has to be don on both receive activities.
As recap, if you want to setup correlation between N receive activities, we have to :

1) Create variable of type Correlation Handle

2) Set CorrelatesWith of each correlating activity to the variable defined under 1.

3) Set CorrelatesOn to the property used for correlation

Note that CorrelationInitializers is not used in this scenario. This property is used when multiple receive activities has to be correlated in sequence (after each other). Example which shows that scenario is shown here.

In the real world scenario you will probably have to implement timeout operation. That means that you want to limit time-window in which messages have to be correlated. Workflow offers an elegant way to do that.
Following picture shows the Pick-activity with two branches. Branch on the left the the workflow shown above. That branch is responsible for message receive and correlation. The branch on the right side is used for time-out.
image

By using of the Pick activity Workflow will wait for specified Delay-time (upper delay on right) in the right branch.

In the next post I will shown how to implement the same scenario with the pure C# code by using of Azure Platform Services.


Posted Apr 17 2014, 08:27 PM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.