package example.rmi;
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
...
public class ChatRoomImp extends UnicastRemoteObject implements ChatRoom {
   private java.util.Hashtable users =null;
   private boolean clearFailedUsers =true;
   ...

   public synchronized void distributeMessage(Message message){*1*
      Vector failedUsers =new Vector();
      for (Enumeration e =users.keys();e.hasMoreElements();){
            String userName =(String)e.nextElement();
         User user =(User)users.get(userName);
         try {
            user.receiveMessage(message);
         }catch (RemoteException ex){
            failedUsers.addElement(userName);
         }
      }
      if (!clearFailedUsers)return;

      clearFailedUsers =false;
      for (Enumeration e=failedUsers.elements();e.hasMoreElements();){
         leave((String)e.nextElement());
      }
      clearFailedUsers =true;
   }

   public synchronized void leave(String userName){
      Message message =new UserLeftMessage(userName);
      distributeMessage(message);
      users.remove(userName);
   }
   ...

   public tatic void main(String []args){
      if (args.length <1){
         System.out.println(«Required argument:»);
         return;

      }
      try {
         Naming.rebind(«rmi://localhost/»+args [0 ],
            new ChatRoomImp());
         *2*
      }catch (java.net.MalformedURLException e){
         e.printStackTrace();
      }catch (RemoteException e){
         e.printStackTrace();
      }
   }
}

Поделитесь материалом с коллегами и друзьями