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..
:-)

Wednesday 19 February 2014

Biztalk Design Patterns -- Sequential Convoy (Non-Uniform Message)

In this blog we will have look at Biztalk Pattern -- Sequential Convoy Non-Uniform.

What is Biztalk Pattern??
Biztalk Patterns are nothing but the common scenarios which we use frequently while designing the Biztalk solution.
Depending on the requirement, one has to decide which pattern to use. We can use one or more patterns in the solution.

In this blog we will understand and Implement Sequential Convoy(Non Uniform) Pattern.

What is Sequential Convoy?
Sequential Convoy is the pattern in which we group number of receive messages together in Singleton manner.


What are the scenarios where we can implement Sequential Convoy.
Case I --

Buisness Scenario -- In most cases patient also have their Mediclaim. Now we have two documents for each patient(patient info and Mediclaim).

Technical Aspects -- Patient may or may not have mediclaim. So wait for some specific time for mediclaim form if it comes then group the mediclaim and patient info and send to send folder. If Mediclaim does not come in specified time then pass on the only patient info to send folder.

Solution -- At first, We will receive patient Info form then we will wait for mediclaim form for same customer(Unique Reference ID). If if comes in specified time then club the two message for that customer and process those.

This is Non Uniform Sequential Convoy, as we grouping two different receive messages in this sequential Convoy.


Case II --

Buisness 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 Forms such as 20 forms at a time or collect all the forms after every one hour. By this way we reduce the number of Orchestration Instances.

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


Enough with the theory, Lets start with the implementation of Case I with Non Uniform Sequential Convoy...

To start with, lets create three schemas:

1)schema_patient -- This will contain patient's Info



2)Schema_Medi    -- This will contain patient's mediclaim info.




3)patientForm    -- This will contain mix atrributes of PatientForm and Mediclaim Form.




4)Property111(Property Schema) -- This schema is needed for the purpose of correlation. Here we can promote the properties so that we can use them in orchestation.
In our scenario we will promote schema_patient/AdharNo, Schema_Medi/AdharNo. In the Property Schema create a new field name it as "UniqueNo".



To promote the properties go to that field -> Promote -> Show Promotion
Following window will pop up


Click on Property Fields Tab. Browse to Property file which we have created. From left panel add the field which we want to promote.

We will also create a map file which will convert schema_patient and Schema_Medi into PatientForm.It will contain following links:



Now, we are all set to design out orchestration.  Design the orchestration as below:


Explanation :
Port_1 : This is the receive port which will accept the Patient Info forms and Mediclaim Form from same location. It has two different Operations. (Operation_1) for receiving msg of type schema_patient (Operation_2) for receiving msg type Schema_Medi.
Note - Set "Ordered Delivery" property to "True". This is to specify that Msg will get delivered in sequence.

Receive_1 : This is the Starting point of Orchestration where we will receive msg of schema_patient.Set "Activate" property to "True".

Listen_1 : This Listen shape is added because we need to wait for Mediclaim for some specific time. So it will contain two branches. One branch(Receive_2) will be waiting for msg of type Schema_Medi and other(Delay_1) will wait for some time period. I have used following expression:

new System.TimeSpan(0,1,0) 
By this Listen shape will wait for 1 minute and will start the execution if other branch does not start their execution.

Correlation Set up
Create one Correlation Type with Property Field which we have set in the Property file.
Create Correlation Set based on that Correlation Type.

For Receive_1 -- Set property "Initializing Correlation Set" with name of Correlation set you created.
For Receive_2 -- Set property "Following Correlation Set" with name of Correlation set you created.

ConstructMessage1 : This construct shape contains Transform_1 shape which is having map to convert schema_patient + Schema_Medi => patientForm.

Send_2 : Send shape to send the msg of type patientForm to Send Port.

Send_1 : Send shape to send the msg of type schema_patient to Send port.

Port_2 : This is Send Port to which we will send the msgs of type patientForm.

Port_3 : This is Send Port to which we will send the msgs of type schema_patient.

With this we are done with the creation of solution, now deploy this solution.
Configure the application in Biztalk console with receive and send port configuration.
Now, lets test what we have done till now..

Generate the instance of schema_patient place it in the specified Receive folder.
My Input sample contains:
<ns0:Pat xmlns:ns0="http://Listen_Practice.schema_patient">
  <Name>FirstName</Name> 
  <LastName>LastName</LastName> 
  <Age>27</Age> 
  <ANo>1</ANo> 
  <Disease>XYZ</Disease> 
</ns0:Pat>

You wont see any output for 1 minute. This is because we have set the delay of 1 min. So Orchestration will wait for Schema_Medi msg for 1 min.

Within this 1 minute generate the instance of Schema_Medi place it in the specified Receive folder.
My Input sample contains:
<ns0:MClaim xmlns:ns0="http://Listen_Practice.Schema_Medi">
  <MNo>MNo_0</MNo> 
  <MName>MName_0</MName> 
  <AdharNo>1</AdharNo> 
</ns0:MClaim>


And when I chk the Output folder it is having following contents:
<?xml version="1.0" encoding="utf-8" ?> 
<ns0:PatientForm xmlns:ns0="http://Listen_Practice.patientForm">
  <Name>Girish</Name> 
  <Age>27</Age> 
  <AdharNo>1</AdharNo> 
  <MNo>MNo_0</MNo> 
  <Disease>Disease_0</Disease> 
</ns0:PatientForm>


If you do not send Schema_Medi msg within 1 min then you will get output as:
<?xml version="1.0" encoding="utf-8" ?> 
<ns0:Pat xmlns:ns0="http://Listen_Practice.schema_patient">
  <Name>Girish</Name> 
  <LastName>Patil</LastName> 
  <Age>27</Age> 
  <ANo>1</ANo> 
  <Disease>Disease_0</Disease> 
</ns0:Pat>

This is how Non-Uniform Sequential Convoy works..


We will implement next pattern in next blog, so till then "Keep Reading, Keep Improving"..
Enjoy..