Skip to content

Load Balancing with Message Forwarding Processor

This example demonstrates how the message forwarding processor handles load balancing.

Synapse configuration

Following are the artifact configurations that we can use to implement this scenario. See the instructions on how to build and run this example.

<proxy xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteProxy"
              transports="https http"
              startOnLoad="true">
    <description/>
    <target>
       <inSequence>
          <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
          <property name="OUT_ONLY" value="true"/>
          <store messageStore="JMSMS"/>
       </inSequence>
       <outSequence/>
       <faultSequence/>
    </target>
 </proxy>
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="SimpleStockQuoteService1">
  <address uri="http://localhost:9001/services/SimpleStockQuoteService"/>
</endpoint>
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="SimpleStockQuoteService2">
  <address uri="http://localhost:9002/services/SimpleStockQuoteService"/>
</endpoint>
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="SimpleStockQuoteService3">
  <address uri="http://localhost:9003/services/SimpleStockQuoteService"/>
</endpoint>
<messageStore xmlns="http://ws.apache.org/ns/synapse" class="org.apache.synapse.message.store.impl.jms.JmsStore" name="JMSMS">
  <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
  <parameter name="store.jms.cache.connection">false</parameter>
  <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>
  <parameter name="store.jms.JMSSpecVersion">1.1</parameter>
  <parameter name="store.jms.destination">JMSMS</parameter>
</messageStore>
<messageProcessor xmlns="http://ws.apache.org/ns/synapse" class="org.apache.synapse.message.processor.impl.forwarder.ScheduledMessageForwardingProcessor"
                         name="Forwarder1"
                         targetEndpoint="SimpleStockQuoteService1"
                         messageStore="JMSMS">
  <parameter name="client.retry.interval">1000</parameter>
  <parameter name="interval">1000</parameter>
  <parameter name="is.active">true</parameter>
</messageProcessor>
<messageProcessor xmlns="http://ws.apache.org/ns/synapse" class="org.apache.synapse.message.processor.impl.forwarder.ScheduledMessageForwardingProcessor"
                         name="Forwarder2"
                         targetEndpoint="SimpleStockQuoteService2"
                         messageStore="JMSMS">
  <parameter name="client.retry.interval">1000</parameter>
  <parameter name="interval">1000</parameter>
  <parameter name="is.active">true</parameter>
</messageProcessor>
<messageProcessor xmlns="http://ws.apache.org/ns/synapse" class="org.apache.synapse.message.processor.impl.forwarder.ScheduledMessageForwardingProcessor"
                         name="Forwarder3"
                         targetEndpoint="SimpleStockQuoteService3"
                         messageStore="JMSMS">
  <parameter name="client.retry.interval">1000</parameter>
  <parameter name="interval">1000</parameter>
  <parameter name="is.active">true</parameter>
</messageProcessor>

Build and run

Configure the ActiveMQ broker.

Create the artifacts:

    1. Launch Visual Studio Code with the Micro Integrator for VS Code extension (MI for VS Code) installed.

    Info

    Follow the Install Micro Integrator for VS Code documentation for a complete installation guide.

  1. Create an integration project.

  2. Create the proxy service, endpoints, message stores and message processors with the configurations given above.
  3. Deploy the artifacts in your Micro Integrator.

Set up the back-end service:

  1. Download the back-end service.
  2. Extract the downloaded zip file and create two additional copies of the extracted folder.
  3. Open the axis2.xml file located in the axis2Server/conf/ directory within each extracted folder.
  4. Update the port numbers in the axis2.xml file for each folder to 9001, 9002, and 9003 respectively.
  5. Open a terminal, navigate to the axis2Server/bin/ directory inside the extracted folders.
  6. Execute the following command to start the axis2server with the SimpleStockQuote back-end service:

=== "On MacOS/Linux/CentOS"

sh axis2server.sh
=== "On Windows"
axis2server.bat

Send the following request to invoke the service:

POST http://localhost:8290/services/StockQuoteProxy HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "urn:getQuote"
Content-Length: 492
Host: localhost:8290
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getQuote xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
         <ser:request>
            <xsd:symbol>IBM</xsd:symbol>
         </ser:request>
      </ser:getQuote>
   </soapenv:Body>
</soapenv:Envelope>

If you send the request to the proxy service several times and observe the log on the back-end server, you will see that the following messages are distributed among the back-end nodes.

Mon Aug 12 13:19:27 IST 2024 samples.services.SimpleStockQuoteService :: Generating quote for : IBM