Dynamic routing in the OSB

In this post I will show how it is possible to make a dynamic routing in the OSB. You can use this if you need to route to different BusinessServices dependant of information you have. First we have to create a ProxyService CustomerService. As a last step insert an Routing element with a Dynamic Routing in it.

Route2Customers

We also create 2 BusinessServices where BS_CustomerA calls customers A service and BS_CustomerB calls customers B service. They both have an operation called Order. Dependant on certain information in our process we want to route to customer A or customer B. How do we do this? We will have to create a XQuery which will determine which BusinessService to call. This will have to look something like this:

(:: pragma type="ctx:route" ::)

declare namespace xf = "http://www.redrock.nl/test/routingexample/";
declare namespace ctx = "http://www.bea.com/wli/sb/context";

declare function xf:mapping ()
as element(*) {
	<mappings>
		<mapping>
			<Customer>A</Customer>
			<Service>CustomerService/business/CustomerServiceA</Service>
		</mapping>
		<mapping>
			<Customer>B</Customer>
			<Service>CustomerService/business/CustomerServiceB</Service>
		</mapping>
	</mappings>
};

declare function xf:checkDefault($value as xs:string*)
as xs:string {
	if (exists($value))
	then $value
	else xf:mapping()/mapping[Customer="A"]/Service/text()
};

declare function xf:selectBusinessService($customer as xs:string)
as xs:string {
	xf:checkDefault(xf:mapping()/mapping[Customer=$customer]/Service/text())
};

declare function xf:DetermineCustomerEndpoint($customer as xs:string)
as element(*) {
	<ctx:route>
		<ctx:service isProxy="false">{ xf:selectBusinessService($customer) }</ctx:service>
		<ctx:operation>Order</ctx:operation>
	</ctx:route>
};

declare variable $customer as xs:string external;

xf:DetermineCustomerEndpoint($customer)

This piece of XQuery will set the mapping to the CustomerA businessservice if the customer string is A and to CustomerB business service if the customer is B. If the value is empty, it will also choose Customer A. Now we only need to make the Dynamic Routing make use of this XQuery. We can do this by selecting the xquery and setting the customer variable.

xqueryl

If $customer is ‘A’ it will call BS_CustomerA and if $customer is ‘B’ it will call BS_CustomerB and if empty it will also call BS_CustomerA.

Leave a Reply

Your email address will not be published. Required fields are marked *

*