RMI: Ensuring that classes/stubs are NOT dynamically downloaded.
This is for my SCJD exam, which states, "You must provide all classes pre-installed so that no dynamic class downloading occurs."
Due to my lack of experience with RMI, I decided to write a small test program to get comfortable with the technology. I would like to confirm that my RMI example does not use any "dynamic class downloading."
My example program consists of three classes: DateService (an interface that extends Remote); DateServiceImpl (an implementation of DateService, which contains the code to create an RMI registry, export a DateService implementation instance, and bind it to the registry); and DateClient, which looks for the RMI registry, finds the DateService object, and calls one of its methods.
I have used rmic to generate a DateServiceImpl_Stub.class file. I have left this file in the same directory as DateServiceImpl.class.
Does it sound as if I have got this correct? I will post my code in the next post.
Thanks in advance.
[993 byte] By [
RATiXa] at [2007-11-27 8:13:42]

DateService.javapackage date;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface DateService extends Remote {
/**
* Returns an array containing data pertaining to the current date.
* The first index of the array contains the year, the second has
* the month, and the third contains the day.
*
* @return the current date in an array, separated into year, month, and day
* @throws RemoteException if the remote invocation fails
*/
public String[] currentDate() throws RemoteException;
}
DateServiceImpl.javapackage server;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Calendar;
import date.DateService;
public class DateServiceImpl implements DateService {
/**
* Returns an array containing data pertaining to the current date.
* The first index of the array contains the year, the second has
* the month, and the third contains the day.
*
* @return the current date in an array, separated into year, month, and day
* @throws RemoteException if the remote invocation fails
*/
public String[] currentDate() throws RemoteException {
String[] ymd = new String[3];
Calendar today = Calendar.getInstance();
ymd[0] = Integer.toString(today.get(Calendar.YEAR));
ymd[1] = Integer.toString(today.get(Calendar.MONTH) + 1);
ymd[2] = Integer.toString(today.get(Calendar.DATE));
for (int i = 0; i < ymd.length; i++) {
if (ymd[i].length() < 2) {
ymd[i] = "0" + ymd[i];
}
}
return ymd;
}
public static void main(String[] args) {
try {
String name = "DateService";
int rmiPort = 1099;
DateService service = new DateServiceImpl();
System.out.println("DateServiceImpl instance created.");
Registry registry = LocateRegistry.createRegistry(rmiPort);
System.out.println("RMI registry created on port " + rmiPort);
DateService stub =
(DateService)UnicastRemoteObject.exportObject(service, 0);
System.out.println(
"Service object exported and able to receive method invocation calls.");
registry.rebind(name, stub);
System.out.println("DateServiceImpl bound to the name \"" + name + "\"");
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
System.err.println("DateServiceImpl exception:");
e.printStackTrace();
}
}
}
DateClient.javapackage client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import date.DateService;
public class DateClient {
public static void main(String args[]) {
try {
String name = "DateService";
Registry registry = LocateRegistry.getRegistry();
DateService service = (DateService)registry.lookup(name);
String[] today = service.currentDate();
for (int i = 0; i < today.length; i++) {
System.out.print((i == 0 ? "Today's date is " : "/") + today[i]);
}
System.out.println();
} catch (Exception e) {
System.err.println("DateClient exception:");
e.printStackTrace();
}
}
}
RATiXa at 2007-7-12 19:58:09 >
