Simple RMI client

In order to access the services offered by the server SoilRMI, an RMI

client needs to be developed. Figure 11-17 shows the implementation in Java of an RMI client.

1 import java.rmi.Naming;

2 import java.rmi.RemoteException;

3 public class RMIClient {

5 //contact remote soil

9 String registry = new String(url);

11 SoilRMIInterface soil = new SoilRMIServer();

12 soil = (SoilRMIInterface)Naming.lookup(registry);

13 double soilDepth = soil.getSoilDepth();

14 System.out.println("RMI Soil Server is available and running");

16 catch(Exception ex) {

17 System.out.println("RMI Soil Server is not available!");

Figure 11-17. Implementation of an RMI client.

Lines 1 and 2 show the Java standard the libraries needed to support the functionalities defined in the class RMIClient defined in line 4. Lines 7 through 10 collect information about the server: Its port number and IP (Internet Protocol) address, its stringified reference, and registry. In the case that the IP address and/or the port number are not the ones used by the server, the communication with the server will not be established. Lines 12 through 17 establish contact with the server object to obtain from it the value of soil depth. In the case that the communication with the server is not established, an error message is displayed. Note that the syntax for a remote method is the same as for a local method.

0 0

Post a comment