1. Problem
You need to execute the same orchestration logic for two or more different schemas.
2. Solution
Instead of
creating separate orchestrations with identical logic, you can have one
orchestration that uses the Listen shape and multiple Receive shapes.
The Listen shape enables an orchestration to listen for any messages
matching the schemas of any Receive shapes within the Listen shape. A
Listen shape can contain multiple Receive shapes, all listening for
different messages. In this solution, we will look at how to create an
orchestration that listens for either of two messages to arrive.
The first step in the process is
to define two different schemas to represent the different documents
for which the orchestration will be listening. A typical example of this
would be two versions of the same schema, where the elements defined
differ enough to warrant a completely different schema. For instance,
several required nodes on version 1 may not exist on version 2. Another
example would be two different schemas representing similar data (such
as customer, order, and so on) from two different legacy systems. In
both cases, you would need to have both of the documents instantiate the
same orchestration and be subject to the same rules and workflow.
For this solution, we will assume the following two documents are being used, representing different versions of the Person schema:
<ns0:Person xmlns:ns0="http://SampleListenShape.Person.V2">
<ID/>
<Name/>
<Role/>
<Age/>
</ns0:Person>
<ns0:Person xmlns:ns0="http://SampleSolution.Person">
<ID/>
<FirstName/>
<MiddleName/>
<LastName/>
<Role/>
<Age/>
</ns0:Person>
In an empty orchestration, drop a Listen shape onto the design surface.
Add two Receive shapes within the Listen shape.
Set the Activate property on both Receive shapes to True.
Rename the Receive shapes to Receive_Ver_1 and Receive_Ver_2. This is for reference purposes only.
Create
two message variables in the orchestration (click the Orchestration
tab, right-click Messages, and select New Message), named msgVer1 and msgVer2, pointing them to the appropriate schema.
Set the message type of Receive_Ver_1 to msgVer1 and Receive_Ver_2 to msgVer2.
Add a new port to the Port Surface with the following properties (using the Port Configuration Wizard).
Name the new port Port_Receive_Incoming.
Create a new port type of PortType_ReceiveIncoming.
Select "I'll always receive messages on this port."
Set the port binding to Specify Later, or give a file path and set it to Specify Now.
Create two operations on the port (there will be one created automatically). Right-click the port, name the operation Operation_VI, and set the Message Type to msgVer1. Repeat this to add Operation_V2 with the Message Type set to msgVer2.
Add
a map to transform the version 1 documents into version 2. This will
allow the orchestration to work with one message type throughout.
Add a construct message with a Transform shape under the Receive_Ver_1 shape.
Set the source schema to msgVer1 and the destination schema to msgVer2. The orchestration will resemble Figure 1.
At this point, the orchestration can be deployed, bound to a receive port, and started. If either version of the Person schema is dropped on the MessageBox (via a port or directly bound), the orchestration will instantiate.
3. How It Works
This recipe demonstrated how to
uses the Listen shape with multiple Receive shapes. There are
alternative ways to use the Listen shape. One of the most common is a
combination of a Receive shape and a Delay shape. This can be used for
polling behavior in a long-running orchestration. For example, an
orchestration could be set up to listen for a document to be dropped on a
file directory. If a file is not dropped within a certain amount of
time (as specified in the Delay shape), a series of steps could take
place (such as notifying an administrator). By adding a Loop shape, the
orchestration could return to listening for the document to arrive. An
example of this is shown in Figure 2.