Thursday 20 February 2014

Biztalk Design Patterns -- Sequential Convoy (Uniform Message) And Aggregator

In this blog we will continue what we have started in my last blog http://girishbiz.blogspot.in/2014/02/biztalk-patterns-sequential-convoy-non.html

We have implemented Case(I) in last blog in this we will go through Case(II)
Business Scenario -- Suppose we are creating hospital administration system. In this case, throughout day we will keep on receiving patient Information forms. We have system which receives this form and converts that info into the other form/db.

Technical Aspects -- For every patient's form a separate Orchestration instance will get created.

Solution -- We can create bunch of three Forms. By this way we reduce the number of Orchestration Instances.

This is Uniform Sequential Convoy, as we are using same receive message.

While Implementing solution for above scenario we are going to implement two design patterns 1)Sequential Convoy(Uniform Message) 2) Aggregator Pattern

Aggregator Pattern -- In this pattern we group together number of input messages, to create single batch message.
Throgh the given scenario, we will have two more design pattern under our belt.. :)

Now we will start building the solution for this scenario..

Create one schema for Patient Info. Patient will fill this info when he comes to hospital. (PatientFormas_Src.xsd)


Promote the City. We will need this to create Correlation Sets. Make sure that you are selecting field which will be same for all the received msgs. This is because our correlation set will try to match the msgs with same specified field.

Now lets create one Envelope Schema. Before creating it, lets chk what is the concept of Envelope Schema
As the name suggest it works as Envelope for other schemas. We create Envelope schema in the same way as we create xml schema, both will have structure as well . The only difference is, we need to specify the "Envelope" Property of schema as "Yes". By doing so, we tells the compiler that this schema will act as a envelope. In other words, this envelope will act as a Batch of schema msgs.
We also need to set Body XPath of the Root node, this XPath will point to the node which will actually act as Envelope(Which will contain batch in it).

In our case Envelope Schema will only contain Root node and its child node as "Any Element"
After all your Envelope schema will look something like this(Form_Batch.xsd):



As we all know, One of the important task of Send Pipeline is to Assemble/Batch messages. We will make use of this feature in our orchestration to batch our required message.
For that we will need to create a Send Pipeline in out solution. Right Click Project -> Add -> New Item -> Send Pipeline -> In the created Send Pipeline's skeleton add "XML Assembler" component, at Assemble level.(SendPipeline_Uniform.btp)
Also, Go to Properties of XML Assemble,  set the property "Envelope schemas" in the specified ellipsis with the name of your envelope schema.


So lets design orchestration now, we will design it in a way that it will receive 3 input forms and then creates the batch and sends it to destination folder. Our Orchestration will looks like:(Uniform_Process.odx)



Create Messages of type PatientForms_Src, Form_Batch
Will also need some variables as below,
Var_ctr         Int32
SendPipelineVar Microsoft.XLANGs.Pipeline.SendPipelineInputMessages

You need to add reference of the SendPipelineInputMessages. Its dll is present at the location "Drive:\Program Files\Microsoft BizTalk Server 2010".

Create a Correlation Type and Correlation Set based on UID.

Explanation of shapes in the above orchestration is as follows:

Port_ReceiveForms -- This is receive port, which will receive the input Patient forms from the specified location.

Receive_Input -- Receive Shape, which will receive input and starts the orchestration. Initialise the correlation set in this receive shape.

Expression_Initializer -- It will contain following instructions:
Var_ctr = Var_ctr + 1;
SendPipelineVar.Add(Msg_PatientForm);

We are incrementing the counter of received messages, so that we can add the condition based on that.
Along with that we are adding the received message in the Send Pipeline variable.

Scope_Batching -- This is a None Scope which is required as we are going to deal with Send Pipeline objects, which are non serializable.

Loop_Batch -- Looping required add the repetitive actions for required number of time.In this scenario I have used 3 times iterations. So the condition is Var_ctr < 3

Receive_InputLoop  --  This Receive shape will receive second onwards messages. This will follow the correlation set.

Expression_Batching -- This will contain same instartion what we had in Expression_Initializer.

ConstructMessage_Envelope -- This message will hold MessageAssignment_1 which will create Batch message from the SendPipeline variable. At this point of time variable will hold all the messages but those are not yet batch together, so that we will do in Message Assignment.

Msg_Env = null;
Microsoft.XLANGs.Pipeline.XLANGPipelineManager.ExecuteSendPipeline(typeof(SendPipeline_Uniform), SendPipelineVar, Msg_Env);

Send_Envelope -- To send the Envelope message to send port.


So far, so good.. We done with the solution creation, Now name it, sign it, deploy it.
Configure the application in the Biztalk console with physical Receive/Send port details.

Now Lets test the application..
I have placed below same file three times in the receive folder

Sample1.xml :
<ns0:PatientForm xmlns:ns0="http://SequentialConvoy.PatientForms_Src">
 <PersonalInfo>
  <Name>AAA</Name> 
  <UID>100</UID> 
  <Age>10</Age> 
  <Address>
   <City>City_0</City> 
   <Street>Street_0</Street> 
  </Address>
 </PersonalInfo>
 <Mediclaim>
  <MNo>100</MNo> 
  <CompanyName>CompanyName_0</CompanyName> 
  </Mediclaim>
</ns0:PatientForm>

(When biztalk picks the first file, it get dehydrated, meanse it is waiting for some more files for its processing)

After consuming these three sample files what I got is output is as below:

<?xml version="1.0" encoding="utf8" ?> 
 <ns0:Patient_Batch xmlns:ns0="http://SequentialConvoy.Form_Batch">
 <ns0:Patient_Batch xmlns:ns0="http://SequentialConvoy.Form_Batch">
 <ns0:PatientForm xmlns:ns0="http://SequentialConvoy.PatientForms_Src">
 <PersonalInfo>
  <Name>Name_0</Name> 
  <UID>100</UID> 
  <Age>100</Age> 
 <Address>
  <City>City_0</City> 
  <Street>Street_0</Street> 
  </Address>
  </PersonalInfo>
 <Mediclaim>
  <MNo>100</MNo> 
  <CompanyName>CompanyName_0</CompanyName> 
  </Mediclaim>
  </ns0:PatientForm>
 <ns0:PatientForm xmlns:ns0="http://SequentialConvoy.PatientForms_Src">
 <PersonalInfo>
  <Name>Name_0</Name> 
  <UID>100</UID> 
  <Age>100</Age> 
 <Address>
  <City>City_0</City> 
  <Street>Street_0</Street> 
  </Address>
  </PersonalInfo>
 <Mediclaim>
  <MNo>100</MNo> 
  <CompanyName>CompanyName_0</CompanyName> 
  </Mediclaim>
  </ns0:PatientForm>
 <ns0:PatientForm xmlns:ns0="http://SequentialConvoy.PatientForms_Src">
 <PersonalInfo>
  <Name>Name_0</Name> 
  <UID>100</UID> 
  <Age>100</Age> 
 <Address>
  <City>City_0</City> 
  <Street>Street_0</Street> 
  </Address>
  </PersonalInfo>
 <Mediclaim>
  <MNo>100</MNo> 
  <CompanyName>CompanyName_0</CompanyName> 
  </Mediclaim>
  </ns0:PatientForm>
  </ns0:Patient_Batch>
  </ns0:Patient_Batch>


So in this way, we have seen implementaion of two Biztalk Design Patterns in this blog..

Will catch you in next blog with some more Patterns..

Thanks..
:-)

6 comments: