Wednesday, March 18, 2020

XML Batching(Aggregation) in Logic App

Introduction

One of my connection on Linkedin asked for help 



So basically his requirement is to send all the individual records in a group to CRM endpoint i.e. a Batch of individual records. This is called Aggregator or Batching pattern in Integration.

aggregator pattern
When we say we want to group messages then comes a question as to how to  wrap them.In BizTalk we handle such scenario with help of Envelope schema (for Debatching and Batching). 

In logic app there is a special trigger for grouping of the messages - Batch trigger.


In last post Batching(Aggregating) messages in Logic App we saw how to group JSON messages, in this post let's see how to batch XML message.

One of the primary thing to consider is valid that XML document has single Root Node.

We would create two logic app 1. XMLBatchProcessor 2. BatchProcessCaller


Steps to implement Aggregator/Batching Pattern in Logic App


1. Create a XMLBatchProcessor Logic App

From the Azure Portal select Create a resource -> search of “Logic App” -> click Create button

After instance is created, go to logic app designer and to add trigger - search for Batch. Select the Batch message trigger and configure batch process trigger .

  • Batch Mode 
Here we have two option

- Inline: For defining release criteria inside the batch trigger itself (for this post am using this - it's kind of hard coding). 

IMO This should be opted if you don't have integration account or you have limited batch processing requirement.

- Integration Account: Ideally, this is should be preferred, as it helps for defining multiple release criteria configurations and also you don't have to make updates in Logic app every time release criteria is changed . It is like a configuration file sitting in an integration account,so you can maintain these configurations all in one place rather than in separate logic apps
  • Batch Name
The name for your batch, which is "CommonBatch" in this example  and applies only to Inline batch mode
  • Release Criteria

As we have used Inline batch mode, we need to select the criteria to meet before processing each batch (we can select multiple criteria also):
- Message count based: Release the batch based on the number of messages collected by the batch.
- Size based: Release the batch based on the total size in bytes for all messages collected by that batch.
- Schedule: Release the batch based on a recurrence schedule, which specifies an interval and frequency. In the advanced options, you can also select a time zone and provide a start date and time.
- Select all: Use all the specified criteria.

Here Message count is selected with count as 3

configuring batch process trigger in logic app

Now, what you want to do with Batched Message? 

We want to wrap this group of xml messages with a Root element tag, so that we get a valid XML Batched message and then finally can send it to the required destination for further processing.

So how we do it -- one option would be to create Azure Function - you will have to write code  which will accept the Batched items and return new batched message. And call this function from your logic app.

Another would be to use Inline code - but it is still in preview and currently only supports Java Script.

Here we will make use of string Variable, using which we do following

Initialize variable with Start tag <RootNode> 

So after the Batch Trigger step , add new action and select variable
Add variable

Select Initialize variable


Give the Variable a Name , String as Datatype and against Value provide the start tag you want , here <BatchedMessages> is used

Initialize variable

Keep on appending each Batched Item

Now add new action -- Append to string variable
Append to string variable

Select Message Content of BatchedItems which is output of BatchTrigger step as a value to append. Note that For each gets automatically added.

values to append



Close the Rootnode tag </RootNode>

Next again we add Append Variable action after Foreach and add closing tag to it.

And now the final step is to send to destination -- for this post have used Send email action with attachment

send email action


Note : Batch trigger is a special trigger meant for collecting messages and it only starts the instance once it's release condition is met - so you won't see it's instance in run history till it reaches it's release point. 

Also you would not be able to call it outside of your subscription or from different region.


2. Create a caller Logic App

From the Azure Portal select Create a resource -> search of “Logic App” -> click Create button.

In designer select http tigger and in next action search for Batch, you should see all the logic apps which have Batchtrigger, select the one created in above step.
call xml processor



As soon as you select it, Trigger Name and Workflow will be auto populated, against Batch Name give the same name used in step 1 and for Message content provide triggerbody.

configure call xml processor

That's it, ready to use.


Testing


Using Postman, sent 3 request(xml) having order details in it in it to the BatchProcessCaller logic app
xml input


And below is the email received - which is sent by XMLBatchProcessor logic app
received email

Downloaded the attachment and following is what we expected

final output






Related Post 

Tuesday, March 17, 2020

Batching(Aggregating) messages in Logic App

Introduction

In last couple of posts Debatching(Splitting) XML Message in Logic Apps - ForEach and SplitOn  and Debatching(Splitting) JSON Message in Logic Apps - ForEach and SplitOn we understood about Splitter pattern and how to use it to split/debatch messages.

Batching or aggregation is opposite process of debatching/splitting where we group various individual message and send them as a single composed message for further processing.
aggregator pattern
If you have worked on BizTalk, you can relate to the Parallel convoy - Which is used to receive multiple messages and act upon them based on certain condition. 

Likewise , here the Aggregator/Batch Processor gets started as soon as it receives individual message and keeps on waiting and receiving for other individual messages till the time the Release condition is met.

Release conditions are to set boundary to the compose message, like if size of composed message is reached to certain limit or if count of messages grouped reached to limit or batched messages have to be sent at a scheduled time etc.

When done with grouping it is further sent for processing as one Batch this certainly increases throughput, performance, cost etc - as the number of calls to destination system is reduced.

In this post we will see how to batch/aggregate message, for this we would create two logic app 1. BatchProcessor 2. BatchProcessCaller


Steps to implement Aggregator/Batching Pattern in Logic App


1. Create a Batch Processor Logic App

From the Azure Portal select Create a resource -> search of “Logic App” -> click Create button


Create BatchProcessor Logic App

After instance is created, go to logic app designer and to add trigger - search for Batch. Select the Batch message trigger and configure batch process trigger .

  • Batch Mode 
Here we have two option

- Inline: For defining release criteria inside the batch trigger itself (for this post am using this - it's kind of hard coding). 

IMO This should be opted if you don't have integration account or you have limited batch processing requirement.

- Integration Account: Ideally, this is should be preferred, as it helps for defining multiple release criteria configurations and also you don't have to make updates in Logic app every time release criteria is changed . It is like a configuration file sitting in an integration account,so you can maintain these configurations all in one place rather than in separate logic apps
  • Batch Name
The name for your batch, which is "CommonBatch" in this example  and applies only to Inline batch mode
  • Release Criteria

As we have used Inline batch mode, we need to select the criteria to meet before processing each batch (we can select multiple criteria also):
- Message count based: Release the batch based on the number of messages collected by the batch.
- Size based: Release the batch based on the total size in bytes for all messages collected by that batch.
- Schedule: Release the batch based on a recurrence schedule, which specifies an interval and frequency. In the advanced options, you can also select a time zone and provide a start date and time.
- Select all: Use all the specified criteria.

Here Message count is selected with count as 3

configuring batch process trigger in logic app

Now, what you want to do with Batched Message? you can send it to the required destination/do further processing, for the sake of this post, I have used send email action to send the batched message
send email action


Note : Batch trigger is a special trigger meant for collecting messages and it only starts the instance once it's release condition is met - so you won't see it's instance in run history till it reaches it's release point. 

Also you would not be able to call it outside of your subscription or from different region.


2. Create a caller Logic App

From the Azure Portal select Create a resource -> search of “Logic App” -> click Create button.

In designer select http tigger and in next action search for Batch, you should see all the logic apps which have Batchtrigger, select the one created in above step.


call batch process logic app
As soon as you select it, Trigger Name and Workflow will auto populate, against Batch Name give the same name used in step 1 and for Message content provide triggerbody.

Configure batch process logic app

That's it, ready to use.


Testing


Using Postman, sent 3 request(JSON) having three two properties in it in it to the BatchProcessCaller logic app
request to logic app


And below is the email received - which is sent by BatchProcessor logic app

The message contains some additional fields apart from content(highlighted in yellow), which are details about the Batch .

We saw how we can batch json messages, in following post have covered about XML messages batching  -    XML Batching(Aggregation) in Logic App





Related Post 

Sunday, March 8, 2020

Removing ns0: prefix from xml output from BizTalk/Logic app XSLT map

Introduction

Xml, which stands for stands for EXtensible Markup Language was designed to describe data and to do that you use tags. It's basic purpose is to focus on what data is  - self decsribing. 

And as we have a provision to add our own tags, it becomes Extensible.

Having this properties makes it very suitable for defining a message (Records, elements and attributes) and exchange it across, also data can be stored in it and can be refered/updated as an when needed (most of the configurations are xml based).

In case of where XML is involved in messaging, it becomes vital to have Namespace as it helps in avoiding element name clashes/conflicts.

A Namespace is a set of unique names. Namespace is a mechanisms by which element and attribute name can be assigned to a group. The Namespace is identified by URI(Uniform Resource Identifiers).

An XML instance may contain element or attribute names from more than one XML vocabulary. If each vocabulary is given a namespace, the ambiguity between identically named elements or attributes can be resolved.

For example, you are dealing with OrderXML and you are receiving multiple OrderXML from different vendors - so how do you differentiate which xml(same structure) is from whom - It is with namespace.


To define xml you use xsd and when you create xsd in BizTalk/Logic app Project(using visual studio) few namespaces automatically gets added, also gets added is Target namespace(identifier - that created xsd belongs to this project ) to say that this xsd belong to it.


default namespaces in xsd

The elements (schema, element, complexType, sequence, …) of the schema itself belong to the namespace http://www.w3.org/2001/XMLSchema.

The namespaces which gets added automatically are actually to tell our schema editor that we are referring to definitions in them and it is done with help of namespace prefix - xs and b in above image.  Note that for Target Namespace there is no prefix .

The target namespace is the place were your declared elements and attributes in your schema belong to (you can refer them as Local).

Consider Target Namespace is to schema is what a namespace is to .Net Object. 

Read more about Schema -- BizTalk Server : Deep dive in schema design

How ns0 gets added in output of map using above schema as destination


Now when we do map from one schema to another, we create new xml and to which namespace it
belongs to it gets added and thus you see namespace and it's prefix

 ns0 added in map result

If you see above ns0 gets added in test map result, although there was no prefix associated with TragetNamespace in xsd -- It is the mapper which does it and ns0 is the default prefix.

So if I use xsd having prefix already associated with TargetNamespace  -- Still mapper will add the default prefix ns0.


How to remove default prefix ns0 and have prefix as per our preference


To do this we need to bypass the mapper and for that we make changes in xslt generated
by the mapper.

Right click the map, select debug and you should see the path where xslt is generated,Copy the path.

Now edit the xslt and add preferred prefix and save it
change prefix ns0
You can use saved xslt in Logic app or in BizTalk by setting the custom xslt path and get desired output as below-

As can be seen , edi is now added as prefix appears with Root node.

What if you are required to have this prefix associated with all the local elements and local attributes?

To do that we use following properties Element FormDefault and Attribute FormDefault
This properties are only relevant if a target namespace(it is optional) is used and only for local declared attributes and elements.

Qualified means: “belongs to a target namespace”.
Unqualified means: “not belonging to a target namespace”.

When Default or Unqualified is used -- No Prefix is associated and for
Qualified -- Prefix is associated








Related Post