<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  ~  Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
  ~
  ~  WSO2 Inc. licenses this file to you under the Apache License,
  ~  Version 2.0 (the "License"); you may not use this file except
  ~  in compliance with the License.
  ~  You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing,
  ~  software distributed under the License is distributed on an
  ~  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~  KIND, either express or implied.  See the License for the
  ~  specific language governing permissions and limitations
  ~  under the License.
  -->

<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta content="text/html; charset=iso-8859-1" http-equiv="content-type"/>
  <title>WSO2 EI - Endpoint samples </title>
</head>

<body>
<h1>Running the Endpoint Mediation samples with WSO2 Enterprise Integrator (EI) </h1>

<h2><a name="Sample50">Sample 50: POX to SOAP conversion</a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
    &lt;!-- filtering of messages with XPath and regex matches --&gt;
    &lt;filter source="get-property('To')" regex=".*/StockQuote.*"&gt;
        &lt;send&gt;
            &lt;endpoint&gt;
                &lt;address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/&gt;
            &lt;/endpoint&gt;
        &lt;/send&gt;
        &lt;drop/&gt;
    &lt;/filter&gt;
    &lt;send/&gt;
&lt;/definitions&gt; </pre>

<p><strong>Objective: POX to SOAP conversion</strong> </p>

<p><strong>Prerequisites:</strong><br />
Start the Synapse configuration numbered 50: i.e. wso2EI-samples -sn 50 </p>

<p>Start the Axis2 server and deploy the SimpleStockQuoteService if not
already done </p>

<p>Execute the 'ant stockquote' specifying that the request should be a REST
request as follows: </p>
<pre xml:space="preserve">ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuote -Drest=true</pre>

<p>This example shows a http REST request (as shown below) being transformed
into a SOAP request and forwarded to the stock quote service. </p>
<pre xml:space="preserve">POST /services/StockQuote HTTP/1.1
Content-Type: application/xml; charset=UTF-8;action="urn:getQuote";
SOAPAction: urn:getQuote
User-Agent: Axis2
Host: 127.0.0.1
Transfer-Encoding: chunked

75
&lt;m0:getQuote xmlns:m0="http://services.samples/xsd"&gt;
   &lt;m0:request&gt;
      &lt;m0:symbol&gt;IBM&lt;/m0:symbol&gt;
   &lt;/m0:request&gt;
&lt;/m0:getQuote&gt;0</pre>

<h2><a name="Sample51">Sample 51: MTOM and SwA optimizations and
request/response correlation</a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
    &lt;in&gt;
        &lt;filter source="get-property('Action')" regex="urn:uploadFileUsingMTOM"&gt;
            &lt;property name="example" value="mtom"/&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;address uri="http://localhost:9000/services/MTOMSwASampleService" optimize="mtom"/&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;
        &lt;/filter&gt;
        &lt;filter source="get-property('Action')" regex="urn:uploadFileUsingSwA"&gt;
            &lt;property name="example" value="swa"/&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;address uri="http://localhost:9000/services/MTOMSwASampleService" optimize="swa"/&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;
        &lt;/filter&gt;
    &lt;/in&gt;
    &lt;out&gt;
        &lt;filter source="get-property('example')" regex="mtom"&gt;
            &lt;property name="enableMTOM" value="true" scope="axis2"/&gt;
        &lt;/filter&gt;
        &lt;filter source="get-property('example')" regex="swa"&gt;
            &lt;property name="enableSwA" value="true" scope="axis2"/&gt;
        &lt;/filter&gt;
        &lt;send/&gt;
    &lt;/out&gt;
&lt;/definitions&gt;</pre>

<p><strong>Objective: MTOM and SwA optimizations and request/response
correlation</strong> </p>

<p><strong>Prerequisites:</strong><br />
Start the Synapse configuration numbered 51: i.e. wso2EI-samples -sn 51<br />
Start the Axis2 server and deploy the MTOMSwASampleService if not already
done </p>

<p>Execute the 'ant optimizeclient' specifying MTOM optimization as follows:
</p>
<pre xml:space="preserve">ant optimizeclient -Dopt_mode=mtom</pre>

<p>The configuration now sets a local message context property, and forwards
the message to 'http://localhost:9000/services/MTOMSwASampleService' optimizing
binary content as MTOM. By sending this message through TCPMon you would be
able to see the actual message sent over the http transport if required. Thus
during response processing, by checking the local message property EI could
identify the past information about the current message context, and uses
this knowledge to transform the response back to the client in the same
format as the original request. </p>

<p>When the client executes successfully, it will upload a file containing
the ASF logo and receive its response back again and save it into a temporary
file. </p>
<pre xml:space="preserve">[java] Sending file : ./../../samples/service-bus/resources/mtom/asf-logo.gif as MTOM</pre>
<pre xml:space="preserve">[java] Saved response to file : ./../../work/temp/sampleClient/mtom-49258.gif</pre>

<p>Next try SwA as: </p>
<pre xml:space="preserve">ant optimizeclient -Dopt_mode=swa</pre>
<pre xml:space="preserve">[java] Sending file : ./../../samples/service-bus/resources/mtom/asf-logo.gif as SwA
[java] Saved response to file : ./../../work/temp/sampleClient/swa-47549.gif</pre>

<p>By using TCPMon and sending the message through it, one can inspect that
the requests and responses sent are indeed MTOM optimized or sent as http
attachments as follows: </p>
<pre xml:space="preserve">POST http://localhost:9000/services/MTOMSwASampleService HTTP/1.1
Host: 127.0.0.1
SOAPAction: urn:uploadFileUsingMTOM
Content-Type: multipart/related; boundary=MIMEBoundaryurn_uuid_B94996494E1DD5F9B51177413845353; type="application/xop+xml";
start="&lt;0.urn:uuid:B94996494E1DD5F9B51177413845354@apache.org&gt;"; start-info="text/xml"; charset=UTF-8
Transfer-Encoding: chunked
Connection: Keep-Alive
User-Agent: Synapse-HttpComponents-NIO

--MIMEBoundaryurn_uuid_B94996494E1DD5F9B51177413845353241
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID:
   &lt;0.urn:uuid:B94996494E1DD5F9B51177413845354@apache.org&gt;221b1
      &lt;?xml version='1.0' encoding='UTF-8'?&gt;
         &lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
            &lt;soapenv:Body&gt;
               &lt;m0:uploadFileUsingMTOM xmlns:m0="http://www.apache-synapse.org/test"&gt;
                  &lt;m0:request&gt;
                     &lt;m0:image&gt;
                        &lt;xop:Include href="cid:1.urn:uuid:78F94BC50B68D76FB41177413845003@apache.org" xmlns:xop="http://www.w3.org/2004/08/xop/include" /&gt;
                     &lt;/m0:image&gt;
                  &lt;/m0:request&gt;
               &lt;/m0:uploadFileUsingMTOM&gt;
            &lt;/soapenv:Body&gt;
         &lt;/soapenv:Envelope&gt;
--MIMEBoundaryurn_uuid_B94996494E1DD5F9B51177413845353217
Content-Type: image/gif
Content-Transfer-Encoding: binary
Content-ID:
         &lt;1.urn:uuid:78F94BC50B68D76FB41177413845003@apache.org&gt;22800GIF89a... &lt;&lt; binary content &gt;&gt;</pre>
<pre xml:space="preserve">POST http://localhost:9000/services/MTOMSwASampleService HTTP/1.1
Host: 127.0.0.1
SOAPAction: urn:uploadFileUsingSwA
Content-Type: multipart/related; boundary=MIMEBoundaryurn_uuid_B94996494E1DD5F9B51177414170491; type="text/xml";
start="&lt;0.urn:uuid:B94996494E1DD5F9B51177414170492@apache.org&gt;"; charset=UTF-8
Transfer-Encoding: chunked
Connection: Keep-Alive
User-Agent: Synapse-HttpComponents-NIO

--MIMEBoundaryurn_uuid_B94996494E1DD5F9B51177414170491225
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID:
   &lt;0.urn:uuid:B94996494E1DD5F9B51177414170492@apache.org&gt;22159
      &lt;?xml version='1.0' encoding='UTF-8'?&gt;
         &lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
            &lt;soapenv:Body&gt;
               &lt;m0:uploadFileUsingSwA xmlns:m0="http://www.apache-synapse.org/test"&gt;
                  &lt;m0:request&gt;
                     &lt;m0:imageId&gt;urn:uuid:15FD2DA2584A32BF7C1177414169826&lt;/m0:imageId&gt;
                  &lt;/m0:request&gt;
               &lt;/m0:uploadFileUsingSwA&gt;
            &lt;/soapenv:Body&gt;
         &lt;/soapenv:Envelope&gt;22--34MIMEBoundaryurn_uuid_B94996494E1DD5F9B511774141704912
17
Content-Type: image/gif
Content-Transfer-Encoding: binary
Content-ID:
         &lt;urn:uuid:15FD2DA2584A32BF7C1177414169826&gt;22800GIF89a... &lt;&lt; binary content &gt;&gt;</pre>

<h2><a name="Sample52">Sample 52: Session less load balancing between 3
endpoints</a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;sequence name="main" onError="errorHandler"&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;loadbalance&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9001/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendDurationOnFailure&gt;60&lt;/suspendDurationOnFailure&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9002/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendDurationOnFailure&gt;60&lt;/suspendDurationOnFailure&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9003/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendDurationOnFailure&gt;60&lt;/suspendDurationOnFailure&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                    &lt;/loadbalance&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;&lt;drop/&gt;
        &lt;/in&gt;

        &lt;out&gt;
            &lt;!-- Send the messages where they have been sent (i.e. implicit To EPR) --&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

    &lt;sequence name="errorHandler"&gt;

        &lt;makefault&gt;
            &lt;code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/&gt;
            &lt;reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/&gt;
        &lt;/makefault&gt;

        &lt;header name="To" action="remove"/&gt;
        &lt;property name="RESPONSE" value="true"/&gt;

        &lt;send/&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;</pre>

<p><strong>Objective: Demonstrate the simple load balancing among a set of
endpoints</strong> </p>

<p><strong>Prerequisites:</strong> </p>

<p>Start EI with sample configuration 52. (i.e. wso2EI-samples -sn 52) </p>

<p>Deploy the LoadbalanceFailoverService by switching to &lt;EI installation
directory&gt;/samples/axis2Server/src/LoadbalanceFailoverService directory
and running ant. </p>

<p>Start three instances of sample Axis2 server on HTTP ports 9001, 9002 and
9003 and give some unique names to each server. </p>

<p>Example commands to run sample Axis2 servers from the &lt;EI installation
directory&gt;/samples/axis2Server directory in Linux are listed below: </p>
<pre xml:space="preserve">./axis2server.sh -http 9001 -https 9005 -name MyServer1
./axis2server.sh -http 9002 -https 9006 -name MyServer2
./axis2server.sh -http 9003 -https 9007 -name MyServer3</pre>

<p>Now we are done with setting up the environment for load balance sample.
Start the load balance and failover client using the following command: </p>
<pre xml:space="preserve">ant loadbalancefailover -Di=100</pre>

<p>This client sends 100 requests to the LoadbalanceFailoverService through
EI. EI will distribute the load among the three endpoints mentioned in the
configuration in round-robin manner. LoadbalanceFailoverService appends the
name of the server to the response, so that client can determine which server
has processed the message. If you examine the console output of the client,
you can see that requests are processed by three servers as follows: </p>
<pre xml:space="preserve">[java] Request: 1 ==&gt; Response from server: MyServer1
[java] Request: 2 ==&gt; Response from server: MyServer2
[java] Request: 3 ==&gt; Response from server: MyServer3
[java] Request: 4 ==&gt; Response from server: MyServer1
[java] Request: 5 ==&gt; Response from server: MyServer2
[java] Request: 6 ==&gt; Response from server: MyServer3
[java] Request: 7 ==&gt; Response from server: MyServer1
...</pre>

<p>Now run the client without the -Di=100 parameter to send infinite
requests. While running the client shutdown the server named MyServer1. You
can observe that requests are only distributed among MyServer2 and MyServer3
after shutting down MyServer1. Console output before and after shutting down
MyServer1 is listed below (MyServer1 was shutdown after request 63): </p>
<pre xml:space="preserve">...
[java] Request: 61 ==&gt; Response from server: MyServer1
[java] Request: 62 ==&gt; Response from server: MyServer2
[java] Request: 63 ==&gt; Response from server: MyServer3
[java] Request: 64 ==&gt; Response from server: MyServer2
[java] Request: 65 ==&gt; Response from server: MyServer3
[java] Request: 66 ==&gt; Response from server: MyServer2
[java] Request: 67 ==&gt; Response from server: MyServer3
...</pre>

<p>Now restart MyServer1. You can observe that requests will be again sent to
all three servers roughly after 60 seconds. This is because we have specified
&lt;suspendDurationOnFailure&gt; as 60 seconds in the configuration.
Therefore, load balance endpoint will suspend any failed child endpoint only
for 60 seconds after detecting the failure. </p>

<h2><a name="Sample53">Sample 53: Failover sending among 3 endpoints</a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;sequence name="main" onError="errorHandler"&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;failover&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9001/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendDurationOnFailure&gt;60&lt;/suspendDurationOnFailure&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9002/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendDurationOnFailure&gt;60&lt;/suspendDurationOnFailure&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9003/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendDurationOnFailure&gt;60&lt;/suspendDurationOnFailure&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                    &lt;/failover&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;&lt;drop/&gt;
        &lt;/in&gt;

        &lt;out&gt;
            &lt;!-- Send the messages where they have been sent (i.e. implicit To EPR) --&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

    &lt;sequence name="errorHandler"&gt;

        &lt;makefault&gt;
            &lt;code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/&gt;
            &lt;reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/&gt;
        &lt;/makefault&gt;

        &lt;header name="To" action="remove"/&gt;
        &lt;property name="RESPONSE" value="true"/&gt;

        &lt;send/&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;</pre>

<p><strong>Objective: Demonstrate the failover sending</strong> </p>

<p><strong>Prerequisites:</strong> </p>

<p>Start EI with sample configuration 53 (i.e. wso2EI-samples -sn 53) </p>

<p>Deploy the LoadbalanceFailoverService and start three instances of sample
Axis2 server as mentioned in sample 52. </p>

<p>Above configuration sends messages with the failover behavior. Initially
the server at port 9001 is treated as primary and other two are treated as
back ups. Messages are always directed only to the primary server. If the
primary server has failed, next listed server is selected as the primary.
Thus, messages are sent successfully as long as there is at least one active
server. To test this, run the loadbalancefailover client to send infinite
requests as follows: </p>
<pre xml:space="preserve">ant loadbalancefailover</pre>

<p>You can see that all requests are processed by MyServer1. Now shutdown
MyServer1 and inspect the console output of the client. You will observe that
all subsequent requests are processed by MyServer2. </p>

<p>The console output with MyServer1 shutdown after request 127 is listed
below: </p>
<pre xml:space="preserve">...
[java] Request: 125 ==&gt; Response from server: MyServer1
[java] Request: 126 ==&gt; Response from server: MyServer1
[java] Request: 127 ==&gt; Response from server: MyServer1
[java] Request: 128 ==&gt; Response from server: MyServer2
[java] Request: 129 ==&gt; Response from server: MyServer2
[java] Request: 130 ==&gt; Response from server: MyServer2
...</pre>

<p>You can keep on shutting down servers like this. Client will get a
response till you shutdown all listed servers. Once all servers are shutdown,
the error sequence is activated and a fault message is sent to the client as
follows. </p>
<pre xml:space="preserve">[java] COULDN'T SEND THE MESSAGE TO THE SERVER.</pre>

<p>Once a server is detected as failed, it will be added to the active
servers list again after 60 seconds (specified in
&lt;suspendDurationOnFailure&gt; in the configuration). Therefore, if you
have restarted any of the stopped servers and have shutdown all other
servers, messages will be directed to the newly started server. </p>

<h2><a name="Sample54">Sample 54: Session affinity load balancing between 3
endpoints</a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;sequence name="main" onError="errorHandler"&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;!-- specify the session as the simple client session provided by Synapse for
                    testing purpose --&gt;
                    <strong>&lt;session type="simpleClientSession"/&gt;</strong>

                    &lt;loadbalance&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9001/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9002/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9003/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                            &lt;/address&gt;
                        &lt;/endpoint&gt;
                    &lt;/loadbalance&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;&lt;drop/&gt;
        &lt;/in&gt;

        &lt;out&gt;
            &lt;!-- Send the messages where they have been sent (i.e. implicit To EPR) --&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

    &lt;sequence name="errorHandler"&gt;

        &lt;makefault&gt;
            &lt;code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/&gt;
            &lt;reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/&gt;
        &lt;/makefault&gt;

        &lt;header name="To" action="remove"/&gt;
        &lt;property name="RESPONSE" value="true"/&gt;

        &lt;send/&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;</pre>

<p><strong>Objective: Demonstrate the load balancing with session affinity
using client initiated sessions</strong> </p>

<p><strong>Prerequisites:</strong> </p>

<p>Start EI with sample configuration 54 (i.e. wso2EI-samples -sn 54). </p>

<p>Deploy the LoadbalanceFailoverService and start three instances of the
sample Axis2 server as in sample 52. </p>

<p>Above configuration is same as the load balancing configuration in sample
52, except that the session type is specified as "simpleClientSession". This
is a client initiated session, which means that the client generates the
session identifier and sends it to with each request. In this sample session
type, client adds a SOAP header named ClientID containing the identifier of
the client. EI binds this ID with a server on the first request and sends
all seccessive requests containing that ID to the same server. Now switch to
samples/axis2Client directory and run the client using the following command
to check this in action.</p>
<pre xml:space="preserve">ant loadbalancefailover -Dmode=session</pre>

<p>In the session mode, client continuously sends requests with three diferent
client (session) IDs. One ID is selected among these three IDs for each
request randomly. Then client prints the session ID with the responded server
for each request. Client output for the first 10 requests are shown below.
</p>
<pre xml:space="preserve">[java] Request: 1 Session number: 1 Response from server: MyServer3
[java] Request: 2 Session number: 2 Response from server: MyServer2
[java] Request: 3 Session number: 0 Response from server: MyServer1
[java] Request: 4 Session number: 2 Response from server: MyServer2
[java] Request: 5 Session number: 1 Response from server: MyServer3
[java] Request: 6 Session number: 2 Response from server: MyServer2
[java] Request: 7 Session number: 2 Response from server: MyServer2
[java] Request: 8 Session number: 1 Response from server: MyServer3
[java] Request: 9 Session number: 0 Response from server: MyServer1
[java] Request: 10 Session number: 0 Response from server: MyServer1
... </pre>

<p>You can see that session number 0 is always directed to the server named
MyServer1. That means session number 0 is bound to MyServer1. Similarly
session 1 and 2 are bound to MyServer3 and MyServer2 respectively. </p>

<h2><a name="Sample55">Sample 55: Session affinity load balancing between fail
over endpoints</a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;sequence name="main" onError="errorHandler"&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;!-- specify the session as the simple client session provided by Synapse for
                    testing purpose --&gt;
                    <strong>&lt;session type="simpleClientSession"/&gt;</strong>

                    &lt;loadbalance&gt;
                        &lt;endpoint&gt;
                            &lt;failover&gt;
                                &lt;endpoint&gt;
                                    &lt;address uri="http://localhost:9001/services/LBService1"&gt;
                                        &lt;enableAddressing/&gt;
                                    &lt;/address&gt;
                                &lt;/endpoint&gt;
                                &lt;endpoint&gt;
                                    &lt;address uri="http://localhost:9002/services/LBService1"&gt;
                                        &lt;enableAddressing/&gt;
                                    &lt;/address&gt;
                                &lt;/endpoint&gt;
                            &lt;/failover&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;failover&gt;
                                &lt;endpoint&gt;
                                    &lt;address uri="http://localhost:9003/services/LBService1"&gt;
                                        &lt;enableAddressing/&gt;
                                    &lt;/address&gt;
                                &lt;/endpoint&gt;
                                &lt;endpoint&gt;
                                    &lt;address uri="http://localhost:9004/services/LBService1"&gt;
                                        &lt;enableAddressing/&gt;
                                    &lt;/address&gt;
                                &lt;/endpoint&gt;
                            &lt;/failover&gt;
                        &lt;/endpoint&gt;
                    &lt;/loadbalance&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;&lt;drop/&gt;
        &lt;/in&gt;

        &lt;out&gt;
            &lt;!-- Send the messages where they have been sent (i.e. implicit To EPR) --&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

    &lt;sequence name="errorHandler"&gt;

        &lt;makefault&gt;
            &lt;code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/&gt;
            &lt;reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/&gt;
        &lt;/makefault&gt;

        &lt;header name="To" action="remove"/&gt;
        &lt;property name="RESPONSE" value="true"/&gt;

        &lt;send/&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;</pre>

<p><strong>Objective: Demonstrate the session affinity based load balancing
with failover capability</strong> </p>

<p><strong>Prerequisites:</strong> </p>

<p>Start EI with sample configuration 55 (i.e. wso2EI-samples -sn 55). </p>

<p>Deploy the LoadbalanceFailoverService and start four sample Axis2 servers
on http ports 9001, 9002, 9003 and 9004 respectively (make sure to specify
unconflicting https ports). </p>

<p>This configuration also uses "simpleClientSession" to bind sessions as in
the previous sample. But failover endpoints are specified as the child
endpoints of the load balance endpoint. Therefore sessions are bound to the
failover endpoints. Session information has to be replicated among the
servers listed under each failover endpoint using some clustering mechanism.
Therefore, if one endpoint bound to a session failed, successive requests for
that session will be directed to the next endpoint in that failover group.
Run the client using the following command to observe this behaviour. </p>
<pre xml:space="preserve">ant loadbalancefailover -Dmode=session</pre>

<p>You can see a client output as shown below. </p>
<pre xml:space="preserve">...
[java] Request: 222 Session number: 0 Response from server: MyServer1
[java] Request: 223 Session number: 0 Response from server: MyServer1
[java] Request: 224 Session number: 1 Response from server: MyServer1
[java] Request: 225 Session number: 2 Response from server: MyServer3
[java] Request: 226 Session number: 0 Response from server: MyServer1
[java] Request: 227 Session number: 1 Response from server: MyServer1
[java] Request: 228 Session number: 2 Response from server: MyServer3
[java] Request: 229 Session number: 1 Response from server: MyServer1
[java] Request: 230 Session number: 1 Response from server: MyServer1
[java] Request: 231 Session number: 2 Response from server: MyServer3
...</pre>

<p>Note that session 0 is always directed to MyServer1 and session 1 is
directed to MyServer3. No requests are directed to MyServer2 and MyServer4 as
they are kept as backups by failover endpoints. Now shutdown the server named
MyServer1 while running the sample. You will observe that all successive
requests for session 0 is now directed to MyServer2, which is the backup
server for MyServer1's group. This is shown below, where MyServer1 was
shutdown after the request 534. </p>
<pre xml:space="preserve">...
[java] Request: 529 Session number: 2 Response from server: MyServer3
[java] Request: 530 Session number: 1 Response from server: MyServer1
[java] Request: 531 Session number: 0 Response from server: MyServer1
[java] Request: 532 Session number: 1 Response from server: MyServer1
[java] Request: 533 Session number: 1 Response from server: MyServer1
[java] Request: 534 Session number: 1 Response from server: MyServer1
[java] Request: 535 Session number: 0 Response from server: MyServer2
[java] Request: 536 Session number: 0 Response from server: MyServer2
[java] Request: 537 Session number: 0 Response from server: MyServer2
[java] Request: 538 Session number: 2 Response from server: MyServer3
[java] Request: 539 Session number: 0 Response from server: MyServer2
...</pre>

<h2><a name="Sample56">Sample 56: WSDL endpoint</a> </h2>
<pre xml:space="preserve">&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;

    &lt;sequence name="main"&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;!-- get epr from the given wsdl --&gt;
                &lt;endpoint&gt;
                    &lt;wsdl uri="file:samples/service-bus/resources/proxy/sample_proxy_1.wsdl" service="SimpleStockQuoteService" port="SimpleStockQuoteServiceSOAP11port_http"/&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;
        &lt;/in&gt;

        &lt;out&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;</pre>

<p><strong>Objective: Demonstrate the use of WSDL endpoints</strong> </p>

<p><strong>Prerequisites:</strong> </p>

<p>Start the Synapse configuration numbered 56 (i.e. wso2EI-samples -sn 56).
</p>

<p>Deploy the SimpleStockQuoteService and start the sample Axis2 server. </p>

<p>This sample uses a WSDL endpoint inside the send mediator. WSDL endpoints
can extract endpoint's address from the given WSDL. As WSDL documents can
have many services and many ports inside each service, the service and port
of the required endpoint has to be specified. As with address endpoints, QoS
parameters for the endpoint can be specified inline in the configuration. An
excerpt taken from the sample_proxy_1.wsdl containing the specified service
and port is listed below. </p>
<pre xml:space="preserve">&lt;wsdl:service name="SimpleStockQuoteService"&gt;
    &lt;wsdl:port name="SimpleStockQuoteServiceSOAP11port_http"
               binding="axis2:SimpleStockQuoteServiceSOAP11Binding"&gt;
        &lt;soap:address location="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
    &lt;/wsdl:port&gt;
    &lt;wsdl:port name="SimpleStockQuoteServiceSOAP12port_http"
               binding="axis2:SimpleStockQuoteServiceSOAP12Binding"&gt;
        &lt;soap12:address location="http://localhost:9000/services/SimpleStockQuoteService"/&gt;
    &lt;/wsdl:port&gt;
&lt;/wsdl:service&gt;</pre>

<p>Specified service and port refers to the endpoint address
"http://localhost:9000/services/SimpleStockQuoteService" according to the above
WSDL. Now run the client using the following command. </p>
<pre xml:space="preserve">ant stockquote -Dsymbol=IBM -Dmode=quote -Daddurl=http://localhost:8280</pre>

<p>Client will print the quote price for IBM received from the server running
on port 9000. Observe the Axis2 console and the EI console to verify this
behavior. </p>
    <h2>
<a name="Sample57">Sample 57: Dynamic load balancing between 3 nodes</a></h2>
<pre xml:space="preserve">
&lt;definitions xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;

    &lt;sequence name=&quot;main&quot; onError=&quot;errorHandler&quot;&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;endpoint name=&quot;dynamicLB&quot;&gt;
                    &lt;dynamicLoadbalance failover=&quot;true&quot;
                                           algorithm=&quot;org.apache.synapse.endpoints.algorithms.RoundRobin&quot;&gt;
                        &lt;membershipHandler
                                class=&quot;org.apache.synapse.core.axis2.Axis2LoadBalanceMembershipHandler&quot;&gt;
                            &lt;property name=&quot;applicationDomain&quot; value=&quot;apache.axis2.application.domain&quot;/&gt;
                        &lt;/membershipHandler&gt;
                    &lt;/dynamicLoadbalance&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;
            &lt;drop/&gt;
        &lt;/in&gt;

        &lt;out&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

    &lt;sequence name=&quot;errorHandler&quot;&gt;
        &lt;makefault response=&quot;true&quot;&gt;
            &lt;code value=&quot;tns:Receiver&quot; xmlns:tns=&quot;http://www.w3.org/2003/05/soap-envelope&quot;/&gt;
            &lt;reason value=&quot;COULDN'T SEND THE MESSAGE TO THE SERVER.&quot;/&gt;
        &lt;/makefault&gt;
        &lt;send/&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;
</pre>
<p>
<strong>Objective: Demonstrate the simple dynamic load balancing among a set of nodes</strong> </p>
<p>
<strong>Prerequisites:</strong> </p>
<p>
    Enable clustering and group management in the &lt;EI installation directory&gt;/ repository/conf/axis2.xml
    file. This can be done by setting the &quot;enable&quot; attribute of the &quot;cluster&quot; and
    &quot;groupManagement&quot; elements. Also provide the IP address of you machine as the values of the
    &quot;mcastBindAddress&quot; and &quot;localMemberHost&quot; parameters.
</p>
<p>Start EI with sample configuration 57. (i.e. wso2EI-samples -sn 57) </p>
<p>
    Deploy the LoadbalanceFailoverService by switching to &lt;Synapse installation directory&gt;/samples/axis2Server/src/LoadbalanceFailoverService
    directory and running ant.
</p>
<p>
    Enable clustering in the &lt;Synapse installation directory&gt;/samples/axis2Server/ repository/conf/axis2.xml
    file. This can be done by setting the &quot;enable&quot; attribute of the &quot;cluster&quot;
    element. Also provide the IP address of you machine as the values of the &quot;mcastBindAddress&quot;
    and &quot;localMemberHost&quot; parameters. Make sure that the "applicationDomain" of the membershipHandler is
    the same as the domain name specified in the axis2.xml files of the Axis2 servers. Then Start three
    instances of sample Axis2 server on HTTP ports 9001, 9002 and 9003 and give some unique names to each
    server.
</p>
<p>Example commands to run sample Axis2 servers from the &lt;Synapse installation directory&gt;/samples/axis2Server directory in Linux are listed below: </p>
<pre xml:space="preserve">./axis2server.sh -http 9001 -https 9005 -name MyServer1
./axis2server.sh -http 9002 -https 9006 -name MyServer2
./axis2server.sh -http 9003 -https 9007 -name MyServer3</pre>
<p>Now we are done with setting up the environment for load balance sample. Start the load balance and failover client using the following command: </p>
<pre xml:space="preserve">ant loadbalancefailover -Di=100</pre>
<p>This client sends 100 requests to the LoadbalanceFailoverService through Synapse. Synapse will distribute the load among the three nodes mentioned in the configuration in a round-robin manner. LoadbalanceFailoverService appends the name of the server to the response, so that client can determine which server has processed the message. If you examine the console output of the client, you can see that requests are processed by three servers as follows: </p>
<pre xml:space="preserve">[java] Request: 1 ==&gt; Response from server: MyServer1
[java] Request: 2 ==&gt; Response from server: MyServer2
[java] Request: 3 ==&gt; Response from server: MyServer3
[java] Request: 4 ==&gt; Response from server: MyServer1
[java] Request: 5 ==&gt; Response from server: MyServer2
[java] Request: 6 ==&gt; Response from server: MyServer3
[java] Request: 7 ==&gt; Response from server: MyServer1
...</pre>
<p>Now run the client without the -Di=100 parameter, i.e. ant loadbalancefailover, to send infinite requests. While running the client shutdown the server named MyServer1. You can observe that requests are only distributed among MyServer2 and MyServer3 after shutting down MyServer1. Console output before and after shutting down MyServer1 is listed below (MyServer1 was shutdown after request 63): </p>
<pre xml:space="preserve">...
[java] Request: 61 ==&gt; Response from server: MyServer1
[java] Request: 62 ==&gt; Response from server: MyServer2
[java] Request: 63 ==&gt; Response from server: MyServer3
[java] Request: 64 ==&gt; Response from server: MyServer2
[java] Request: 65 ==&gt; Response from server: MyServer3
[java] Request: 66 ==&gt; Response from server: MyServer2
[java] Request: 67 ==&gt; Response from server: MyServer3
...</pre>
<p>Now restart MyServer1. You can observe that requests will be again sent to all three servers. </p>
<h2>
<a name="Sample58">Sample 58: Static load balancing between 3 nodes</a></h2>
<pre xml:space="preserve">
&lt;definitions xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;

    &lt;sequence name=&quot;main&quot; onError=&quot;errorHandler&quot;&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;loadbalance failover=&quot;true&quot;&gt;
                        &lt;member hostName=&quot;127.0.0.1&quot; httpPort=&quot;9001&quot; httpsPort=&quot;9005&quot;/&gt;
                        &lt;member hostName=&quot;127.0.0.1&quot; httpPort=&quot;9002&quot; httpsPort=&quot;9006&quot;/&gt;
                        &lt;member hostName=&quot;127.0.0.1&quot; httpPort=&quot;9003&quot; httpsPort=&quot;9007&quot;/&gt;
                    &lt;/loadbalance&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;
            &lt;drop/&gt;
        &lt;/in&gt;

        &lt;out&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;

    &lt;sequence name=&quot;errorHandler&quot;&gt;
        &lt;makefault response=&quot;true&quot;&gt;
            &lt;code value=&quot;tns:Receiver&quot; xmlns:tns=&quot;http://www.w3.org/2003/05/soap-envelope&quot;/&gt;
            &lt;reason value=&quot;COULDN'T SEND THE MESSAGE TO THE SERVER.&quot;/&gt;
        &lt;/makefault&gt;
        &lt;send/&gt;
    &lt;/sequence&gt;

&lt;/definitions&gt;
</pre>
<p>
<strong>Objective: Demonstrate the simple static load balancing among a set of nodes</strong> </p>
<p>
<strong>Prerequisites:</strong> </p>
<p>Start Synapse with sample configuration 58. (i.e. wso2EI-samples -sn 58) </p>
<p>Deploy the LoadbalanceFailoverService by switching to &lt;Synapse installation directory&gt;/samples/axis2Server/src/LoadbalanceFailoverService directory and running ant. </p>
<p>Start three instances of sample Axis2 server on HTTP ports 9001, 9002 and 9003 and give some unique names to each server. </p>
<p>Example commands to run sample Axis2 servers from the &lt;Synapse installation directory&gt;/samples/axis2Server directory in Linux are listed below: </p>
<pre xml:space="preserve">./axis2server.sh -http 9001 -https 9005 -name MyServer1
./axis2server.sh -http 9002 -https 9006 -name MyServer2
./axis2server.sh -http 9003 -https 9007 -name MyServer3</pre>
<p>Now we are done with setting up the environment for load balance sample. Start the load balance and failover client using the following command: </p>
<pre xml:space="preserve">ant loadbalancefailover -Di=100</pre>
<p>This client sends 100 requests to the LoadbalanceFailoverService through Synapse. Synapse will distribute the load among the three nodes mentioned in the configuration in a round-robin manner. LoadbalanceFailoverService appends the name of the server to the response, so that client can determine which server has processed the message. If you examine the console output of the client, you can see that requests are processed by three servers as follows: </p>
<pre xml:space="preserve">[java] Request: 1 ==&gt; Response from server: MyServer1
[java] Request: 2 ==&gt; Response from server: MyServer2
[java] Request: 3 ==&gt; Response from server: MyServer3
[java] Request: 4 ==&gt; Response from server: MyServer1
[java] Request: 5 ==&gt; Response from server: MyServer2
[java] Request: 6 ==&gt; Response from server: MyServer3
[java] Request: 7 ==&gt; Response from server: MyServer1
...</pre>
<p>Now run the client without the -Di=100 parameter, i.e. ant loadbalancefailover, to send infinite requests. While running the client shutdown the server named MyServer1. You can observe that requests are only distributed among MyServer2 and MyServer3 after shutting down MyServer1. Console output before and after shutting down MyServer1 is listed below (MyServer1 was shutdown after request 63): </p>
<pre xml:space="preserve">...
[java] Request: 61 ==&gt; Response from server: MyServer1
[java] Request: 62 ==&gt; Response from server: MyServer2
[java] Request: 63 ==&gt; Response from server: MyServer3
[java] Request: 64 ==&gt; Response from server: MyServer2
[java] Request: 65 ==&gt; Response from server: MyServer3
[java] Request: 66 ==&gt; Response from server: MyServer2
[java] Request: 67 ==&gt; Response from server: MyServer3
...</pre>
<p>Now restart MyServer1. You can observe that requests will be again sent to all three servers. </p>
<h2><a name="Sample59">Sample 59: Weighted load balancing between 3
endpoints</a> </h2>
<pre xml:space="preserve">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;definitions xmlns="http://ws.apache.org/ns/synapse"&gt;
    &lt;sequence name="main" onError="errorHandler"&gt;
        &lt;in&gt;
            &lt;send&gt;
                &lt;endpoint&gt;
                    &lt;loadbalance
                            algorithm="org.apache.synapse.endpoints.algorithms.WeightedRoundRobin"&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9001/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendOnFailure&gt;
                                    &lt;initialDuration&gt;20000&lt;/initialDuration&gt;
                                    &lt;progressionFactor&gt;1.0&lt;/progressionFactor&gt;
                                &lt;/suspendOnFailure&gt;
                            &lt;/address&gt;
                            &lt;property name="loadbalance.weight" value="1"/&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9002/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendOnFailure&gt;
                                    &lt;initialDuration&gt;20000&lt;/initialDuration&gt;
                                    &lt;progressionFactor&gt;1.0&lt;/progressionFactor&gt;
                                &lt;/suspendOnFailure&gt;
                            &lt;/address&gt;
                            &lt;property name="loadbalance.weight" value="2"/&gt;
                        &lt;/endpoint&gt;
                        &lt;endpoint&gt;
                            &lt;address uri="http://localhost:9003/services/LBService1"&gt;
                                &lt;enableAddressing/&gt;
                                &lt;suspendOnFailure&gt;
                                    &lt;initialDuration&gt;20000&lt;/initialDuration&gt;
                                    &lt;progressionFactor&gt;1.0&lt;/progressionFactor&gt;
                                &lt;/suspendOnFailure&gt;
                            &lt;/address&gt;
                            &lt;property name="loadbalance.weight" value="3"/&gt;
                        &lt;/endpoint&gt;
                    &lt;/loadbalance&gt;
                &lt;/endpoint&gt;
            &lt;/send&gt;
            &lt;drop/&gt;
        &lt;/in&gt;
        &lt;out&gt;
            &lt;send/&gt;
        &lt;/out&gt;
    &lt;/sequence&gt;
    &lt;sequence name="errorHandler"&gt;
        &lt;makefault response="true"&gt;
            &lt;code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/&gt;
            &lt;reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/&gt;
        &lt;/makefault&gt;
        &lt;send/&gt;
    &lt;/sequence&gt;
&lt;/definitions&gt;</pre>

<p><strong>Objective: Demonstrate the weighted load balancing among a set of
endpoints</strong> </p>

<p><strong>Prerequisites:</strong> </p>

<p>Start EI with sample configuration 59. (i.e. wso2EI-samples -sn 59) </p>

<p>Deploy the LoadbalanceFailoverService and start three instances of sample Axis2 server as mentioned in sample 52.</p>

<p>Above configuration sends messages with the weighted loadbalance behaviour. Weight of each leaf
address endpoint is defined by integer value of "loadbalance.weight" property associated with each endpoint.
If weight of a endpoint is x, x number of requests will send to that endpoint before switch to next active endpoint.
<br/>To test this, run the loadbalancefailover client to send 100 requests as follows: </p>
<pre xml:space="preserve">ant loadbalancefailover -Di=100</pre>

<p>This client sends 100 requests to the LoadbalanceFailoverService through
EI. EI will distribute the load among the three endpoints mentioned in the
configuration in weighted round-robin manner. LoadbalanceFailoverService appends the
name of the server to the response, so that client can determine which server
has processed the message. If you examine the console output of the client,
you can see that requests are processed by three servers as follows: </p>
<pre xml:space="preserve">[java] Request: 1 ==&gt; Response from server: MyServer1
[java] Request: 2 ==&gt; Response from server: MyServer2
[java] Request: 3 ==&gt; Response from server: MyServer2
[java] Request: 4 ==&gt; Response from server: MyServer3
[java] Request: 5 ==&gt; Response from server: MyServer3
[java] Request: 6 ==&gt; Response from server: MyServer3
[java] Request: 7 ==&gt; Response from server: MyServer1
[java] Request: 8 ==&gt; Response from server: MyServer2
[java] Request: 9 ==&gt; Response from server: MyServer2
[java] Request: 10 ==&gt; Response from server: MyServer3
[java] Request: 11 ==&gt; Response from server: MyServer3
[java] Request: 12 ==&gt; Response from server: MyServer3
...</pre>

<p>As logs, endpoint with weight 1 received a 1 request and endpoint with weight 2 received 2
requests and etc... in a cycle</p>
</body>
</html>
