In a distributed client server application, i am using java RMI to invoke server side method from client machine. At server side EJB is being used and application server is Glassfish. I have a SampleFacade Class at server end which is a java session bean and SampleFacadeRemote is a Remote interface exposed to client (@Remote has been used with it). SampleFacade
implements SampleFacadeRemote
Interface .
Please look at the following code snippet :
private static SampleFacadeRemote lookupSampleFacade()
{
Context c = new InitialContext();
return (SampleFacadeRemote)c.lookup("java:comp/env/SampleFacade");
}
catch (NamingException ne)
{
throw new RuntimeException(ne);
}
Here, with the returned object, remote method can be successfully executed.
I am curious to know how it works. Does lookup method return SampleFacade
object to client in order to execute its method? But SampleFacade
's method is suppose to be run at server side/Remote JVM. then what type of object is being returned by lookup()
method? Please explain the underlying mechanism.
It returns a stub: a client-side object that implements the same remote interface and knows how to communicate with the actual remote object.
In fact you should note that it isn't the lookup()
method that performs this magic: it is ultimately the remote object itself that translates itself to its stub. The stub is already in the Registry or LDAP or whatever you're looking up, and lookup()
just gives you whatever was found.