The content in this documentation is for older versions of WSO2 products. For updated information on Enterprise Integration Patterns, go to the latest Micro Integrator documentation.

Publish-Subscribe Channel

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

Introduction to Publish-Subscribe Channel

The Publish-Subscribe Channel EIP receives messages from the input channel, and then splits and transmits them among its subscribers through the output channel. Each subscriber has only one output channel. For more information, go to Publish-Subscribe Channel










Figure 1: Publisher-Subscribe Channel EIP

Example scenario

The following diagram depicts an example scenario, where the Publish-Subscribe Channel EIP is implemented using the ESB. In this example, a stock inventory is exposed through two instances of the Stock Quote Service. When a client sends a message, the ESB receives the message and routes it both instances of the back-end service (Stock Quote service). Both back-end services are subscribed to the ESB through the event mediator.

Figure 2: Example scenario of the Publish-Subscribe Channel EIP

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

Publish-Subscribe Chanel EIP (Figure 1)Publish-Subscribe Channel Example Scenario (Figure 2)

Publisher

Stock Quote Request

Publisher SubscriberChannel

Event Mediator

Subscriber

Stock Quote service instance

The ESB configuration

Given below is the ESB configuration for simulating the example scenario explained above. 

<definitions xmlns="http://ws.apache.org/ns/synapse">
   <proxy name="PublishSubscribeChannel" startOnLoad="true" transports="http,https">
       <target>
           <inSequence>
               <event topic="PublisherSubsciber"/>
           </inSequence>
       </target>
   </proxy>
</definitions>

The configuration elements

The elements used in the above ESB configuration are explained below.

  • <main sequence> - The default sequence that gets triggered when the user invokes the ESB.
  • <event> - Allows you to define a set of receivers.

Simulating the example scenario

Now, let's try out the example scenario explained above.

Setting up the environment

You need to set up the ESB, and the back-end service:

  1. Download the Publish-Subscribe_1.0.0.zip file, which includes the ESB configuration described above. 
  2. See Setting up the Environment for instructions on setting up the ESB and the back-end service.

    When you set up the environment, note that you need to start two instances of the back-end service (Stock Quote Service) to simulate this example.

    Your ESB, as well as the two back-end services, should not be started.

  3. Now, you can log in to the ESB's management console and create an event as explained below.

    1. Log in to the management console.

    2. Click Topics on the Main menu, and then click Add.

    3. Enter PublisherSubsciber as the topic name, and click Add Topic to create the topic. You will be directed to the Topic Browser tree view where the new topic is displayed. 

    4. Click the new topic (in the Topic Browser), and click Subscribe to create a static subscription. 

    5. Enter  http://localhost:9000/services/SimpleStockQuoteService as the Event Sink URL, and click Subscribe. The first back-end service (Stock Quote service) that was started on port 9000 of the Axis2 server is now subscribed to the ESB topic.

    6. Click the new topic (in the Topic Browser) again, and click Subscribe to create another static subscription.

    7. Enter http://localhost:9001/services/SimpleStockQuoteService as the Event Sink URL (note that the port is changed to 9001), and click Subscribe. The second back-end service (Stock Quote service) that was started on port 9001 of the Axis2 server is now subscribed to the ESB topic.
    8. If you click the topic (in the Topic Browser) again and click Details, you can see the two subscriptions listed as shown below.

Executing the sample

Let's send a request to the ESB using the Stock Quote Client application. Find out more about the Stock Quote Client from the ESB documentation.

  1. Open a new terminal, and navigate to the <ESB_HOME>/samples/axis2Client/ directory. The Stock Quote client application is stored in this directory.
  2. Execute the following command to send the request to the ESB.

    ant stockquote -Dtrpurl=http://localhost:8280/services/publish-subscribe-proxy -Dsymbol=foo

    The structure of the request is as follows:

    <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>    
             <ser:request>          
                <xsd:symbol>foo</xsd:symbol>
             </ser:request>         
          </ser:getQuote>
       </soapenv:Body>
    </soapenv:Envelope>

Analyzing the output

When you execute the command above, the ESB first receives the message and then routes it to the two back-end services (StockQuoteService), simultaneously. The following output will be printed on the consoles of both Axis2 servers.

samples.services.SimpleStockQuoteService :: Generating quote for : foo

Note that the Stock Quote client application does not receive a response.