Using ADF BC Event Listeners to monitor data changes application wide through RSS Feeds and Monitor Pages

0

ADF Business Components has a built mechanism of event-producers and listener-registration that allows for non-intrusive monitoring of data changes. At the level of Entity Objects, changes in one Entity Object instance can be communicated to other associated Entities. ViewObjects allow registration of RowSetListeners – objects that are notified of various events on the RowSet, such Insert, Update and Delete of Rows as well as navigation events.

In this article we will look at the development of an application wide monitor of data events, based on this infrastructure. I will create both a Web page as well as an RSS feed that reports the 25 most recent salary change events – created in any session, by any user, across the JVM.

The initial set up ....
for the project contains a simple ADF BC Application Module AppModule with a DeptView and EmpView ViewObject.


 
In order to monitor Salary Change events, we need to register a Listener on the EmpView ViewObject. That Listener is an object that is notified of all events on the EmpView and can handle as it sees fit. The Listener needs to implement the RowSetListener interface.

The EmpListener class looks as follows:

package nl.amis.adfbc;<br /><br />import java.util.ArrayList;<br />import java.util.Arrays;<br />import java.util.List;<br /><br />import oracle.jbo.DeleteEvent;<br />import oracle.jbo.InsertEvent;<br />import oracle.jbo.NavigationEvent;<br />import oracle.jbo.RangeRefreshEvent;<br />import oracle.jbo.RowSetListener;<br />import oracle.jbo.ScrollEvent;<br />import oracle.jbo.UpdateEvent;<br /><br />public class EmpListener implements RowSetListener {<br />    private static List&lt;SalaryChangeEvent&gt; events;<br />    private static EmpListener el = new EmpListener();<br /><br />    public static EmpListener getEmpListener() {<br />      return el;<br />    }<br />    private EmpListener() {<br />      events = new ArrayList(50);<br />    }<br /><br />    public void rangeRefreshed(RangeRefreshEvent rangeRefreshEvent) {<br />    }<br /><br />    public void rangeScrolled(ScrollEvent scrollEvent) {<br />    }<br /><br />    public void rowInserted(InsertEvent insertEvent) {<br />    }<br /><br />    public void rowDeleted(DeleteEvent deleteEvent) {<br />    }<br /><br />    public void rowUpdated(UpdateEvent updateEvent) {<br />      List attrs  = Arrays.asList( updateEvent.getChangedAttrNames());<br />      if (attrs.contains(&quot;Sal&quot;)) {<br />          System.out.println(&quot;Employee &quot;+updateEvent.getRow().getAttribute(&quot;Ename&quot;)<br />          +&quot; has new salary &quot;+updateEvent.getRow().getAttribute(&quot;Sal&quot;)+&quot;. His old salary was &quot;+updateEvent.getRow().getAttribute(&quot;PreviousSal&quot;));<br />        events.add<br />        (0, new SalaryChangeEvent<br />              ( (String)updateEvent.getRow().getAttribute(&quot;Ename&quot;)<br />          , ((oracle.jbo.domain.Number)updateEvent.getRow().getAttribute(&quot;PreviousSal&quot;)).floatValue()<br />          , ((oracle.jbo.domain.Number)updateEvent.getRow().getAttribute(&quot;Sal&quot;)).floatValue()<br />          , (String)updateEvent.getRow().getAttribute(&quot;Job&quot;)<br />          , ((oracle.jbo.domain.Number)updateEvent.getRow().getAttribute(&quot;Deptno&quot;)).toString()<br />              )<br />        );<br />        // trim back to 25<br />        while (events.size()&gt;25) {<br />            events.remove(events.size()-1);<br />        }<br />      }<br />    }<br /><br />    public void navigated(NavigationEvent navigationEvent) {<br />    }<br /><br />    public static List&lt;SalaryChangeEvent&gt; getEvents() {<br />        return events;<br />    }<br />}<br />

It implements the Singleton Pattern – there will be only a single EmpListener instance throughout the application.
The listener is registered on every instance of the EmpView ViewObject, through its create() method:

public class EmpViewImpl extends ViewObjectImpl {<br />  /**This is the default constructor (do not remove)<br />   */<br />  public EmpViewImpl() {  <br />  }<br /><br />    protected void create() {<br />        super.create();<br />        addListener(EmpListener.getEmpListener());<br />    }<br />}<br />

The listener class makes use of the Bean definition of SalaryChangeEvent, a bean with properties such as EmployeeName, Deptno, OldSalary, NewSalary and Job. Whenever a new Salary Change Event is processed, an instance of the SalaryChangeEvent bean is created and added to the top of the Events list, as to keep the events in chronological order, the most recent first. 

A simple master-detail page is developed to maintain the Employee-data. Through this page, we generate the Salary Change events – by simply changing the salary and submitting the change.


 
Note: we can run  many sessions for this page, in Firefox and IE, on different machines: as long as the sessions are in same OC4J instance and therefore in the same JVM, all events are collected in the single instance of EmpListener.


 
By creating a DataControl for the SalaryChangeEventManager class that acts as an intermediary for the EmpListener, we can quickly create a web page with read only table that presents the 25 most recent salary changes.

 

package nl.amis.adfbc;<br /><br />import java.util.List;<br /><br />public class SalaryChangeEventManager {<br />    public SalaryChangeEventManager() {<br />    }<br />    <br />    public List&lt;SalaryChangeEvent&gt; getSalaryChangeEvents() {<br />        return EmpListener.getEvents();<br />    }<br />}<br />

 

A simple JSP can be created to produce an RSS feed that presents that same information:

&lt;%@ page contentType=&quot;text/xml;charset=windows-1252&quot;%&gt;<br />&lt;%@ page import=&quot;java.util.*, java.text.*, nl.amis.adfbc.*&quot; %&gt;<br />&lt;% <br />       SimpleDateFormat dateFormat = new SimpleDateFormat(&quot;EEE, d MMM yyyy HH:mm:ss z&quot;);<br />%&gt;<br />&lt;rss version=&quot;2.0&quot; &gt;<br />        &lt;channel&gt;<br />            &lt;title&gt;HRM Salary Changes&lt;/title&gt;<br />            &lt;link&gt;http://www.amis.nl&lt;/link&gt;<br />            &lt;description&gt;Most recent Salary Changes&lt;/description&gt;<br />            &lt;copyright&gt;Copyright(c) 2007 AMIS.nl&lt;/copyright&gt; <br />             &lt;language&gt;en&lt;/language&gt;<br />            &lt;docs&gt;http://technology.amis.nl/blog/rss&lt;/docs&gt;<br />            &lt;lastBuildDate&gt;&lt;%=dateFormat.format(Calendar.getInstance().getTime())%&gt;&lt;/lastBuildDate&gt; <br />            &lt;%<br />              List&lt;SalaryChangeEvent&gt; events = EmpListener.getEvents();<br />              for (SalaryChangeEvent event:events)<br />              {<br />            %&gt;<br />            &lt;item&gt;<br />               &lt;title&gt; &lt;%=event.getEmployeeName()%&gt; (&lt;%=event.getJob()%&gt;)&lt;/title&gt;<br />               &lt;description&gt;New Salary: &lt;%=event.getNewSalary()%&gt;; Old Salary:&lt;%=event.getOldSalary()%&gt;&lt;/description&gt;<br />               &lt;pubDate&gt;&lt;%=dateFormat.format(event.getTimestamp())%&gt;&lt;/pubDate&gt;                  <br />            &lt;/item&gt;<br />
           &lt;%<br />              }<br />
            %&gt;<br />          &lt;/channel&gt;<br />&lt;/rss&gt; <br />

 

What we have done so far is quite nice. However, there are some issues, that may or may not be crippling:

  • The application has been invaded: an RSS rendering JSP or an ADF DataControl with Web Pagehave been added to the application for administrative purposes. They could have been created in a separate web application that is deployed in conjunction with the primary application the same container. However, for monitoring data changes in an application based on ADF BC not having a web interface of its own, we need a different mechanism.
  • The approach used here with ADF BC listeners and a static event collector works well, but only only within the JVM: there is no way to get information outside of the JVM, otherwise than through a Web UI or other explicit external communication initiated from within the application
  • We cannot make the distinction between successful and ultimately rejected/rolled back changes; we could perhaps post changes through the event listener, tied to the current transaction, and have the transaction’s successful commit or rollback also trigger a notification to the event collector.. (Steve: any suggestions?).

An interesting alternative approach, which I will discuss in a subsequent article later this week, is using MBeans to expose this kind of information from within an application. The application is slightly impacted by the extension with the MBeans – ever so slightly – and otherwise could not care less about who will access the MBeans and for what purpose. Note that MBeans can accessed from outside the application – any Java application, web or otherwise. Note that the third issue – distinction between real and ultimately failed salary changes – is still to be resolved.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Comments are closed.