This documentation is for WSO2 Complex Event Processor 2.0.0. View documentation for the latest release.

Stock Quote Analyzer

This sample demonstrates how Siddhi engine can be used with Local event broker to receive, process and publish XML messages.

In this sample CEP will receive stock quote information and fire outputs if the last traded amount vary by 2 percent with regards to the average traded price within past 2 minutes.

from allStockQuotesStream#window.time(120000)
insert into fastMovingStockQuotesStream
symbol,avg(price) as avgPrice, price
group by symbol
having ((price > (avgPrice*1.02)) or ((avgPrice*0.98)>price ));

Here we will publish events using an web-service client and fired outputs of the bucket will be send to a web-service called FastMovingStockQuoteReceiverService which will receive the events and log then in console.

Following is the configuration used in this sample.

<cep:bucket xmlns:cep="http://wso2.org/carbon/cep" name="XMLStockQuoteAnalyzer">
    <cep:description>
This bucket analyzes stock quotes and trigger an event if the last
traded amount vary by 2 percent with regards to the average traded
price within past 2 minutes.
    </cep:description>
    <cep:engineProviderConfiguration engineProvider="SiddhiCEPRuntime">
        <cep:property name="siddhi.persistence.snapshot.time.interval.minutes">0</cep:property>
        <cep:property name="siddhi.enable.distributed.processing">false</cep:property>
    </cep:engineProviderConfiguration>
    <cep:input topic="AllStockQuotes" brokerName="localBroker">
        <cep:xmlMapping stream="allStockQuotesStream" <!--queryEventType="Tuple"--> >
            <cep:xpathDefinition prefix="quotedata" namespace="http://ws.cdyne.com/"/>
            <cep:property name="price" xpath="//quotedata:StockQuoteEvent/quotedata:LastTradeAmount"
                          type="java.lang.Double"/>
            <cep:property name="symbol" xpath="//quotedata:StockQuoteEvent/quotedata:StockSymbol"
                          type="java.lang.String"/>
        </cep:xmlMapping>
    </cep:input>
    <cep:query name="StockDetector">
        <cep:expression>
            <![CDATA[
from allStockQuotesStream#window.time(120000)
insert into fastMovingStockQuotesStream
symbol,avg(price) as avgPrice, price
group by symbol
having ((price > (avgPrice*1.02)) or ((avgPrice*0.98)>price ));
            ]]></cep:expression>
        <cep:output topic="FastMovingStockQuotes" brokerName="localBroker">
            <cep:xmlMapping>
                <quotedata:StockQuoteDataEvent xmlns:quotedata="http://ws.cdyne.com/"
                                               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <quotedata:StockSymbol>{symbol}</quotedata:StockSymbol>
                    <quotedata:AvgLastTradeAmount>{avgPrice}</quotedata:AvgLastTradeAmount>
                    <quotedata:LastTradeAmount>{price}</quotedata:LastTradeAmount>
                </quotedata:StockQuoteDataEvent>
            </cep:xmlMapping>
        </cep:output>
    </cep:query>
</cep:bucket>

Prerequisites

  • Apache Ant to build & deploy the Sample & Service, and to run the client. Refer Installation Prerequisites for instructions to install Apache Ant.

Deploying the configurations

The steps are as follows :

  1. Install the WSO2 Complex Event Processor, but do not start the server,  Refer to the Installation Guide for instructions.
  2. In a command prompt, switch to the sample directory: <CEP_HOME>/samples/cep-samples 
    For example, in Linux: cd <CEP_HOME>/samples/cep-samples
  3. From there, type ant deploy-xml,
    This will copy the broker-manager-config.xml to <CEP_HOME>/repository/conf directory and the bucket configuration to <CEP_HOME>/repository/deployment/server/cepbuckets directory.

Deploying receiver service

The steps are as follows :  

  1. In a command prompt, switch to the FastMovingStockQuoteReceiverService services directory: <CEP_HOME>/samples/services/FastMovingStockQuoteReceiverService
    For example, in Linux: cd <CEP_HOME>/samples/services/FastMovingStockQuoteReceiverService
  2. From there, type ant, this will deploy the FastMovingStockQuoteReceiverService in CEP itself.
    You can follow the server logs to check whether FastMovingStockQuoteReceiverService.arr has been properly deployed.



    You will also be able to see the axis2 service in the services list.

Configuring receiver service

We need to Configure the FastMovingStockQuoteReceiverService in order to receive the output events emitted by the bucket under the FastMovingStockQuotes topic. Here we will be creating FastMovingStockQuotes topic in the local broker and subscribe  FastMovingStockQuoteReceiverService on that topic.

The steps are as follows :

  1. Sign In. Enter your user name and password to log on to the Complex Event Processor Management Console.



  2. Click on "Add" menu item under "Topics" Menu in Manage section of the left panel



  3. Specify the topic name in the topic input text box, in this case topic name is : "FastMovingStockQuotes" (the output topic) and click on 'Add Topic' button. This will add the topic to the server and you will be directed to the Topic Browser page.



  4. Once you click on the topic in topic browser page you will be able see four links as in the bellow image. Click in the subscribe link and you will be directed to Subscribe page. 



  5. Create subscription with following details. Once you are done click the Subscribe button.

    topic		 : FastMovingStockQuotes (Output topic)
    subscription mode: Topic only subscription
    URL		 : http://localhost:9763/services/FastMovingStockQuoteService/getOMElement
    expiration Time	 : select a future date from calender

    Once you click on the "Subscribe" button, you will be directed to the Topic Browser page. 

  6. You can verify whether you have correctly subscribe to the topic by clicking on "Details" link of that topic in topic browser page. 

    Once you click on that, you will be directed to the "topic details" page and there you will find all the subscriptions for that topic and its children (in this case it does not exists) and permission on that topic. Apart from that with the publish section, you can publish a test XML message to that topic and check whether your subscription URL has been properly subscribed.


Running the Client

The steps are as follows :

  1. In a command prompt, switch to the CEP samples directory:<CEP_HOME>/samples/cep-samples 
    For example, in Linux: cd <CEP_HOME>/samples/cep-samples
  2. From there, type ant xmlStockQuotePublisher
    This will send some XML messages that will trigger output.

Observation

You will be able observe filtered events in the server console as bellow.