This blog will show you how to use BizTalk Server to send to different resources that share the same base URL.
Use case
@biztalkdj asked
We have a scenario, where we are calling the backend APIs all have different JSON request n response. Backend apis are hosted in app-server say (apiappserver), thus the base url can be considered as same. We have used itineraries define the process flow. Our bottleneck being not able to handle different responses on the dynamic request n response port pipeline.
This was part of an earlier Twitter exchange where they asked
Is it possible to set pipeline properties dynamically in #biztalk
I replied and send I don’t use dynamic send ports anymore. Further to this I have never used the BizTalk Server ESB Tool Kit and prefer to use the BRE Pipeline Component instead for scenarios like this. I have never really understood the “crazy uncle in the basement” and avoid itineraries etc like the plague.
The rest of this post will focus on how to do all this using the BRE pipeline component. If you want to do it in the ESB toolkit then you will have to work it out by yourself.
BRE Pipeline Component Framework solution
This is example is taken from one of our production solutions. Firstly create your send port with the base url. Note the custom send pipeline and receive pipeline are specific to our implementation but have nothing to do with the assigning the resources. You can replace these with any pipeline that you like.
Secondly select configure button and configure the dynamic resources in HTTP Mapping and URL box.
There a five distinct REST resources as shown below. All you need to do now is to assign the correct BTS.Operation context property.
Thus if a message is sent to this port with BTS.Operation equal to “AllOrders” a GET request is sent to https://apiappserver/orders/for-supplier. Additionally if the message has a BTS.Operation equal to “Invoice” then the body of the request is sent to https://appiappserver/order/invoice.
Thirdly send port subscribes to a message in the message box like this.
Finally the BTS.Operation property can be set either;
- On a direct port in any orchestration
- or in a receive pipeline using the BRE Pipeline Component framework
Setting BTS.Operation in BRE Pipeline Component framework
Firstly, create a custom pipeline that contains the BRE pipeline component framework like this. Note you can leave out all the other stages except the XML dissembler. In the my case I needed to do a bit of extra work parse some json into xml and saving a back up.
Secondly write a business rule in the business rules composer like this. The example below assigns the value of “AllOrders” to BTS.Operation.
Finally after the messages goes to the message box the send port will subscribe and send it to https://apiappserver/orders/for-supplier as above.
Conclusion
BizTalk Server can be configured send to different resources that share the same base URL An example using BRE pipeline component to assign the “Operation” context was shown. The value of “Operation” resolves to an action that ultimately yields the final rest resource dynamically.
Finally, I have not answered the question on how to handle different JSON responses because it is not easy to do this. In my case I always get the same JSON ACK/NACK message and don’t have to worry about this.
If you want to do this then look at this old post by Johann Cooper. The hard part is how you decide from the response payload which JSON decoder to run. Once you know this you can use the BRE Pipeline Component once again to dynamically decode the JSON. This is probably worth another post but I have run out of time today.