Observer et observable en rmi

Description

L'observer et l'observable sert à modéliser des vues et de faire des actions communes sur chacune. Anisi, dans le cadre d'un client/serveur, les données qui ont changées par un utilisateur seront apparents pour les autres utilisateurs (notion de rafraîchissement de données). Or le problème, en RMI, c'est que ces deux classes sympathiques ne peuvent pas fonctionner. Ainsi, j'ai fait un petit package util et fonctionnel (utilisation de Publishing et de Subscribing).

Voilà

Source / Exemple :


/*

  • Created on 22 janv. 2006
*
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
package observer; import java.io.Serializable; import java.rmi.Remote; import java.rmi.RemoteException; /**
  • @author Julien
*
  • TODO To change the template for this generated type comment go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
public interface Subscriber extends Remote, Serializable { public void update(Object pub, Object code) throws RemoteException; } ///////////////////////////////////////////////////////////////////////////////// /*
  • Created on 22 janv. 2006
*
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
package observer; import java.rmi.Remote; import java.rmi.RemoteException; /**
  • @author Julien
*
  • TODO To change the template for this generated type comment go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
public interface Publisher extends Remote { public void addSubscriber(Subscriber s) throws RemoteException; public void notifyAllSubscribers()throws RemoteException; public void removeSubscriber(Subscriber s) throws RemoteException; public void removeAllSubscribers() throws RemoteException; } ///////////////////////////////////////////////////////////////// /*
  • Created on 22 janv. 2006
*
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
package observer; import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.Enumeration; import java.util.Vector; /**
  • @author Julien
*
  • TODO To change the template for this generated type comment go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
public class BasicPublisher extends UnicastRemoteObject implements Publisher, Serializable { private Vector subscribers; public BasicPublisher() throws RemoteException { subscribers = new Vector(); } public void addSubscriber(Subscriber s) throws RemoteException { subscribers.addElement(s); } public void notifyAllSubscribers() throws RemoteException { notifySubscribers(null); } public void removeSubscriber(Subscriber s) throws RemoteException { subscribers.removeElement(s); } public void removeAllSubscribers() throws RemoteException { 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(); try { removeSubscriber(s); // forget this subscriber } catch (RemoteException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } public void notifySubscribers(Object pub) throws RemoteException { notifySubscribers(pub, null); } } ///////////////////////////////////////////////////////////////////////// /*
  • Created on 22 janv. 2006
*
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
package observer; import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; /**
  • @author Julien
*
  • TODO To change the template for this generated type comment go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
public class Publishing extends UnicastRemoteObject implements Publisher, Serializable { BasicPublisher pub; public Publishing() throws RemoteException { pub = new BasicPublisher(); } /** Delegate Publisher interface to BasicPublisher */ public void addSubscriber(Subscriber s) throws RemoteException { pub.addSubscriber(s); } public void notifyAllSubscribers() throws RemoteException{ pub.notifyAllSubscribers(); } public void removeSubscriber(Subscriber s) throws RemoteException{ pub.removeSubscriber(s); } public void removeAllSubscribers() throws RemoteException { pub.removeAllSubscribers(); } } //////////////////////////////////////////////////////////////////////// /*
  • Created on 22 janv. 2006
*
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
package observer; import java.lang.reflect.*; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; /**
  • @author Julien
*
  • TODO To change the template for this generated type comment go to
  • Window - Preferences - Java - Code Style - Code Templates
  • /
public class Subscribing extends UnicastRemoteObject implements Subscriber { //Variables globales private Method methode; private Object instance; private Object[] params; public Subscribing(Object instance, Method methode, Object[] params) throws RemoteException { this.instance = instance; this.methode = methode; this.params = params; this.methode.setAccessible(true); } /* (non-Javadoc)
  • @see dico.observer.Subscriber#update(java.lang.Object, java.lang.Object)
  • /
public void update(Object pub, Object code) throws RemoteException { // TODO Auto-generated method stub try{ methode.invoke(instance, params); } catch(Exception e){ } } }

Conclusion :


Enjoy it :D

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.