Powered By Blogger

May 15, 2014

WSO2 Siddhi


WSO2 CEP is an enterprise grade server which integrates with various systems to analyze meaningful patterns in real world basically financial analysis, fraud detection, sales, shipping and other business events. The core backend runtime engine of the WSO2 CEP Server is WSO2 Siddhi. 

When considering how Siddhi works, basically it receives incoming events in Event Streams through input handlers and then it process them, and finally notifies using callbacks. When concerning the Event, it has a stream ID, time stamp representing the event creation time and an array containing the attributes. 

Initially an event stream needs to be defined specifying its name and its attributes. The attributes should define as a pair of its name and the type. After defining the stream Siddhi creates an input handler which uses to send the defined stream into the system. Also, callbacks can be used to receive notifications when events are produced on the event stream. Finally the event is projected into the outgoing event stream based on the defined outgoing stream attributes.

Some of the basic Siddhi Queries

  • Pass - through
Pass - through query creates an outgoing (output) stream and include the events from the incoming (input) stream as projected.

Following projects are available

1. All
2. Selected attributes

Following are some example which relates with the pass - through Queries

from DataStream 

select *  
insert into DisplayItems;

from DataStream 
select price, quantity
insert into DisplayItems;

  • Filter
Filter query creates an outgoing (output) stream and include the events from the incoming (input) stream which satisfies the conditions defined in the query. The main conditions support by Filters are as follows 


1. >, <, ==, >=, <=, !=
2. contains, instanceof
3. and, or, not


Following are some example which relates with the Filter Queries
from DataStream[price >= 100]select price, quantity
insert into DisplayItems
from DataStream[name instanceof string]

select name,price
insert into DisplayItems 

from DataStream[name contains 's']

select name,price
insert into DisplayItems

from DataStream[price >= 20 and price < 100]

select price,quantity
insert into DisplayItems  


  • Window
Users can define a window and then use the events on the window for calculations. A window has two types of output: current events and expired events. A window shows current events when a new event arrives. Expired events are showed whenever an existing event has expired from a window.

If we want to calculate the quantities sold in the last hour, we can improve the above query by adding a time window.
from DataStream#window.time( 60 min ) 

select name, sum(quantity) as totalQuantity
group by quantity 
insert into DisplayItems 


  • Join
Join considers two streams as input streams where each stream should have an associated window. With the condition this joins the events matches the condition and with the time it joins the events that falls within the time.


Following example shows a join query.


from DataStream#window.time( 60 min ) as ds 
join NewDataStream#window.time( 60 min) as nds
on ds.name == nds.name
insert into DisplayItems ds.name as brandName, ds.price as tradePrice


Here events from the DataStream and NewDataStream will be joined only if the events of DataStream have a name which is equal to the NewDataStream's name

May 14, 2014

Create a JDBC mount to WSO2 Governance Registry


This blog post basically describes about the way to do a JDBC mount by using WSO2 Governance Registry (GReg) and WSO2 Enterprise Service Bus (ESB)

Applies To : WSO2 Governance Registry 4.6.0

                      WSO2 Enterprise Service Bus 4.8.1

Configure Governance Registry

  • Create a new database for Governance Registry (eg: GregDB)
  • Then update the datasource configuration in master-datasource.xml in $GREG_HOME/repository/conf/datasources
  • Update the url, username, password and driverClassName of the datasource configuration

<datasource>

           <name>WSO2_CARBON_DB</name>

           <description>The datasource used for registry and user manager</description>

           <jndiConfig>
               <name>jdbc/WSO2CarbonDB</name>
           </jndiConfig>
           <definition type="RDBMS">
               <configuration>
                   <url>jdbc:mysql://localhost:3306/GregDB</url>
                   <username>root</username>
                   <password>root</password>
                   <driverClassName>com.mysql.jdbc.Driver</driverClassName>
                   <maxActive>50</maxActive>
                   <maxWait>60000</maxWait>
                   <testOnBorrow>true</testOnBorrow>
                   <validationQuery>SELECT 1</validationQuery>
                   <validationInterval>30000</validationInterval>
               </configuration>
           </definition>
 </datasource>

  • Enabling clustering in axis2.xml in $GREG_HOME/repository/conf/axis2 to send invalidation messages across WSO2 GREG and WSO2 ESB.
<clustering class="org.apache.axis2.clustering.tribes.TribesClusteringAgent" enable="true"/>

  • Copy the MySql driver library (mysql-connector-java-5.1.13-bin.jar) to $GREG_HOME/repository/components/lib directory.
Configure WSO2 Enterprise Service Bus
  • Enable database access by copying the MySQL driver library (mysql-connector-java-5.1.13-bin.jar) to $ESB_HOME/repository.components/lib.
  • Add a new datasource configuration to master-datasources.xml in $ESB_HOME/repository/conf/datasources

<datasource>

           <name>WSO2_CARBON_DB_GREG</name>

           <description>The datasource used for registry and user manager</description>

           <jndiConfig>
               <name>jdbc/WSO2CarbonDB_GREG</name>
           </jndiConfig>
           <definition type="RDBMS">
               <configuration>
                   <url>jdbc:mysql://localhost:3306/GregDB</url>
                   <username>root</username>
                   <password>root</password>
                   <driverClassName>com.mysql.jdbc.Driver</driverClassName>
                   <maxActive>50</maxActive>
                   <maxWait>60000</maxWait>
                   <testOnBorrow>true</testOnBorrow>
                   <validationQuery>SELECT 1</validationQuery>
                   <validationInterval>30000</validationInterval>
               </configuration>
           </definition>
       </datasource>

  • Add new db config to registry.xml in $ESB_HOME/repository/conf in order to refer the newly added data source.

<dbConfig name="mounted_registry">

       <dataSource>jdbc/WSO2CarbonDB_GREG</dataSource>
</dbConfig>



  • Add a remote governance registry instance in registry.xml


<remoteInstance url="https://localhost:9444/registry">

       <id>instanceid</id>

       <dbConfig>mounted_registry</dbConfig>
       <readOnly>false</readOnly>
       <enableCache>true</enableCache>
       <registryRoot>/</registryRoot>
       <cacheId>root@jdbc:mysql://localhost:3306/GregDB</cacheId>
</remoteInstance>

  • Add mount configurations to registry.xml

<mount path="/_system/config" overwrite="true">
       <instanceId>instanceid</instanceId>
       <targetPath>/_system/nodes</targetPath>
   </mount>

<mount path="/_system/governance" overwrite="true">
       <instanceId>instanceid</instanceId>
       <targetPath>/_system/governance</targetPath>
   </mount>

  • Enabling clustering in axis2.xml in $GREG_HOME/repository/conf/axis2 to send invalidation messages across WSO2 GREG and WSO2 ESB.

<clustering class="org.apache.axis2.clustering.tribes.TribesClusteringAgent" enable="true"/>

  •  Sign in to the ESB server  using the default username and password (admin/admin) and browse the Registry. Now you should see the config and governance collections are updated with arrow icons as shown below