Java Reference
In-Depth Information
public String getGreeting() throws RemoteException
{
return ("Hello there!");
}
}
3. Create the server process.
The server creates object(s) of the above implementation class and registers them
with a naming service called the registry . It does this by using static method rebind
of class Naming (from package java.rmi ). This method takes two arguments:
￿
a String that holds the name of the remote object as a URL with protocol rmi ;
￿
a reference to the remote object (as an argument of type Remote ).
The method establishes a connection between the object's name and its refer-
ence. Clients will then be able to use the remote object's name to retrieve a reference
to that object via the registry.
The URL string, as well as specifying a protocol of rmi and a name for the
object, specifi es the name of the remote object's host machine. For simplicity's
sake, we shall use localhost (which is what RMI assumes by default anyway). The
default port for RMI is 1099, though we can change this to any other convenient port
if we wish. The code for our server process is shown below and contains just one
method: main . To cater for the various types of exception that may be generated,
this method declares that it throws Exception .
import java.rmi.*;
public class HelloServer
{
private static fi nal String HOST = "localhost";
public static void main(String[] args)
throws Exception
{
//Create a reference to an
//implementation object…
HelloImpl temp = new HelloImpl();
//Create the string URL holding the
//object's name…
String rmiObjectName = "rmi://" + HOST + "/Hello";
//(Could omit host name here, since 'localhost'
//would be assumed by default.)
//'Bind' the object reference to the name…
Naming.rebind(rmiObjectName,temp);
//Display a message so that we know the process
//has been completed…
Search WWH ::




Custom Search