How to: Implement Content-Based Routing Using a Business Rules Policy for a Known Message Type
Goal
This section demonstrates how to create an itinerary that dynamically routes a message, based on the content of a known message type (schema deployed to the Microsoft BizTalk Server Configuration database), using a business rules policy.
In this How-to topic, you will complete the following steps:
Create a business rules policy that will be used to route a message based on content.
Create an itinerary routing slip with a BRE resolver to dynamically route a message.
Test the itinerary using the Itinerary Test Client sample application.
Prerequisites
The procedures in this How-to topic require the completion of the Prerequisites for the Development Activities.
Before You Begin
Complete the following tasks before you perform the steps later in this How-to topic:
Create the GlobalBank West test message.
Create the GlobalBank East test message.
The following procedures describe how to do each of these.
To create the GlobalBank West test message
In Windows Explorer, browse to C:\HowTos.
Create a copy of NAOrderDoc.xml, and then name the copy West.xml.
In Notepad, open West.xml, and then change the value of the customerName element to GlobalBankWest.
Save West.xml as UTF-8, and then close Notepad.
To create the GlobalBank East test message
In Windows Explorer, browse to C:\HowTos.
Create a copy of NAOrderDoc.xml, and then name the copy East.xml.
In Notepad, open East.xml, and then change the value of the customerName element to GlobalBankEast.
Save East.xml as UTF-8, and then close Notepad.
Steps
To create a BRE policy to route using custom message properties
This rule will use the customer's name to dynamically set the endpoint used to route the message.
Click Start on the taskbar, point to All Programs, point to BizTalk Server, and then click Business Rule Composer.
In Policy Explorer, right-click Policies, and then click Add New Policy. Name the policy RouteBasedOnCustomerKnownType.
To add a routing rule for customer GlobalBank West
In the RouteBasedOnCustomerKnownType policy, right-click Version 1.0 (not saved), and then click Add New Rule. Name the rule SetWestEndpoint.
In Facts Explorer, click the XML Schemas tab, right-click Schemas, and then click Browse.
In the Schema Files dialog box, browse to C:\Projects\Microsoft.Practices.ESB\Source\Samples\DynamicResolution\Source\ESB.DynamicResolution.Schemas, select NAOrderDoc.xsd, and then click Open.
Note
This is the schema that defines the NAOrderDoc.xml message, which was used to create the West and East messages you will use for testing.
In Facts Explorer, click NAOrderDoc.xsd, and then in the Properties pane, click the Document Type property, and then type GlobalBank.ESB.DynamicResolution.Schemas.NAOrderDoc.
Note
This is the fully qualified name of the schema.
In Facts Explorer, expand NAOrderDoc.xsd, and then expand OrderDoc.
In the Rule window, right-click Conditions, point to Predicates, and then click Equal.
From Facts Explorer, drag the customerName element to the argument1 node under Conditions.
Click the argument2 node, and then type GlobalBankWest.
In Facts Explorer, click the Vocabularies tab, expand ESB.EndPointInfo, and then expand Version 1.0.
Note
The Microsoft BizTalk ESB Toolkit includes several vocabularies that can be used for creating rules for use in the ESB. Some of these should be replaced or augmented with your own vocabularies. For example, the DynamicRunTimeMaptypes has definitions for the maps provided in the GlobalBank samples.
From Facts Explorer, drag the Set End Point Outbound Transport Location definition to Actions.
Click <empty string> and then type C:\HowTos\Out\West%MessageID%.xml.
From Facts Explorer, drag the Set End Point Outbound Transport Type definition to Actions.
In Facts Explorer, expand ESB.TransportTypes, expand Version 1.0, and then drag the Adaptor Providers definition to <empty string>.
In the Actions pane, expand the Adaptor Providers drop-down list, and then click FILE.
To add a routing rule for customer GlobalBank East
In Policy Explorer, right-click the SetWestEndpoint rule, and then click Copy.
Right-click Version 1.0 (not saved), and then click Paste.
In the New Rule Name dialog box, type SetEastEndpoint, and then click OK.
In Policy Explorer, click the SetEastEndpoint rule.
In the Conditions section, right-click GlobalBankWest, and then click Reset argument.
Click argument2, and then type GlobalBankEast.
In the Actions section, right-click C:\HowTos\Out\West%MessageID%.xml, and then click Reset argument.
Click <empty string>, and then type C:\HowTos\Out\East%MessageID%.xml.
To add a routing rule for unknown customers
In the RouteBasedOnCustomerKnownType policy, right-click Version 1.0 (not saved), and then click Add New Rule. Name the rule SetUnknownCustomerEndpoint.
In the Rule window, right-click Conditions, and then click Add logical AND.
In the Rule window, right-click AND, point to Predicates, and then click NotEqual.
In Facts Explorer, click the XML Schemas tab, expand NAOrderDoc.xsd, and then expand OrderDoc.
From Facts Explorer, drag the customerName element to the argument1 node under Conditions.
Click the argument2 node, and then type GlobalBankWest.
In the Rule window, right-click AND, point to Predicates, and then click NotEqual.
From Facts Explorer, drag the customerName element to the argument1 node under Conditions.
Click the argument2 node, and then type GlobalBankEast.
In Facts Explorer, click the Vocabularies tab, expand ESB.EndPointInfo, and then expand Version 1.0.
From Facts Explorer, drag the Set End Point Outbound Transport Location definition to Actions.
Click <empty string>, and then type C:\HowTos\Out\CustomerUnknown%MessageID%.xml.
From Facts Explorer, drag the Set End Point Outbound Transport Type definition to Actions.
In Facts Explorer, expand ESB.TransportTypes, expand Version 1.0, and then drag the Adaptor Providers definition to <empty string>.
In the Actions pane, expand the Adaptor Providers drop-down list, and then click FILE.
To publish and deploy the policy
In Policy Explorer, under the RouteBasedOnCustomerKnownType policy, right click Version 1.0 (not saved), and then click Publish.
In Policy Explorer, under the RouteBasedOnCustomerKnownType policy, right click Version 1.0 - Published, and then click Deploy.
To create an ESB itinerary DSL model
In Visual Studio, open C:\HowTos\Patterns\Patterns.sln.
In Solution Explorer, right-click the ItineraryLibrary project, point to Add, and then click New Itinerary.
In the Add New Item dialog box, in the Name box, type CbrKnownType, and then click Add.
To configure the properties of the itinerary
In Visual Studio, click the design surface of CbrKnownType.itinerary. In the CbrKnownType Properties window, configure the following properties:
In the Model Exporter drop-down list, click XML Itinerary Exporter.
In the Extender Settings section, next to the Itinerary XML file property, click the ellipsis button (...).
In the Select XML File dialog box, type C:\HowTos\Itineraries\CbrKnownType in the File name box, and then click Save.
Note
This step enables you to export the itinerary as XML to a local file location. By exporting an itinerary to a local file location, instead of to the itinerary database, enables testing of the itinerary using the ESB Test Client application. You will complete this process later in this How-to topic.
To define the structure of the itinerary
From the Toolbox, drag an On-Ramp model element to the design surface. In the OnRamp1 Properties window, configure the following properties:
Click the Name property, and then type ReceiveNAOrder.
In the Extender drop-down list, click On-Ramp ESB Extender.
In the BizTalk Application drop-down list, click Microsoft.Practices.ESB.
In the Receive Port drop-down list, click OnRamp.Itinerary.
From the Toolbox, drag an Off-Ramp model element to the design surface, and then place it to the right of the ReceiveNAOrder model element. In the OffRamp1 Properties window, configure the following properties:
Click the Name property, and then type SendRegionalOrders.
In the Extender drop-down list, click Off-Ramp ESB Extender.
In the BizTalk Application drop-down list, click GlobalBank.ESB.
In the Send Port drop-down list, click DynamicResolutionOneWay.
From the Toolbox, drag an Itinerary Service model element to the design surface, and then place it between the ReceiveNAOrder model element and the SendRegionalOrders model element. In the ItineraryService1 Properties window, configure the following properties:
Click the Name property, and then type SendPortFilter.
In the Itinerary Service Extender drop-down list, click Off-Ramp Extender.
In the Off-Ramp drop-down list, expand SendRegionalOrders, and then click Send Handlers.
Right-click the Resolver collection of the SendPortFilter model element, and then click Add new Resolver. In the Resolver1 Properties window, configure the following properties:
Click the Name property, and then type RouteRegionalOrders.
In the Transport Name drop-down list, click BRE.
In the Resolver Implementation drop-down list, click Bre Resolver Extension.
In the Policy drop-down list, click RouteBasedOnCustomerKnownType.
In the Recognize Message Format drop-down list click True.
In the UseMsg drop-down list, click True.
Note
If you use the BRE Resolver Extension from orchestration, the recognizeMessageFormat property must be set to False.
In the Toolbox, click Connector. Drag a connection from the ReceiveNAOrder model element to the SendPortFilter model element.
In the Toolbox, click Connector. Drag a connection from the SendPortFilter model element to the SendRegionalOrders model element.
To export the model for use with the Itinerary Test Client
In Visual Studio, right-click the design surface of the CbrKnownType itinerary, and then click Export Model.
Note
The XML version of the itinerary opens in Visual Studio.
Save all project artifacts.
In Windows Explorer, browse to C:\HowTos\Itineraries and notice the creation of your itinerary XML (CbrKnownType.xml).
To test the itinerary and business rules
Open the Itinerary Test Client sample application using the shortcut created during the Prerequisites for the Development Activities (C:\HowTos\ESB.Itinerary.Test.exe - Shortcut).
In the Itinerary Test Client, clear the Use WCF Service check box, and then click Load Itinerary.
In the Open Itinerary File dialog box, browse to C:\HowTos\Itineraries. Select CbrKnownType.xml, and then click Open to load the itinerary.
Click OK to clear the Itinerary Loaded Successfully message.
In the Itinerary Test Client, click the ellipsis button (...) next to the Load Message box.
In the Select XML Document to load dialog box, browse to C:\HowTos. Select West.xml, and then click Open to load the test message.
Click the Submit Request button. When the test completes, click OK to dismiss the confirmation that appears.
In Windows Explorer, browse to C:\HowTos\Out. Verify that the West%MessageID%.xml message has been written to the directory.
Repeat the test process using the East.xml message.
In Windows Explorer, browse to C:\HowTos\Out. Verify that the East%MessageID%.xml message has been written to the directory.
Repeat the test process using the NAOrderDoc.xml message.
In Windows Explorer, browse to C:\HowTos\Out. Verify that the CustomerUnknown%MessageID%.xml message has been written to the directory.
Additional Resources
For more information, see the following related topics: