Stock Price Analyzer
Stock Quote sample is based on Stock Quotes of any Stock Exchange. In this sample WSO2 CEP will receive some stock quote information and it will give an output if price of the stock is greater than a predefined value.
Create Local Broker
Before creating the bucket to filter stock quotes it is essential to have a broker adopter. Since In this example we are going to use local Broker, it is needed to create a broker with type local.To do that
- Start CEP Server and login as admin. (Follow the installation guide for more information on product startup)
In the Configure menu you can find a Menu item called "Broker" and under that click on 'Add' sub menu item.
You will be redirected to a page with header "Create a New Broker" and you need to enter following details in that form to create a local broker.
Broker Name : localBroker Broker Type : local
- Finally click on Add Broker button and you will get the added broker to the list of available brokers.
For more information about brokers visit here.
Create Bucket with Siddhi
To create a bucket use 'Add' sub menu item under CEP Buckets in the Main menu. Bucket creation form has three major sections. Basic information, Input and query. How to fill those sections is described below. If you need more information about buckets visit here.
Section 1 : Basic Information
Use the following information to fill the basic information section as shown in the below screen-shot.
Bucket Name (Name of the bucket) : XMLStockQuoteAnalyzer Description (Description about the bucket) : This bucket analyzes stock quotes and trigger an event if stock price >20 Engine Provider(CEP Runtime engine to be used) : SiddhiCEPRuntime [Choose from the drop down] Persistence snapshot time interval in minutes : 0 Enable distributed processing : false
Section 2 : Inputs
This section is used to define the inputs CEP will receive. To add an input click on Add Input link and then use following details. Screen shot is provided below for your convenience.
Topic( topic to events be received) : AllStockQuotes Broker Name (Broker to be used) : localBroker Mapping Stream (Name of the event stream) : StockPriceStream Query Event Type : Tuple Input Mapping Type : XML Mapping XPath prefixes Prefix : quotedata Namespace : http://ws.cdyne.com/
Properties (these properties will be extracted from the received xml event and fed to the CEP engine) Name : symbol xpath : //quotedata:StockQuoteEvent/quotedata:StockSymbol type : java.lang.String [Choose from the drop down Name : price xpath : //quotedata:StockQuoteEvent/quotedata:LastTradeAmount type : java.lang.Double [Choose from the drop down]
Important
Section 3 : Queries
This section is used to define the queries which will run on inputs and define outputs. To add a query click on Add query link and use following information. Screen shot is provided below for your convenience.
Query Name (To identify the query) : StockDetector Expression: from StockPriceStream insert into highStocks symbol,price group by symbol having ((price > 20)); Output(Define the output) Topic : highStocks Broker Name : localBroker Output Mapping : XML Mapping XML Mapping(Define the XML mapping) XML Mapping Text : <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:LastTradeAmount>{price}</quotedata:LastTradeAmount> </quotedata:StockQuoteDataEvent>
Important
Invoking Deployed Bucket
When the bucket is successfully deployed, an axis2 service will be automatically created. To send events to bucket we can use that service. Also we can output subscriber to handle outputs using the output topic name.
Define output subscriber
When the user send events to CEP with this service, there should be a subscriber to the output topic given, to receive filtered events from the CEP. So before send events to the engine we need to have an axis2 service deployed which prints received results to the console. Then create a subscription to the output topic, with providing the URL of the axis2 service as the subscription URL.
Use following java class and deploy it as a web service in CEP server. As for this example name the web service as FastMovingStockQuoteReceiverService.
import org.apache.axiom.om.OMElement; public class FastMovingStockQuoteReceiver { public void getOMElement(OMElement omElement){ System.out.println(""+omElement.toString()); } }
You will be able to see the axis2 service in the services list.
Now you can create a subscription to the output topic defined when configuring the bucket.
Create topic and subscribe
In order to subscribe, you need to create a topic.
Step 1: Click on "Add" sub menu item under "Topics" Menu in Manage section of the left panel
Step 2: Specify the topic name in the provided text box , in this case topic name is : "highStocks"(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.
Step 3: Once you click on the topic in topic browser page you will be able see four links as in the above image. Click in the subscribe link and you will be directed to Subscribe page. Then use following details to fill it. Once you are done click the Subscribe button.
Topic : highStocks (Output topic) Subscription mode : Topic only Subscription URL : http://localhost:9763/services/FastMovingStockQuoteService/getOMElement Expiration Time : select a future date from calender
Step 4 : You can verify whether you have correctly subscribe to the topic by click 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 (if 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 it is received to you subscription URL.
Sending events to CEP engine
Last step of invoking the bucket is sending events to CEP engine. As states earlier bucket service is deployed as an axis 2 service. Hence we can write a java client to sent requests to the axis 2 service. Following class will directly send events to axis2 service.
Above client will send a request to the deployed web service explained earlier.
When you run this class, if you have subscribed correctly to the output topic of the query, you will be able to see the filtered events in the console as below.
<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>MSFT</quotedata:StockSymbol> <quotedata:LastTradeAmount>26.36</quotedata:LastTradeAmount> </quotedata:StockQuoteDataEvent> <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>MSFT</quotedata:StockSymbol> <quotedata:LastTradeAmount>36</quotedata:LastTradeAmount> </quotedata:StockQuoteDataEvent>
Copyright © WSO2 Inc. 2005-2014