tags:

views:

210

answers:

3

Lets say you have interface definition.

That interface can be Operation.

Then you have two applications running in different JVMs and communicating somehow remotely by exchanging Operation instances.

Lets call them application A and application B.

If application A implements Operation with the class that is not available in the classpath of the application B, will application B still be able to handle that implementation of interface? Even when B is in different JVM?

A: 

As long as it only tries to reference it by the common Interface, it should be able to handle it. The actual implementation doesn't matter as long as they both implement the same version of the Interface.

Matthew Brubaker
Does it actually mean that the java byte code is sent over the wire?
jan
@jansokol: No it doesn't. By default serialisation just includes an objects 'state' - the value of its fields - and the class type. I'm not sure that this answer is correct, but I'd happily be proven wrong by a link to something...
Dan Vinton
+1  A: 

It depends on what you mean by "communicating somehow remotely". If application A actually just hands application B some sort of token which is built into a proxy such that calls to the Operation interface are proxied back to application A, then it may be okay. If the idea is for application B to create a local instance of the implementation class, then that's not going to work because it won't know what the object looks like.

Jon Skeet
Application B should call method on instance of operation.It does not require to instantiate the unknown class because it will receive instance of that class - implementation of known interface.
jan
How can it "receive" an instance of that class without actually instantiating it? *Something* has to create an instance in the JVM, doesn't it?
Jon Skeet
Application A will create instance of operation, send that instance to the Application B, and application B will call method on that instance.
jan
You're just repeating yourself. *How* is that instance going to be sent to application B? What will it look like in memory? If it's an object, it has to be an instance of a class... what's the type going to be if application B doesn't know about the class?
Jon Skeet
Let me try to be clear. Application A creates instance of Operation. Sends it as a method parameter in the EJB call (RMI). Application B calls method on received instance of Operation. What is the class then in application B I don't know. And if this can work at all is actually my question.
jan
Again, you're just repeating what you've said before. The point is that you *can't* do that - precisely because Application B has to create an instance of Operation when it receives the data from Application A. If it doesn't have the right class to create an instance of, it's basically stuffed.
Jon Skeet
Sorry for repeating myself but more or less that was my question. If I would know answer I would not ask it :)So if I understand correctly, answer to my original question is "no".
jan
And that was more or less my answer, 6 hours ago. Read the last sentence.
Jon Skeet
+5  A: 

It depends on the magic that happens in your "somehow communicate remotely" part.

If this communication is done via RMI or a similar technology, then this will be fine. Application B will create a remote proxy to the Operation object in JVM A, and calling methods on this proxy generate HTTP requests to JVM A, which are resolved against the actual object living in that JVM (which has access to the implementing class).

If this communication is done by serialising objects and sending them over the wire, then it will not work. When the object from application A arrives in JVM B, the deserialisation will fail (with a ClassNotFoundException or similar).

There could well be other remoting technologies, in which case things are implementation dependent. I know that Classloaders can load classes from byte arrays, and thus it's conceptually very possible to have such classloaders that would be able to load classes from remote sources. The networking library could in theory serialise the actual class across the wire this way, I believe, so while JVM B would not natively know about the implementing class, its classloader would be provided with the class' bytecode as required.

Andrzej Doyle