Java remote method invocation: Difference between revisions

Content deleted Content added
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 22:
'''<code>RmiServer</code> class''' &mdash; listens to RMI requests and implements the interface which is used by the client to invoke remote methods.
 
<sourcesyntaxhighlight lang=java>
import java.rmi.Naming;
import java.rmi.RemoteException;
Line 58:
}
}
</syntaxhighlight>
</source>
 
'''<code>RmiServerIntf</code> interface''' — defines the interface that is used by the client and implemented by the server.
 
<sourcesyntaxhighlight lang=java>
import java.rmi.Remote;
import java.rmi.RemoteException;
Line 69:
String getMessage() throws RemoteException;
}
</syntaxhighlight>
</source>
 
'''<code>RmiClient</code> class''' &mdash; this is the client which gets the reference (a proxy) to the remote object living on the server and invokes its method to get a message. If the server object implemented java.io.Serializable instead of java.rmi.Remote, it would be serialized and passed to the client as a value.<ref>[http://www.javaworld.com/javaworld/jw-11-2000/jw-1110-smartproxy.html Get smart with proxies and RMI - JavaWorld]</ref>
 
<sourcesyntaxhighlight lang=java>
import java.rmi.Naming;
 
Line 82:
}
}
</syntaxhighlight>
</source>
 
Before running this example, we need to make a 'stub' file for the interface we used. For this task we have the RMI compiler - 'rmic'
*Note: we make a stub file from the '*.class' file with the implementation of the remote interface, not from the '*.java' file.
<sourcesyntaxhighlight lang=dos>
rmic RmiServer
</syntaxhighlight>
</source>
Note that since version 5.0 of J2SE support for dynamically generated stub files has been added, and rmic is only provided for backwards compatibility with earlier runtimes,<ref>{{cite web|title=Java RMI Release Notes|url=http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/relnotes.html|publisher=Oracle|accessdate=9 May 2012}}</ref> or for programs that don't provide an explicit port number (or zero) when exporting remote objects, which is required for generated stubs to be possible, as described in the Javadoc for UnicastRemoteObject. See the comment in the constructor above.