Making sequential calls using Oracle Mediator

Many people are unaware that Oracle mediator can be configured to make sequential calls.
This posting presents an example of a mediator component  that calls a web service,
then combines the web service response data with the original mediator request data
and calls a File adapter with the combined payload.

The composite.xml for this example looks this

composite.xml

The processing is completed using a single Mediator component with a single Routing Rule as follows:

Static Rules



Here are the steps to create the example.


Create a New Application and SOA Project

First we'll create a new Application
In JDeveloper Choose File->New Application

New SOA App

Name the application MediatorExamples or choose your own name.

Select Next.
Set the SOA Project name to MediatorSerialCallsExample.

New SOA Proj

Press the Finish button to create the application and create the project.

Add a XML Schema

Before we add the mediator we need to define the type that will be used in its interface.
We will create a new XML Schema to define the type.

In the Application Explorer,
Right Click on the Newly created MediatorSerialCallsExample project and select the New option
Under the General category select XML and in the items pane on the right select 'XML Schema'
Name the new XSD file EmployeeRequest.xsd
Edit the new file and replace all lines by pasting in the XSD contents from the following link.
Then save the newly created file.

EmployeeRequest.xsd

Add a Mediator

Open the composite.xml file
Drag a Mediator control from the Component Palette to the center column of the composite diagram.

Name the Mediator component Mediator1
Choose the One way interface option in the template dropdown box.
An input text box should appear, select the magifying glass icon and locate
the EmployeeRequest Schema in the project
(we just added it in the previous set), expand and select the EmployeeRequest element.

New Mediator

Press OK to create mediator


Create a new Web Service

Next we will create a new Web Service that the mediator will invoke later.
To create the simple web service follow the steps in this post.

  Follow the steps in this post to create or download the Web service

Add a web service partner link to the composite

Open the composite.xml file and drag the a Web Service icon from the component palette to the
External References column of the composite diagram.
Name the service: departmentFinder
in the WSDL url field paste the address for the remote wsdl definiion.
For example
http://localhost:7001/MyDemo-DepartmentFinder-context-root/DepartmentFinderPort?wsdl
If the url is unkown, it can be located in Enterprise Manager console. See the 'Create a new Web Service' article at the link above.   

Next press Port dropdown, (Don't press the green finder icon),
the dropdown should auto fill based on the wsdl resolution.
Check the 'copy wsdl into project' check box, to avoid annoying errors in the future when opening the JDeveloper
project is opened without the web service endpoint being available.
Select NEVER for transaction and press the OK button.
Press OK to localize project references to point to the local wsdl now imported into project.

Next return to the composite diagram click and drag a
connnct wire from departmentfinder partner link to Mediator.

Add a  file adapter partner link to the composite

Next drag a File Adapter to the External References column of the composite.xml diagram.
Set the value of the ServiceName to  WriteEmployeeDetails and press the Next button
Select Define from operation and schema later
Select the Write file operation.
Pick a file location where files will be written, for example /tmp  or c:\temp
Set the file name pattern to emp_%SEQ%.txt
At step 6 in wizard click the magnifying glass and
select the EmployeeRequest element
Then press the finish button.
Return to the Composite diagram editor and drag a wire the File adapter partner link to the mediator

The composite diagram should now match the first diagram shown at the beginning of this article.

Configure the Mediator Routing Rules

Open the coposite diagram and double click on the mediator component to open the mediator editor.
Two Static Routing rules should exist.,
One rule to call the Web Service, which has a Request section, a Synchronous reply section and a Fault section.
And another rule to call the File Adapter which does not have a Synchronous reply or Fault section.

Rule 1
First we will configure the Web Service Call Rule.
Click on the black X transform icon beside the 'Transform Using' field in the section with a target operation
of departmentFinder::getDepartmentDetails

request_xform

The Request Transformation MapDialog should appear: 
Select Create new Mapper File and accept the default name   "EmployeeRequest_To_getDepartmentDetails.xsl"
Press the OK button to create the transform.

In XSL mapper map eDept on left to arg0 on right.
This will pass the department number input value to the web service.

Left Side                               Right Side
inp1:eDeptNum      ----      arg0


xslt
Save and Close the transformation file.

Now still on rule 1, click the gear icon beside the <<Target Operation>> in the Synchronous Reply step.

Sync_reply

The Select Target Type Dialog will appear.
Choose Service, then select expand WriteEmployeeDetails and choose the Write operation.

Write_operation

This instructs mediator to call another service after receiving the response from the first service.
We have the option to send only the response data from the first call to the second endpoint or we can
combine the response from the web service with the original mediator request data.

We will combine the response data with the request data that was passed to the mediator.
Click the Black X icon in the Sync Response Section to create a new XSLT transform
Check the Create new mapper file radio button and accept the default name  getDepartmentDetailsResponse_To_EmployeeRequestForFile.xsl
IMPORTANT: Check the box "Include Request in the Reply Payload"

Press the OK button to launch the xform editor.

reply_xform



Now in transformeditor you will see two sources on the left side, the originial request sent to the mediator and
the response from the web service the mediator, for example

<sources>
   initialRequest
       inp1:EmployeeRequest          (the initial request into the mediator)
         eNumber
         eFirstName
         eLastName
         eUserId
         eDeptNum
         eDeptName
         eDeptOrg
         eDeptCostCtr
 
   tns:getDepartmentDetailsResponse   (the response from the web service)
       return
         departmentCostCetner
         departmentManagerEmail
         departmentName
         departmentNumber
         departmentOrg
      
Using the Mapper, map the following fields

Left Side                                         Right Side
eNumber                             ----     eNumber
eFirstName                        ----      eFirstName
eLastName                        ----      eLastName
eUserId                               ----      eUserId
eDeptNum                          ----     eDeptNum

departmentCostCenter   -----     eDeptCostCtr
deparmentName              -----     eDeptName
departmentOrg                 -----      eDeptOrg

The completed transformation is shown in the diagram below:

combine_xform

Finally, save and close transform editor


Rule 2

Return to the mediator editor and locate rule 2.
This rule was created automatically when the File Adapter Partnerlink was associated with the Mediator by dragging a wire between
the components in the composite diagram.

The second rule is not necessary in this application, since we never expect the mediator to call the file adapter without first
calling the web service.
Click on the second rule which and choose the red X icon top right of the mediator editor window to delete the rule.

The final routing section for the mediator should appear as follows:

final_routing_rules


Save all project files by choosing File->Save All from the main menu


Compile and Deploy

Compile the project by right clicking on the MediatorSerialCallsExample project in the Application Explorer.
Ensure there no errors are listed in the  JDeveloper Compiler-log window.

Deploy the project to a SOA Partition on a running WLS Server by right clicking on the project and choosing the deploy ->
Deploy to Application Server option.


Testing using Enterprise Manager

Open the Enterprise Manager console using a browser.
For example http://localhost:7001/em

Expand the Farm node and locate the mediatorSerialCallsExample composite in Enterprise Manager.
Press the test button.

Supply values for the mandatory fields (prefixed with *)
be sure to use a valid dept number value of 1,2 or 3
Do not enter values for depname, org and cost ctr
they will be added by data returned by the web service

Press the "Test Web Service"

Note: The Mediator is configured to use a one way invocation pattern, so no response will be returned in EM.
After the test, verify the results by viewing the file created by file adapter in the specified /tmp or c:\temp directory.

For example

[/tmp] $ more emp_1.txt
<?xml version="1.0" encoding="UTF-8" ?><inp1:EmployeeRequest xmlns:inp1="http://
www.example.org" xmlns="http://www.example.org">
   <inp1:eNumber>123</inp1:eNumber>
   <inp1:eFirstName>Bob</inp1:eFirstName>
   <inp1:eLastName>Webster</inp1:eLastName>
   <inp1:eUserId/>
   <inp1:eDeptNum>2</inp1:eDeptNum>
   <inp1:eDeptName>Inside Sales</inp1:eDeptName>
   <inp1:eDeptOrg>Sales</inp1:eDeptOrg>
   <inp1:eDeptCostCtr>22</inp1:eDeptCostCtr>
</inp1:EmployeeRequest>

The result file should have the combined department information.


The completed example can be downloaded as a zip from GitHub  Here