Ive managed to create an RMI application that does what i need it to do quite succesfully, but im having a bit of trouble getting my head around where client obtains definitions for remote objects. for example:
I have a server that registers itself with the rmiregistry (allowing clients to call methods on it).
UnicastRemoteObject.exportObject(new Server(), 0);
running reg.list()
confirms that my server has indeed been added to the registry. I have another remote object (rObj) running on the same JVM as the server. This is not added to the registry.
In my client开发者_JS百科, i can get the definition of my Server class by looking up Server
in the rmiregistry:
reg.lookup("Server")
after this can freely create instances of rObj. The crux of my question is, where does my client get a definition for rObj even though its never been added to registry.
I know it must come from the server as thats where the class and interface are stored. Does the connection to Server
automatically open the pipe for other remote classes to received?
If so, how does the client know to look on the server for the remote class. Is the server treated almost as an extension to the clients classpath (it will resort to checking the server for classes that arent in its own classpath)?
First of all, realize that it's not necessary to set up dynamic classloading from the server in order to use RMI. If you compile the interface and implementation into both the client and server jars, then everything will work fine. That is how I've almost always implemented RMI.
If you have a good reason for loading the classes dynamically from the server, you'll need to set up an HTTP server somewhere that has the interfaces and implementation classes (preferably in a jar file, although a class directory will work too). This doesn't happen automatically as part of RMI, you need to build the jars and put them somewhere on your web server. Then launch the client with a system property indicating the URL to this jar file:
-Djava.rmi.server.codebase=http://webline/public/mystuff.jar
This is explained in full detail here: http://download.oracle.com/javase/1.5.0/docs/guide/rmi/codebase.html
If you use new
to create new instances of the same type (say, T
) as rObj
, then of course the Java compiler knew the definition of T
, and your application also knows it at runtime. In this case, no RMI is involved at all.
But maybe I misunderstood your question? How exactly do you "freely create instances of rObj"?
Update: I'm eating my words here, of course being able to compile the file, and having the class available on the classpath at runtime or two different issues. Since you were not mentioning the classpath at all, I was assuming you'd somehow ended up having the classes on the client-side anyway.
精彩评论