Reusing Mediation Sequences¶
What you'll build¶
In this sample scenario, you will use a Sequence Template and reuse it in multiple places of the medation flow. You can reuse the mediation flow that was defined in the Sending a Simple Message to a Service tutorial and then replace its sections with the sequence template . See Creating Templates for details on how to work with templates using WSO2 Integration Studio.
Let's get started!¶
Step 1: Set up the workspace¶
To set up the tools:
- Go to the product page of WSO2 Micro Integrator, download the product installer and run it to set up the product.
- Download the relevant WSO2 Integration Studio based on your operating system. The path to the extracted/installed folder is referred to as
MI_TOOLING_HOMEthroughout this tutorial.
- Download the CLI Tool for monitoring artifact deployments.
To set up the previous artifacts:
- If you did not try the Sending a Simple Message to a Service tutorial yet, open WSO2 Integration Studio, click File, and click Import. Next, expand the WSO2 category and select Existing WSO2 Projects into workspace, click Next and upload the pre-packaged project.
- Download the JAR file of the back-end service from here.
Step 2: Develop the integration artifacts¶
Create a Sequence Template¶
Once you have exported the ESB project as described above, the project directory will appear with the artifacts as shown below.
Right-click on SampleServices and navigate to New -> Template . The New Template Artifact dialog will open.
- Select the Create a New Template and click Next.
Enter the following details and click Finish.
Parameter Description Template Name HospitalRoutingSeq Template Type Sequence Template
The template artifact will open in the canvas as shown below.
Open the Properties tab of the sequence template by clicking on the canvas (outside the sequence box).
Click the icon to start adding parameters .
In the Template Parameter dialog that opens, enter 'sethospital' as the parameter name and click Finish .
Add a Log mediator to the sequence template as shown below. This will print a message indicating to which hospital a requested message is routed.
Open the Properties tab of the log mediator and specify the following:
Property Description Log Category INFO Log Level CUSTOM
Click the icon to start defining a property. Then add the following details for the property:
Property Name Description Name message Type EXPRESSION Property Expression
fn:concat('Routing to ', get-property('Hospital'))
We select EXPRESSION because the required properties for the log message must be extracted from the request, which we can do using an XPath expression.
Add a Property mediator just after the Log mediator to store the value for uri.var.hospital.
With the Property mediator selected, access the Properties tab and enter the information given below:
Property Description Property Name Select New Property New Property Name uri.var.hospital URI Template Select set Value Type Select EXPRESSION Property Data Type Select STRING Value Expression
Description Set Hospital Variable
Update the mediation flow¶
Open the design view of the HealthcareAPI.xml and delete 'GrandOak' Log mediator by right clicking the mediator and selecting Delete from Model .
Delete the 'Set Hospital Variable' Property mediator.
Add a Call Template mediator to the sequence as shown below.
Open the Properties tab of the Call Template mediator and select ' HospitalRoutingSeq' from the list of available templates.
Click the icon to start adding parameters. Enter the following parameter details and click Finish .
Parameter Description Parameter Name sethospital Parameter Type value Value/Expression grandoaks
Repeat the above steps to add Call Templates for 'Clemency' and 'Pine Valley' hospitals. Add clemency and pinevalley as the respective parameter values.
Save the configuration.
Step 3: Package the artifacts¶
Package the artifacts in your composite application project (SampleServicesCompositeApplication project) to be able to deploy the artifacts in the server.
- Open the
pom.xmlfile in the composite application project POM editor.
Ensure that the following artifacts are selected in the POM file.
Save the project.
Step 4: Build and run the artifacts¶
To test the artifacts, deploy the packaged artifacts in the embedded Micro Integrator:
- Right-click the composite application project and click Export Project Artifacts and Run.
- In the dialog that opens, select the composite application project that you want to deploy.
- Click Finish. The artifacts will be deployed in the embedded Micro Integrator and the server will start. See the startup log in the Console tab.
Step 5: Testing the use case¶
Let's use the CLI Tool to find the URL of the REST API that is deployed in the Micro Integrator:
- Open a terminal and navigate to the
- Execute the following command to start the tool:
- Execute the following command to find the APIs deployed in the server:
mi show api
Send a simple request to invoke the service.
Open a command line terminal and execute the following command from the location where the request.json file you created is saved:
curl -v -X POST --data @request.json http://localhost:8280/healthcare/categories/surgery/reserve --header "Content-Type:application/json"
You will see the response in the command line terminal.