Competing Consumers¶
This page explains how you can implement a sample scenario of Competing Consumers EIP using WSO2 Micro Integrator.
Introduction to Competing Consumers¶
The Competing Consumers EIP relates to multiple consumers that compete with each other to receive a request from a given Point-to-Point Channel. In this pattern, requests are handled and delegated similar to how messages are handled in the Point-to-Point channel using the round-robin algorithm.
Info
For more information, see the Competing Consumers.
Sample scenario¶
In this sample scenario, each Axis2 server instance acts as a consumer waiting for a specific message. When the client sends a message to WSO2 MI, it diverts the request based on the round-robin algorithm among the consumers. This way, the consumers receive a request message adhering to the conditions of the algorithm.
An alternative implementation to the Competing Consumers EIP is to use a Content-Based Router and route messages to different receivers based on the content of a message.
The diagram below depicts how to simulate the sample scenario using the WSO2 MI.
Before digging into implementation details, let's take a look at the relationship between the sample scenario and the Competing Consumers EIP by comparing their core components.
Competing Consumers EIP | Competing Consumers Sample Scenario |
---|---|
Sender | Simple Stock Quote Client |
Messages | Simple Stock Quote Requests with Load-Balance Endpoint |
Consumer/Receiver | Simple Stock Quote Server Instances |
Synapse configuration of the artifacts¶
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="StockQuoteProxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<call>
<endpoint key="LoadBalanceEP"/>
</call>
<respond/>
</inSequence>
<faultSequence>
<log level="full">
<property name="MESSAGE" value="Executing default "fault" sequence"/>
<property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
<property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
</log>
<drop/>
</faultSequence>
</target>
</proxy>
<?xml version="1.0" encoding="UTF-8"?>
<endpoint name="LoadBalanceEP" xmlns="http://ws.apache.org/ns/synapse">
<loadbalance algorithm="org.apache.synapse.endpoints.algorithms.RoundRobin">
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService"></address>
</endpoint>
<endpoint>
<address uri="http://localhost:9001/services/SimpleStockQuoteService"></address>
</endpoint>
<endpoint>
<address uri="http://localhost:9002/services/SimpleStockQuoteService"></address>
</endpoint>
</loadbalance>
</endpoint>
How the implementation works¶
Let's explore the elements of the configuration:
-
Proxy Service: The
StockQuoteProxy
proxy service receives requests and forwards them to the appropriate back-end services using a load-balancing mechanism. -
InSequence: Directs the incoming message to the
LoadBalanceEP
endpoint using thecall
mediator, and then immediately responds to the client with therespond
mediator. -
FaultSequence: Handles errors by logging the full details of any faults and dropping the message.
-
Load Balance Endpoint:
- Endpoint: Defines a group of back-end service endpoints that handle the requests.
- LoadBalance: Distributes incoming requests across multiple endpoints using the Round-Robin algorithm, ensuring an even distribution of traffic among the services at
http://localhost:9000
,http://localhost:9001
, andhttp://localhost:9002
.
Set up the sample scenario¶
Follow the below instructions to simulate this sample scenario.
-
Install WSO2 Micro Integrator.
Info
Follow the Install the Micro Integrator Runtime documentation for more information.
-
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.
-
Download the backend service.
-
Extract the downloaded zip file.
-
Open a terminal, and navigate to the
axis2Server/bin/
directory inside the extracted folder. -
Execute the following command to start the axis2server with the SimpleStockQuote backend service:
sh axis2server.sh
axis2server.bat
-
Navigate to the
MI_HOME/bin/
directory and start thetcpmon
application. -
In
tcpmon
application, navigate to Admin tab. Add listeners to ports9001
and9002
. For each listener set the target hostname tolocalhost
and target port to9000
in each instance. -
Download the artifacts of the sample.
-
Import the artifacts to WSO2 MI.
Click File -> Open Folder -> Select the extracted ZIP file to import the downloaded ZIP file.
-
Start the project in the WSO2 MI server.
For instructions, refer to Build and Run.
-
Start SoapUI.
For instructions on downloading and starting, refer to SoapUI Getting Started.
Execute the sample¶
- Repeatedly send several requests using SoapUI (or any other Soap client).
POST http://localhost:8290/services/StockQuoteProxy Accept-Encoding: gzip,deflate Content-Type: text/xml;charset=UTF-8 SOAPAction: "urn:getQuote" Connection: Keep-Alive <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd"> <soapenv:Body> <ser:getQuote> <ser:request> <xsd:symbol>IBM</xsd:symbol> </ser:request> </ser:getQuote> </soapenv:Body> </soapenv:Envelope>
Analyze the output¶
Note
In tcpmon
tabs and Axis Server console that the requests are distributed among several servers. Each server is acting as a competing consumer.