RMI Notes
RMI Notes
RMI Notes
with RMI
• Four major steps
– Define remote interface
• Describes client/server communication
– Define server application to implement remote interface
• Same name as remote interface, ends with Impl
– Define client application that uses remote interface reference
• Interacts with server implementation
– Compile and execute server and client
Defining the Remote Interface
• First step
– Define remote interface that describes remote methods
• Client calls remote methods, server implements them
• To create a remote interface
– Define interface that extends interface Remote
(java.rmi)
• Tagging interface - no methods to define
• An object of a class that implements interface Remote directly
or indirectly is a remote object and can be accesses from any
JVM.
– Each method in Remote interface must throw
RemoteException
• Potential network errors
Defining the Remote Interface
• Interface TemperatureServer
– Extends Remote
– Describes method getWeatherInfo
1 // Fig. 20.1: TemperatureServer.java
2 // TemperatureServer interface definition
3 import java.rmi.*; Interface Remote in java.rmi
4
5 public interface TemperatureServer extends Remote { 1. import
6 public WeatherInfo[] getWeatherInfo()
7 throws RemoteException; 1.1 extends Remote
8 }
2. getWeatherInfo
Methods in Remote interface (is a relationship)
must be able to throw a RemoteException. 2.1 throws
RemoteException
Implementing the Remote Interface
• Define TemperatureServerImpl
– Implements Remote interface TemperatureServer
– Client interacts with TemperatureServerImpl object
– Uses array of WeatherInfo objects to store data
• Copy sent to client when calls getWeatherInfo
Implementing the Remote Interface
18 public class TemperatureServerImpl extends UnicastRemoteObject
19 implements TemperatureServer {
– UnicastRemoteObject
• Provides functionality for remote objects
• Constructor exports object so it can receive remote calls
– Wait for client on anonymous port number
22 public TemperatureServerImpl() throws RemoteException
– URL object
• Contains URL for Traveler's Forecast web page
• Throws MalformedURLException
Implementing the Remote Interface
40 BufferedReader in =
41 new BufferedReader(
42 new InputStreamReader( url.openStream() ) );
51 String s1 =
52 "CITY WEA HI/LO WEA HI/LO";
– WeatherInfo objects
• City name, temperature, description of weather
– Method substring to extract data from line
• Store all WeatherInfo objects in a Vector
84 weatherInformation[ i ] =
85 ( WeatherInfo ) cityVector.elementAt( i );
– Close connection
Implementing the Remote Interface
116 String serverObjectName = "//localhost/TempServer";
• Next step
– Client code to get weather info from
TemperatureServerImpl
– Calls getWeatherInfo through RMI
– Graphically display weather info
• Class WeatherItem (extends JLabel) stores info about
each city
• Display name, High/low, and image (depending on conditions)
Define the client
22 private void getRemoteTemp( String ip )
26 String serverObjectName = "//" + ip + "/TempServer";
– main
• Passes command line argument (ip) to constructor
• localhost default
Define the client
• Class WeatherItem
– extends JLabel
– static initializer block
• For complex initialization of static variables
• backgroundImage - ImageIcon, has background
• weatherImages - ImageIcon array, holds weather
images
18 static {
19 backgroundImage = new ImageIcon( "images/back.jpg" );
20 weatherImages =
21 new ImageIcon[ weatherImageNames.length ];
22
23 for ( int i = 0; i < weatherImageNames.length; ++i )
24 weatherImages[ i ] = new ImageIcon(
25 "images/" + weatherImageNames[ i ] + ".jpg" );
26 }
Define the client