I am learning about RMI for the Development project. I have written the following programs:
//Interface
import java.rmi.*;
public interface WeatherIntf extends Remote {
public
String getWeather() throws RemoteException;
}
//Dummy Class
class Dummy{
public String getWeather(){
return Math.random()>0.5? "sunny" : "rainy";
}
}
//Server Class
import java.rmi.server.*;
import java.rmi.*;
public class Server extends UnicastRemoteObject
implements WeatherIntf {
private Dummy d;
public Server () throws RemoteException {
super();
d = new Dummy();
}
public String getWeather() throws RemoteException {
return d.getWeather();
}
public static void main(String[] args) {
try {
Server myWeatherServer = new Server();
Naming.rebind("//timothycooper/TestServer",myWeatherServer);
System.out.println("Server Ready");
}catch (Exception e) {
System.out.println(e.getMessage() );
}
}
}
//Client Class
import java.rmi.*;
public class Client {
public static void main(String[] args) {
try {
Remote robj = Naming.lookup("//timothycooper/TestServer");
WeatherIntf weatherserver = (WeatherIntf) robj;
String forecast = weatherserver.getWeather();
System.out.println("The weather will be " + forecast );
} catch (Exception e) {System.out.println(e.getMessage());}
}
}
It works with no problem on the network.
Question:
I think that some people will say that the Dummy instance found in the Server class’s constructor probably will not be remoteable and that the Dummy class should implements the WeatherIntf interface.
I ask this question because there seem to be a common approach that in the
Java Development FlightNight project that the class Data should implements DataInterface but it look as if the Data class need not implements the DataInterface?
Can anybody shed some light on it?