Saturday, October 24, 2015

WSO2 ESB: Polling a database for changes

WSO2 ESB has DBLookup mediator to read data from a database. But in some cases, you may need ESB to keep polling a database and proceed with some operations only if there's a change in it. Although this is not supported out-of-the-box, we can easily write a simple class mediator to implement this requirement.

One problem that arises when doing this is that there's no way to keep a value in-memory across multiple messages, since the properties used in ESB artifacts are local to the message context. To overcome this, we can consider two options:
  1. Writing a class mediator that keeps the last read value in an instance variable and performs any comparisons required to identify changes in the database.

  2. Simply store the last read value to another table/field and compare it with the current field (this requires multiple DBLookup/Report calls).

In this blog post, we'll explore how we can implement the first option.

So the basic flow will be:
  • A scheduled task keeps calling a sequence
  • In the called sequence, we have a DBLookup mediator that fetches a field from a database table (includes a column that indicates a change, such as a timestamp)
  • The change indicator field is extracted and added to a property
  • Class mediator is called, it compares the property's current value with the previously stored value and sets the result (changed or not) to another property
  • With a filter on the resultant property, we can identify a change and proceed with necessary operations

A sample configuration is as follows.

The scheduled task that keeps calling a sequence:


The sequence that does the DB lookup and identifies if there's a change:


Sample code for the class mediator used in the above sequence can be found here.

No comments:

Post a Comment