aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: RMI question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: RMI question" Watch "NX: RMI question" New topic
Author

NX: RMI question

Rob Pearson
Greenhorn

Joined: Jun 12, 2003
Posts: 19
This may be a silly question, but using RMI, is there only ever one instance of the remote class created?
I've got a server that creates an object then binds it in the Registry. I then run a number of instances of my GUI that all do a lookup on the remote object and get passed (I assume) a stub to the remote object. They then call methods on this, but is there always only ever one created? and if not, how do you create multiple? I can see multiple threads running on the remote object, but the constructor only gets called once?
Am I missing something?
Cheers
Rob


Assignment: URLyBird 1.3.3, using Java 1.4.0_01<br />Status: Designing, Experimenting, Reading.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Rob,
No, you are not missing anything. You instantiated the one and only remote object before or during your call to the bind() method.
You can instantiate more remote objects if you like - just make a factory and bind it to the registry.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Rob Pearson
Greenhorn

Joined: Jun 12, 2003
Posts: 19
Thanks Andrew,
Thats what I thought.
I got Max's book last week, and thought the design they adopted had a diagram with multiple instances of the adapter class, but I could only see one getting created in the code. I thought there may be som RMI magic that I missed.
Cheers
Rob
james render
Ranch Hand

Joined: May 08, 2003
Posts: 72
When you only bind one object to the registry, does that mean you have multiple (client) threads using that one object?
If I only have one Data instance, but with an adapter would it be more beneficial to have multiple instances of the adapter, or multiple threads using the same instance? Can't see that it would make much difference seen as how they all end up competing on the single Data objects resources..


[SCJP][SCWCD][SCJD]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
When you only bind one object to the registry, does that mean you have multiple (client) threads using that one object?
Yes. Only the one object you invoked bind() (or rebing()) on is shared. To share other objects, use a factory.
If I only have one Data instance, but with an adapter would it be more beneficial to have multiple instances of the adapter, or multiple threads using the same instance? Can't see that it would make much difference seen as how they all end up competing on the single Data objects resources..
Depends on your design. You can have a DBConnectionFactory which passes out DBConnection instances, (DBConnection and DBConnectionFactory both extend Remote). A DBConnection implementation would both forward all calls to the DB, and serve as a convenient way to identfy a particular client as it appears to the server. E.g. if you wanted to add a time-out feature later to release locks held by inactive clients, DBConnectionImpl is a good place to keep track of all locks held by that connection, and you can add a close() method which releases all locks held by that connection. If you make DBConnection extend Unreferenced, you can use the unreferenced() call to notify you of a disconnedted client, and clean up accordingly. A timeout feature is not necessary but might be nice to plan for, but handling disconected clients is probably necessary, so the whole DBConnectionFactory idea may be useful for this reason. You may be able to handle this other ways (e.g. the finally discussion we had) but I do like the idea of putting in some infrastructure that allows you to track individual clients from the server. And those pesky junior developers should recognize the idea of a Connection to a database from the java.sql classes - if they don't, well, it's in their interest to learn sometime.
[ June 26, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Rob Pearson:
Thanks Andrew,
Thats what I thought.
I got Max's book last week, and thought the design they adopted had a diagram with multiple instances of the adapter class, but I could only see one getting created in the code. I thought there may be som RMI magic that I missed.
Cheers
Rob

Hi Rob,
We tried to show that two approaches are possible, but we only implemented one. I don't have a copy of the book handy, but I'm pretty sure we have diagrams of how both would work in chapter 4.
M


Java Regular Expressions
james render
Ranch Hand

Joined: May 08, 2003
Posts: 72
Jim,
a lot of the functionality that you mentioned for the dbconnection is about tracking clients, for disconnections & unlocking.
The version of UrlyBird (1.3.1) that I'm working, as far as I can tell (the absence of cookie id on locking methods) does not require those features. I have made each update call atomic (locks/reads/updates/unlocks) as far as the client is concerned.
Are there any other benefits to the factory rmi concept? I'm inclined to follow Max's book and just have one object bound in the registry as this is the most simple solution.
I might have a ConnectionFactory for deciding whether to hand back a local or a networked version of the adapter.
James
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Are there any other benefits to the factory rmi concept?
Not that I can think of. But I'm still working through the implications of this stuff too, so I could well have missed something.
I'm inclined to follow Max's book and just have one object bound in the registry as this is the most simple solution.
Yeah, that's still very tempting. (In fact it's what I currently have; just considering refactoring.)
I might have a ConnectionFactory for deciding whether to hand back a local or a networked version of the adapter.
One thing to beware of here - my instructions say
"The program must be able to work in a non-networked mode. In this mode, the database and GUI must run in the same VM and must perform no networking, must not use loopback networking, and must not involve the serialization of any objects when communicating between the GUI and database elements."
The DBConnectionFactory I was talking about is a Remote object, extending UnicastRemoteObject. As such you can only create it in networked mode; you can't use it to select between networked and non-networked mode. (As I interpret the instructions anyway.) However you can create some other type of factory for this purpose - one that doesn't do any networking unless requested. Which may well be what you meant in the first place, but I wanted to be sure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: RMI question
 
Similar Threads
lock/unlock & synchronized
terminate of programs
How many instances are created?
Post Database lock scenario
How many instances are created?