Twitter and StockQuote Analyzer
This sample demonstrates how Siddhi engine can be used with JMS broker to receive, process and publish JMS Map & XML messages.
In this sample CEP will receive events from stock quotes stream and Twitter feeds stream, and trigger an event if the last traded amount of the stock quotes stream vary by 2 percent with regards to the average traded price of a symbol within past 2 minutes and the word count of the Twitter feeds stream with related to that symbol is greater than 10.
from allStockQuotes#window.time(120000) insert into fastMovingStockQuotes symbol,price, avg(price) as averagePrice group by symbol having ((price > averagePrice*1.02) or (averagePrice*0.98 > price ))
from twitterFeed#window.time(1200000) insert into highFrequentTweets company as company, sum(wordCount) as words group by company having (words > 10)
from fastMovingStockQuotes#window.time(120000) as fastMovingStockQuotes join highFrequentTweets#window.time(120000) as highFrequentTweets on fastMovingStockQuotes.symbol == highFrequentTweets.company insert into predictedStockQuotes fastMovingStockQuotes.symbol as company, fastMovingStockQuotes.averagePrice as amount, highFrequentTweets.words as words
In this sample we will publish JMS Map events using a Java client and the outputted JMS XML events from the bucket will be published to a JMS subscriber to receive and log those events.
Following is the configuration used in this sample.
Prerequisites
- Apache Ant to build & deploy the Sample & Service, and to run the client. Refer Installation Prerequisites for instructions to install Apache Ant.
- ActiveMQ JMS Broker to publish and subscribe events. Refer Installation Prerequisites for instructions to install ActiveMQ JMS Broker.
Deploying the configurations
The steps are as follows :
- Install the WSO2 Complex Event Processor, but do not start the server, Refer to the Installation Guide for instructions.
Copy paste activemq-all-xxx.jar from the <ActiveMQ_HOME> directory to <CEP_HOME>/samples/lib directory.
Copy paste activemq-core-xxx.jar and geronimo-j2ee-management_1.1_spec-1.0.1.jar from <ActiveMQ_HOME>/lib to <CEP_HOME>/repository/components/lib directory.
- In a command prompt, switch to the sample directory: <CEP_HOME>/samples/cep-samples
For example, in Linux: cd <CEP_HOME>/samples/cep-samples - From there, type ant deploy-jms,
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. - Now start the WSO2 Complex Event Processor. Refer to the Installing the Product for instructions.
Starting JMS subscriber
The steps are as follows :
- Start ActiveMQ JMS Broker. Refer Installation Prerequisites for instructions to run ActiveMQ JMS Broker.
- In a new command prompt, switch to the sample directory: <CEP_HOME>/samples/cep-samples
For example, in Linux: cd <CEP_HOME>/samples/cep-samples - From there, type ant jmsSubscriber -Dtopic=PredictedStockQuotes, this will subscribe to the PredictedStockQuotes topic of the ActiveMQ Broker receiving the output events of CEP.
Publishing events
The steps are as follows :
- 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 - From there, type ant jmsAllStockQuotesPublisher
This will publish some JMS Map events to AllStockQuotes topic. - Then from there, type ant jmsTwitterFeedPublisher
This will publish some JMS Map events to TwitterFeed topic.
Observation
You will be able observe the output events in the JMS subscriber console as shown bellow.