Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMI and the Observer pattern

 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two parts to my program the GUI and the DB.
These work together using the Observer pattern so the GUI gets notified when the DB changes.

It all worked great untill I added the RMI part of the program.

The GUI still registers with the DB as an observer. The problem is when the DB notifies the registered observers, the observers on the db stub(or server side) get notified...not the original observer.

Has anyone got information to get around this problem?

note I have creaked my own observer & observable interfaces to over come serializable problems
 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After god knows how many hours I found the solution...which is very simple.

You need to register the Client object as well as the Server object with the RMI registry
UnicastRemoteObject.exportObject(client);
 
Frank Verbruggen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was not sufficient for me...

public void addObserver(RemoteObserver observer)
throws RemoteException {
if (remote) {
UnicastRemoteObject.exportObject(observer);
}
serverInstance.addObserver(observer);
}

What more needs to be done ???
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are trying to do RMI callback, right (meaning server notifies all clients of DB changes)? If yes, the easiest way (according to my Java book) is to create an observer interface that also extends from Remote, and have an implementation class that extends UnicastRemoteObject and implements that interface. Now you can pass this object to the database server.

In other words, this time round, both the client and server have a Remote interface for communication.

I've tried it, and it works perfectly.

Edit: I have tried not extending UnicastRemoteObject (if you do that, the Remote subinterface must also extend Serializable), and manually exporting the object using the exportObject() method. It actually works. If you want me to send you my sample codes, my pleasure.
[ February 16, 2005: Message edited by: Liang Anmian ]
 
Frank Verbruggen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've improved my code since:







Thanx anyhow

NOTE: Plz remove this post if it contains too much code...

Greetz


Frank
 
Kris Reid
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention that I made my own observer interface and observable class. This was done for the very reason that they need to extend remote and serializable.

The Java observer pattern is not serializable so can't work over a network
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic