• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RMI Interface

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Data class does not need to implement the DataInterface if you chose not to. In my submission I did not have the Data class implement that interface.
Now by implementing that interface, you can now use the Data class in any class or method that expects a class of DataInterface type. Whereas without implementing the interface, that would not be acceptible.
Pros and Cons are up to you, and others can give you some if they want.
The choice is yours. I prefer if it implemented it, just as a nice design.
Mark
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic