Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This section explains, through an example scenario, how the Selective Consumer EIP can be implemented using WSO2 ESB. The following topics are covered:

Table of Contents

...

The Selective Consumer EIP allows a message consumer to select which messages it wishes to will receive. It filteres filters the messages delivered by its channel so that it only receives the ones that match its criteria. For more information, refer to http://www.eaipatterns.com/MessageSelector.html.

...

Figure 1: Selective Consumer EIP

Example

...

scenario

This example scenario demonstrates how a specific receiver only processes messages that are pre-filtered based on certain criteria. We have an Axis2 server as the consumer. The consumer criteria is specified through an XML schema validation, which is stored as a local entry in the registry. We use the Validate mediator to check whether the messages that are sent to the ESB match the criteria of the schema. The Axis2 server can consume the message only if the message meets the validation criteria.   

Implementing the Example Scenario in WSO2 ESB

Getting Started

The diagram below depicts how to simulate the example scenario using WSO2 ESB.

...

Before digging into implementation details, let's take a look at the relationship between the example scenario and the Selective Consumer EIP by comparing their core components.

 

...

Selective Consumer EIP

...

(Figure 1)Selective Consumer Example Scenario (Figure 2)
Specifying ProducerSimple Stock Quote Client 
Messages with Selection ValuesSimple Stock Quote Request
Selective ConsumerSchema Validator (Validate Mediator)
ReceiverSimple Stock Quote Server

...

Environment

...

setup

...

  1. Download

...

  1. and install

...

  1. WSO2 ESB from http://wso2.com/products/enterprise-service-bus. For a list of prerequisites and step-by-step installation instructions, refer to

...

  1. Installation Guide in the WSO2 ESB documentation.

...

  1. Start the sample Axis2 server. For instructions, refer to the section Setting Up the ESB Samples

...

  1. - Starting

...

  1. the Axis2 server in the WSO2 ESB documentation.

ESB

...

configuration

3. Start the ESB server and log into its management console UI (https://localhost:9443/carbon). In the management console, navigate to the Main Menu, click Service Bus and then Source View menu and click Source View in the Service Bus section. Next, copy and paste the following configuration, which helps you explore the example scenario, to the source view.

Anchor
step3
step3

Code Block
languagehtml/xml
linenumberstrue
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
   <localEntry key="selective_criteria">
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                 xmlns="http://www.apache-synapse.org/test"
                 elementFormDefault="qualified"
                 attributeFormDefault="unqualified"
                 targetNamespace="http://services.samples">
         <xs:element name="getQuote">
            <xs:complexType>
               <xs:sequence>
                  <xs:element name="request">
                     <xs:complexType>
                        <xs:sequence>
                           <xs:element name="stockvalue" type="xs:string"/>
                        </xs:sequence>
                     </xs:complexType>
                  </xs:element>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
      </xs:schema>
   </localEntry>
   <sequence name="fault">
      <log level="full">
         <property name="MESSAGE" value="Executing default &#34;fault&#34; sequence"/>
         <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
         <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
      </log>
      <drop/>
   </sequence>
   <sequence name="main">
      <in>
         <validate>
            <schema key="selective_criteria"/>
            <on-fail>
               <makefault>
                  <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
                  <reason value="Invalid custom quote request"/>
               </makefault>
               <property name="RESPONSE" value="true"/>
               <header name="To" expression="get-property('ReplyTo')"/>
               <drop/>
            </on-fail>
         </validate>
         <send>
            <endpoint>
               <address uri="http://localhost:9000/services/SimpleStockQuoteService?wsdl"/>
            </endpoint>
         </send>
      </in>
      <out>
         <send/>
      </out>
   </sequence>
</definitions>

Simulating the

...

sample scenario

4. Send the following request using a SOAP client like SOAP UI SoapUI.

Code Block
languagehtml/xml
<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>
         <!--Optional:-->
         <ser:request>
            <!--Optional:-->
            <ser:symbol>IBM</ser:symbol>
         </ser:request>
      </ser:getQuote>
   </soapenv:Body>
</soapenv:Envelope>

...

The consumer has specified the criteria of using a schema validation. Only the messages that meet this criteria will be consumed.  

How the

...

implementation works

Let's investigate the elements of the ESB configuration in detail. The line numbers below are mapped with refer to the ESB configuration illustrated in step 3 above shown above.

  • localEntry [line 3 in 2 in ESB config] - a A local registry entry with key 'selective_criteria' is used to define the XML Schema that will be schema used for validation inside the main sequence. 
  • validate [line 343] in ESB config] - this This mediator is used to define the portion of a message that is going to be used for validation - in the case . In this example, no source attribute is specified using an XPath expression, which makes so the ESB perform performs the validation on the first child element of the SOAP body. 
  • scehma [line 35 34 in ESB config] - this defines Defines which schema to use for validation - in this case . In this example, the local registry entry definition made in line 3 2 is used. 
  • on-fail [line 36 5 in ESB config] - this defines what is to be carried out Defines the action to take on failure of a validation - in this case . In this example, a fault is created and the message is dropped. 

...

hiddentrue

...