<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="StockQuoteProxy"> <target> <inSequence> <!--use sequence template to trasnform incoming request--> <call-template target="xslt_func"> <with-param name="xslt_key" value="xslt-key-req"/> </call-template> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </inSequence> <outSequence> <!--use sequence template to trasnform incoming response--> <call-template target="xslt_func"> <with-param name="xslt_key" value="xslt-key-back"/> </call-template> <send/> </outSequence> </target> </proxy> <!--this sequence template will trasnform requests with the given xslt local entry key And will log the message before and after. Takes Iterate local entry key as an argument--> <template xmlns="http://ws.apache.org/ns/synapse" name="xslt_func"> <parameter name="xslt_key"/> <sequence> <log level="full"> <property name="BEFORE_TRANSFORM" value="true" /> </log> <xslt key="{$func:xslt_key}"/> <log level="full"> <property name="AFTER_TRANSFORM" value="true" /> </log> </sequence> </template> <localEntry key="xslt-key-req" src="file:repository/samples/resources/transform/transform.xslt"/> <localEntry key="xslt-key-back" src="file:repository/samples/resources/transform/transform_back.xslt"/> </definitions>
Objective: Introduction to ESB Sequence Templates
Prerequisites:
Start the Synapse configuration numbered 750: i.e. wso2esb-samples -sn 750
Start the Axis2 server and deploy the SimpleStockQuoteService if not already
done
Execute the stock quote client by requesting for a stock quote on the proxy service as follows:
ant stockquote -Daddurl=http://localhost:8280/services/StockQuoteProxy -Dmode=customquote
ESB Sequence Template can act a reusable function. Here proxy service reuses template xslt_func which will transform requests with the given xslt local entry key And will log the message before and after. It takes xslt transformation corresponding to local entry key as an argument (for insequence this key is xslt-key-req and out sequence it is xslt-key-back). We use call-template mediator for passing the xslt key parameter to a sequence template. ESB console will display how the custom stockquote is transformed and the transformed response from the service.
<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="SplitAggregateProxy"> <target> <inSequence> <!--use iterate sequence template to split incoming request and send--> <call-template target="iter_func"> <with-param xmlns:m0="http://services.samples" name="iter_expr" value="{{//m0:getQuote/m0:request}}"/> <with-param xmlns:m0="http://services.samples" name="attach_path" value="{{//m0:getQuote}}"/> </call-template> </inSequence> <outSequence> <!--use aggregate sequence template to combine the responses and send back--> <call-template target="aggr_func"> <with-param xmlns:m0="http://services.samples" name="aggr_expr" value="{{//m0:getQuoteResponse}}"/> </call-template> </outSequence> </target> </proxy> <!--this sequence template will aggregate the responses , merge and send back to the client. This takes aggregate expression as an argument--> <template xmlns="http://ws.apache.org/ns/synapse" name="aggr_func"> <parameter name="aggr_expr"/> <sequence> <log level="full"/> <aggregate> <completeCondition> <messageCount min="-1" max="-1"/> </completeCondition> <onComplete expression="$func:aggr_expr"> <log level="full" /> <send/> </onComplete> </aggregate> </sequence> </template> <!--this sequence template will iterate through stock quote symbols ,split and send them to endpoints. Takes Iterate expression and soap attaach path as arguments --> <template xmlns="http://ws.apache.org/ns/synapse" name="iter_func"> <parameter name="iter_expr"/> <parameter name="attach_path"/> <sequence> <iterate xmlns:m0="http://services.samples" preservePayload="true" attachPath="$func:attach_path" expression="$func:iter_expr"> <target> <sequence> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </sequence> </target> </iterate> </sequence> </template> </definitions>
Objective: Demonstrate the use of tempaltes in Split/Aggregate scenario .
Prerequisites:Deploy the SimpleStockQuoteService in sample Axis2 server and start it on port 9000.
Start ESB with the sample configuration 751 (i.e. wso2esb-samples -sn 751).
This will be the same sample demonstrated in advanced mediation with Iterate mediator used to split the messages in to parts and process them asynchronously and then aggregate the responses coming in to ESB. Only addition will be two sequence templates 'iter_func' and 'aggr_func' which will split the message and aggregate the responses back , respectively.
Invoke the client as follows.
ant stockquote -Daddurl=http://localhost:8280/services/SplitAggregateProxy -Ditr=4
Sequence Template 'iter_func' takes iterate expressions and attach path as arguments.
Sequence Template 'aggr_func' takes aggregate expression as argument.
In this sample we are using special dynamic xpath expressions(in double curly braces {{expr}}) in call-template mediator so that they are evaluated only when iteration and aggregation is done. Note that function scope (ie:-$func) is used to access template parameters in 'iter_func' and 'aggr_func'.
<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="LBProxy" transports="https http" startOnLoad="true"> <target faultSequence="errorHandler"> <inSequence> <send> <endpoint> <session type="simpleClientSession"/> <loadbalance algorithm="org.apache.synapse.endpoints.algorithms.RoundRobin"> <endpoint name="templ_ep1" template="endpoint_template" uri="http://localhost:9001/services/LBService1"> <parameter name="suspend_duration" value="5"/> </endpoint> <endpoint name="templ_ep2" template="endpoint_template" uri="http://localhost:9002/services/LBService1"> <parameter name="suspend_duration" value="20"/> </endpoint> <endpoint name="templ_ep3" template="endpoint_template" uri="http://localhost:9003/services/LBService1"> <parameter name="suspend_duration" value="200"/> </endpoint> </loadbalance> </endpoint> </send> <drop/> </inSequence> <outSequence> <send/> </outSequence> </target> <publishWSDL uri="file:repository/samples/resources/proxy/sample_proxy_2.wsdl"/> </proxy> <sequence name="errorHandler"> <makefault> <code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/> <reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/> </makefault> <send/> </sequence> <template name="endpoint_template"> <parameter name="suspend_duration"/> <endpoint name="annonymous"> <address uri="$uri"> <enableAddressing/> <suspendDurationOnFailure>$suspend_duration</suspendDurationOnFailure> </address> </endpoint> </template> </definitions>
Objective: Demonstrate how to use endpoint templates in a load balance configuration .
Prerequisites:Sample setup is same as LoadBalance endpoint scenario (#154). Start the Synapse configuration numbered 752: i.e. wso2esb-samples -sn 752 Start the Axis2 server and deploy the LoadbalanceFailoverService if not already done
Configuration demonstrates a single endpoint template named 'endpoint_template'. Three endpoint templates are created inside loadbalance endpoint which will point to this template. Note different parameters passed on to the same template ie:- service url and suspend duration as parameters
Invoke the client as follows.
ant loadbalancefailover -Dmode=session -Dtrpurl=http://localhost:8280/services/LBProxy
As in the previous loadbalance samples client console will show loadbalance requests going towards three endpoints created from the same template. Notice differnt suspension times for three services in a failed endpoint scenario as well. (reflects different parameters passed onto the template)