Now 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 act as listeners for the stock event. If your event source has multiple listeners, those listeners should be separated by commas. 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 the application. This creates the stockPricer object, then creates the stockWatcher to listen to the stock events. Because no one is actually sending any events, nothing should actually happen.

In the following example, stockWatcher starts a thread that waits for 4 seconds, then fires an event.

Note: This example is for demonstration purposes only, 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.


Copyright © 1997, 2012 Oracle and/or its affiliates. All rights reserved.

Legal Notices