HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Improved Observer/Observable with Remote Capability"
Vol. 2, Issue 10, p. 36

	

Listing 1.
 
public interface Publisher extends Remote {  
  public void addSubscriber(Subscriber s)  
    throws RemoteException; 

  public void removeSubscriber(Subscriber s)  
    throws RemoteException; 

  public void removeAllSubscribers()  
    throws RemoteException; 
} 
  

public interface Subscriber extends Remote {  
  public void update(Object pub, Object code)  
    throws RemoteException; 
} 

Listing 2.
 
public class BasicPublisher implements Publisher { 
  protected Vector subscribers = new Vector(2); 

  public void addSubscriber(Subscriber s) { 
    subscribers.addElement(s); 
  } 

  public void removeSubscriber(Subscriber s) { 
    subscribers.removeElement(s); 
  } 

  public void removeAllSubscribers() { 
    subscribers.removeAllElements(); 
  } 

  public void notifySubscribers(Object pub, Object code) { 
    Vector deadSubs = null; 
    Enumeration e = subscribers.elements(); 
    while (e.hasMoreElements()) { 
      Subscriber s = (Subscriber) e.nextElement(); 
      try { s.update(pub, code); } 
      catch (java.rmi.ConnectException ce) { //serious 
        if (deadSubs == null) deadSubs = new Vector(); 
        deadSubs.addElement(s);// must be dead 
      } 
      catch (java.rmi.NoSuchObjectException nsoe){ //serious  
        if (deadSubs == null) deadSubs = new Vector(); 
        deadSubs.addElement(s);// must be dead 
      } 
      catch (java.rmi.RemoteException re) { 
        /*might recover?*/ 
      } 
    } 
    if (deadSubs != null) { 
      e = deadSubs.elements(); 
      while (e.hasMoreElements()) { 
        Subscriber s = (Subscriber) e.nextElement(); 
        removeSubscriber(s);  // forget this subscriber 
      } 
    } 
  } 

  public void notifySubscribers(Object pub) { 
    notifySubscribers(pub, null); 
  } 
} 

Listing 3. 

public class XX implements Publisher { 
  BasicPublisher pub = new BasicPublisher(); 

  /** Delegate Publisher interface to BasicPublisher */ 
  public void addSubscriber(Subscriber s) { 
    pub.addSubscriber(s);  
  } 
  public void removeSubscriber(Subscriber s) { 
    pub.removeSubscriber(s);  
  } 
  public void removeAllSubscribers() { 
    pub.removeAllSubscribers(); 
  } 
  ... 
  ... 
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.