Now we’ll create the two services. Create a localconfig/test/services/stockWatcher.properties file that looks like this:

$class=StockWatcher

And create a localconfig/test/services/stockPricer.properties file that looks like this:

$class=StockPricer
stockListeners=stockWatcher

The stockListeners property is recognized by Nucleus as indicating that the specified services will act as listeners for the stock event. If your event source has multiple listeners, those listeners should be separated by commas. Note that this means that the Bean should avoid creating a property called stockListeners.

Modify localconfig/Initial.properties to specify the initial service:

initialService+=/services/stockPricer

Now restart your application. This will create the stockPricer object, then create the stockWatcher to listen to the stock events. Of course, no one is actually sending any events, so you shouldn’t see anything happen.

In order to get an event to fire, we’ll have stockWatcher start a Thread that waits for 4 seconds, then fires an event. Note that this is only for demonstration purposes and should not be considered a general programming technique:

public class StockPricer implements Runnable {
  java.util.Vector listeners = new java.util.Vector ();

  public StockPricer () {
    new Thread (this).start ();
  }
  public void run () {
    try { Thread.sleep (4000); }
    catch (InterruptedException exc) {}
    broadcastStockEvent (new StockEvent (this, "ATGC", 20.75));
  }
  public synchronized void addStockListener (StockListener listener) {
    listeners.addElement (listener);
  }
  public synchronized void removeStockListener (StockListener listener) {
    listeners.removeElement (listener);
  }
  public synchronized StockListener [] getStockListeners () {
    StockListener [] ret = new StockListener [listeners.size ()];
    listeners.copyInto (ret);
    return ret;
  }
  public void broadcastStockEvent (StockEvent ev) {
    for (int i = 0; i < listeners.size (); i++) {
      ((StockListener) listeners.elementAt (i)).stockPriceUpdated (ev);
    }
  }
}

Now reassemble your application. When you restart it, the StockPricer should wait 4 seconds, then fire an event to the StockWatcher, which prints the event out.

 
loading table of contents...