[Download] | [Documentation Home] | [Release Note]

Eventing Sample

Introduction

When using data services, we may come across requirements where, when a certain action is done; such as invoking an operation with certain arguments or a specific response is returned, in that situation, we would like to trigger an event to notify of such actions. This is accomplished in WSO2 Data Services Server using input and output event triggers.

Prerequisites

The sample data service "EventingSample" should be deployed as per the instructions mentioned in Deploying Samples section.

Configuring the mail sender transport

In the sample, we are defining a specific topic for events to be published, and an email address is added as a subscription for that topic. In order to allow mail to be sent, we have to configure the mail sender in the client Axis2 configuration. A sample axis2.xml for the client configuration is stored at CARBON_HOME/samples/resources/sample_axis2_client.xml. Use the aforementioned file to replace the CARBON_HOME/repository/conf/axis2_client.xml file, now the axis2_client.xml will contain an already configured mail sender section and is ready to be used.

Editing the data service to add a valid email address

Here we will use the XML Edit option to edit the dataservice. In the XML view, replace all the occurences of "test@test.com" with your own email address.

After the above changes are done, restart the server.

Running the Sample

The sample service can be run using the "tryit" tool, which is bundled with WSO2 Data Services Server.

Service Definition

The service is based around the functionality of managing the inventory of a car/motorcycle dealership. It contain the following queries.

  • addProductQuery: This query adds a new product to the catalogue.
  • updateProductQuantityQuery: This query updates the availablity of a certain product. This is linked with an input event trigger, which monitors if the stock of this product is critically low. If so, if fires an event notifying the subscribers that this product's stock needs to be updated.
  • getProductByCodeQuery: This query retrieves product information, given the product code. The company gives special attention to people querying the database for motocycles. So when a query is made where the product type is a motorcyle, an output trigger associated with this query is triggers and a message is sent to the respective subscribers, here in this case, to our email address.

Sample Run

We will first add a new product into the inventory. This is shown in Figure 1.

Figure 1: Adding new product.

After adding the product, we will update the availability of it by changing the product quantity in stock.

Figure 2: Updating product stock.

Here we are setting the "quantityInStock" to 5, and because of the event trigger, when the stock is less than 10, an event is fired, which will be the case here. Soon you will receive an event notification to your mail inbox. Figure 3 shows the contents of the email attachment that will be received.

Figure 3: Input event trigger notification content.

Here we are querying the inventory system for a specific motorcycle model.

Figure 4: Querying an item.

By making a request with the motorcycle product line, we are triggering an event associated with it, and an email will be sent regarding this query. The contents of the email is shown below in Figure 5.

Figure 5: Output event trigger notification content.