Professional Documents
Culture Documents
by Tung Tran
Introduction
RMI allows object on one system to call a method in an object somewhere else on the
network, and even pass objects in and out to that remote object. RMI is a java-specific
version of a CORBA framework. It is built on top of sockets. Sockets, however, only
allow data passing to the server.
With stub and skeleton classes, client can use all the methods
put_Year, CaculateEaster, get_Year, get_Month, get_Day from a
remote server just like they are right on client machine. This is
done by RMI and its object communication through sockets.
CLIENT
CalculateEasterClient.class
CalculateEasterInterface.class put
CalculateEasterServer_Stub.class _Y e
CalculateEasterServer_Skel.class C al c ar
java.policy u la t
e Ea SERVER
st er
get
get Y e_ CalculateEasterServer.class
_ Mo a r CalculateEasterInterface.class
get nth CalculateEasterServer_Stub.class
_D a CalculateEasterServer_Skel.class
y java.policy
Files needed
// server
CalculateEasterServer.java
With methods:
- put_Year
- get_Year
- get_Month
- get_Day
- CalculateEaster
// interface
CalculateEasterInterface.java
The client who is using methods from the server to get the Easter date given the year to
server.
// client
CalculateEasterClient.java
Create a file called java.policy. This file grants socket permission to the server. It
contains these lines:
grant
{
permission java.net.SocketPermission "*:1024-65535","connect,accept,resolve";
permission java.net.SocketPermission "*:1-1023","connect,resolve";
};
Make sure these files are in classpath if they are not in the same directory.
References
http://www.javacoffeebreak.com/articles/javarmi/javarmi.html
Just Java 2, Fourth Edition, Peter Van der Linden, The Sun Microsystems Press.
These are files that can be found in CalEaster.jar
// CalculateEasterClient.java
import java.rmi.*;
import java.rmi.Naming;
import java.io.*;
//
//
// CalculateEasterClient
//
//// References:
// - http://www.javacoffeebreak.com/articles/javarmi/javarmi.html
// - Beginning ATL3COM Programming from Wrox publisher
//
public class CalculateEasterClient
{
public static void main(String args[]) throws Exception
{
// Check for hostname argument
if (args.length != 1)
{
System.out.println ("Syntax - CalculateEasterClient host");
System.exit(1);
}
for (;;)
{
System.out.println ("1 - Calculate Easter");
System.out.println ("2 - Exit"); System.out.println();
System.out.print ("Choice : ");
switch (value)
{
case 1:
System.out.print ("Enter Year : ");
line = din.readLine();System.out.println();
choice = new Integer (line);
calculateEasterInterface.put_Year(choice.intValue());
calculateEasterInterface.CalculateEaster();
// Call remote method
System.out.println ("Easter : " + calculateEasterInterface.get_Month() +
"/" + calculateEasterInterface.get_Day() + " in " +
calculateEasterInterface.get_Year());
break;
case 2:
System.exit(0);
default :
System.out.println ("Invalid option");
break;
}
}
}
import java.math.*;
import java.rmi.*;
import java.rmi.server.*;
import java.lang.Number;
//
//
// CalculateEasterServer.java
//
// Server for a RMI service that calculates Easter date
// References:
// - http://www.javacoffeebreak.com/articles/javarmi/javarmi.html
// - Beginning ATL3COM Programming from Wrox publisher
//
public class CalculateEasterServer extends UnicastRemoteObject implements
CalculateEasterInterface
{
private static int m_Year;
private static int m_Month;
private static int m_Day;
temp %= 30;
m_Day = ta + te;
//
// References:
// - http://www.javacoffeebreak.com/articles/javarmi/javarmi.html
// - Beginning ATL3COM Programming from Wrox publisher
//
// CalculateEasterInterface.java
//
import java.rmi.*;
public interface CalculateEasterInterface extends java.rmi.Remote
{
// put_Year method to input the year you want to know the date of Easter
public void put_Year ( int year ) throws RemoteException;
// Easter year
public int get_Year ( ) throws RemoteException;
// Easter month
public int get_Month ( ) throws RemoteException;
// Easter day
public int get_Day ( ) throws RemoteException;
java.policy
grant
{
permission java.net.SocketPermission "*:1024-65535",
"connect,accept,resolve";
permission java.net.SocketPermission "*:1-1023",
"connect,resolve";
};