When you have repeating code segments of ESB configurations/XMLs in your API or proxy service, you can add those code segments in a template defined in ESB. And also when you want to reuse a particular set of codes in different places you can include that part of XML configuration in a template and reuse. It will help to reduce the redundancy and your code will be more readable. A template is more similar to a class.
Basically there are two types of template patterns introduced by WSO2 ESB. They are sequence templates and endpoint templates. Here I will discuss about sequence template
A sequence template is a templated form of a sequence. Here I’m going to discuss how to call a template from a proxy service, pass parameters to template and to retrieve parameters from a template. And you can include the repeating code segments inside the template.
To create a template go to ‘Templates’ and click on ‘Add Sequence Template’. Then give a name to that.
Then click on ‘Add parameter’ to retrieve the parameters you pass when calling the template.
Then click on ‘Add parameter’ to retrieve the parameters you pass when calling the template.
Here I have given two parameters (name and age). And then I will add a log mediator to log the parameter values retrieved from the template. For that you can simply click on ‘Add child’ and select the ‘Log’ mediator.
And give the properties you want to log like this.
Then click on ‘update’ then ‘Save & Close’. You can include the other relevant mediators of the XML configuration(repeating codes) here.
This is the final XML configuration of the template I created. In here we get the parameters passed from the proxy service which invokes this template and log them.
<?xml version="1.0" encoding="UTF-8"?> <template xmlns="http://ws.apache.org/ns/synapse" name="testTemplate"> <parameter name="name" /> <parameter name="age" /> <sequence> <log level="custom"> <property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns2="http://org.apache.synapse/xsd" expression="get-property('name')" name="name" /> <property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns2="http://org.apache.synapse/xsd" expression="get-property('age')" name="age" /> </log> </sequence> </template>
Then we will look into how to call this template from the proxy service and pass parameter
Create a custom proxy and give a name. Then click ‘Next’. Then select ‘Define Inline’. Then click on ‘Create’. After that click on ‘Add child’ then select ‘Call Template’.
There you can select the template you created in ‘Target template’. Then you can give the values of parameters like this.
If the values of those parameters are as properties in the proxy service you can give the parameter values like this instead of directly giving the values.
Then update, save and close. And click ‘Next’ and “Finish’ to create the proxy.
Here I have added the XML configuration of the proxy service.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="testTemplate" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="name" value="Sandra" scope="default" type="STRING" />
<property name="age" value="25" scope="default" type="STRING" />
<call-template target="testTemplate">
<with-param xmlns:ns="http://org.apache.synapse/xsd" name="name" value="{get-property('name')}" />
<with-param xmlns:ns="http://org.apache.synapse/xsd" name="age" value="{get-property('age')}" />
</call-template>
</inSequence>
</target>
<description />
</proxy>
Then you can run the proxy from ‘Try this Service’. Then you can see the output like this.
No comments:
Post a Comment