This section explains, through an example scenario, how the Datatype Channel EIP can be implemented using WSO2 ESB. The following topics are covered:
Introduction to Datatype Channel
This EIP creates a separate channel for each type of data so that all the messages on a given channel will contain the same data type. The sender, who knows the data type, should select the appropriate channel on which to send the message. The receiver knows which type of data a message contains based on the channel in which it is received. For more information, refer to http://www.eaipatterns.com/DatatypeChannel.html.
Figure 1: Datatype Channel EIP
Example scenario
The example scenario depicts a Stock Quote
service deployed in Axis2 server. It offers several service operations to the user. WSO2 ESB uses the filter mediator to identify each action that is specified by the sender and diverts the request into the appropriate sequence. Each sequence acts as a separate channel. The sender experiences the decomposition of channels through a log message indicated by the ESB. There will be a different log message for each operation the sender requests.
The diagram below depicts how to simulate the example scenario using the WSO2 ESB.
Figure 2: Example Scenario of the Datatype Channel EIP
Before digging into implementation details, let's take a look at the relationship between the example scenario and the Datatype Channel EIP by comparing their core components.
Datatype Channel EIP (Figure 1) | Datatype Channel Example Scenario (Figure 2) |
---|---|
Sender | Client |
Datatype Channel | Filter and Sequence mediators of the ESB. The Filter Mediator specifies which datatype channel to use, and the ESB defines each datatype channel as a Sequence Mediator. |
Receiver | Stock Quote Server Instance |
Environment setup
- Download and install WSO2 ESB from http://wso2.com/products/enterprise-service-bus. For a list of prerequisites and step-by-step installation instructions, refer to Getting Started in the WSO2 ESB documentation.
- Deploy the
SimpleStockQuoteService
by starting the Sample Axis2 server on port 9000. For instructions, refer to the section ESB Samples Setup - Starting Sample Back-End Services in the WSO2 ESB documentation.
ESB configuration
Start the ESB server and log into its management console UI (https:
//localhost:9443/carbon
). In the management console, navigate to Main Menu, click Service Bus and then Source View. Next, copy and paste the following configuration, which helps you explore the example scenario, to the source view.
<definitions xmlns="http://ws.apache.org/ns/synapse"> <endpoint name="StockQuoteReceiver"> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> <sequence name="MarketActivity"> <in> <log level="custom"> <property name="Messagin_Channel" value="MARKET_ACTIVITY"/> </log> <send> <endpoint key="StockQuoteReceiver"/> </send> </in> <out> <send/> </out> </sequence> <sequence name="FullQuote"> <in> <log level="custom"> <property name="Messagin_Channel" value="FULL_QUOTE"/> </log> <send> <endpoint key="StockQuoteReceiver"/> </send> </in> <out> <send/> </out> </sequence> <sequence name="fault"> <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/> </sequence> <sequence name="StockQuote"> <in> <log level="custom"> <property name="Messagin_Channel" value="STOCK_QUOTE"/> </log> <send> <endpoint key="StockQuoteReceiver"/> </send> </in> <out> <send/> </out> </sequence> <sequence name="main"> <log/> <in> <filter source="get-property('Action')" regex="/*urn:getQuote/*"> <then> <sequence key="StockQuote"/> </then> <else/> </filter> <filter source="get-property('Action')" regex="/*urn:getFullQuote/*"> <then> <sequence key="FullQuote"/> </then> <else/> </filter> <filter source="get-property('Action')" regex="/*urn:getMarketActivity/*"> <then> <sequence key="MarketActivity"/> </then> <else/> </filter> </in> <out> <send/> </out> </sequence> </definitions>
Simulating the sample scenario
- Send a request using the
Stock Quote
client to WSO2 ESB in the following manner. For information about the Stock Quote client, refer to the section Sample Clients in the WSO2 ESB documentation.ant stockquote -Dtrpurl=http://localhost:8280/ -Dmode=quote
- Also execute the command above with
-Dmode=[quote | marketactivity | fullquote]
, and observe the ESB's log of the corresponding valuesSTOCK_QUOTE
,MARKET_ACTIVITY
andFULL_QUOTE
.
How the implementation works
Let's investigate the elements of the ESB configuration in detail. The line numbers below are mapped with the ESB configuration shown above.
- Endpoint [line 2 of the ESB config] - Defines an endpoint referenced by a name that contains an
<address>
element with the endpoint address of a particular service. - Sequence [line 5 of the ESB config] - The Sequence Mediator defines a sequence block, callable by its key (defined in the name attribute). Each sequence block has its own
<in>
and<out>
blocks. - Sequence main [line 52 of the ESB config] - The main sequence, which is always run first.
Filter [line 55 of the ESB config] - A Filter mediator that uses the
get-property
XPath expression to find theAction
field from the SOAP header. The regular expression that is defined in the regex attribute tries to match the value in the Action field. If successfully matched, it calls the relevant sequence by its key. In line 55, ifget-property('Action')
returnsurn:getQuote
, theStockQuote
sequence defined in line 39 is called.