Java 2 Enterprise Edition£¨J2EE£©Ô¶³Ì·½·¨µ÷Óã¨Remote Method Invocation£¬RMI£©¿ò¼ÜÔÊÐíÄã´´½¨Í¸Ã÷µÄ¡¢·Ö²¼Ê½µÄ·þÎñºÍÓ¦ÓóÌÐò¡£»ùÓÚRMIµÄÓ¦ÓóÌÐòÓÉJava¶ÔÏ󹹳ɣ¬ÕâЩ¶ÔÏóÏ໥µ÷Óã¬Í¬Ê±ºöÂÔ¶Ô·½µÄλÖ᣻»ÑÔÖ®£¬Ò»¸öJava¶ÔÏó¿Éµ÷ÓÃÁíÒ»¸öÐéÄâ»úÉϵÄij¸öJava¶ÔÏóµÄ·½·¨£¬Õû¸ö¹ý³ÌºÍµ÷ÓÃͬһ¸öÐéÄâ»úÉϵÄij¸öJava¶ÔÏóµÄ·½·¨ÎÞÒ졣פÁôÔÚ²»Í¬ÐéÄâ»úÉϵĶÔÏóΪÁËÏ໥»ñµÃÒýÓ㬿ÉÒÔʹÓÃRMIµÄ²éÕÒ·þÎñ£¬»òÕß½«¶ÔÏóÒýÓÃ×÷Ϊ·½·¨µ÷ÓõÄÒ»¸ö²ÎÊý»òÕß·µ»ØÖµÀ´½ÓÊÕ¡£²ÎÊýºÍ·µ»ØÖµ½èÖúJavaµÄ¶ÔÏóÐòÁл¯»úÖÆÓÉRMIÀ´½øÐзâËÍ¡£
Ô¶³Ì¶ÔÏóºÍ½Ó¿Ú
JavaÌṩÁËÒ»¸öÍêÈ«ÏÞ¶¨Ãû³ÆΪjava.rmi.RemoteµÄ½Ó¿Ú¡£ÈκζÔÏóÒªÏë²ÎÓëºÍÁíÒ»¸öJava¶ÔÏóµÄÔ¶³Ì»á»°£¬¾Í±ØÐëÖ±½Ó»ò¼ä½ÓµØʵÏָýӿڡ£ÓÈÆäҪעÒâµÄÊÇ£¬ÈκÎÓÉjava.rmi.Remote½Ó¿ÚÀ´±êʶµÄ¶ÔÏ󶼰µÊ¾×ÅËüµÄ·½·¨¿É´ÓÆäËûÈκÎÐéÄâ»ú½øÐе÷Óá£ÊµÏÖÁË java.rmi.Remote½Ó¿ÚµÄ¶ÔÏóͨ³£³ÆΪ¡°Ô¶³Ì¶ÔÏó¡±£¬±ØÐë²ÉÓÃÒÔÏ·½Ê½À´ÉùÃ÷ËüµÄ·½·¨£º
? ÿ¸öÖ§³ÖÔ¶³Ìµ÷Óõķ½·¨¶¼±ØÐëÔÚÆäthrows×Ó¾äÖÐÉùÃ÷java.rmi.RemoteException¡£
? ¶ÔÓÚÒ»¸ö¿ÉÔ¶³Ìµ÷Óõķ½·¨£¬ËüµÄÿ¸ö·Ç»ù±¾£¨nonprimitive£©²ÎÊý»òÕß·µ»ØÖµ¶¼±ØÐëÖ±½Ó»ò¼ä½ÓµØÉùÃ÷ΪʵÏÖÁËjava.io.Serializable½Ó¿Ú¡£
³ýÁËʵÏÖjava.rmi.Remote½Ó¿ÚºÍÕýÈ·ÉùÃ÷ÈκÎÔ¶³Ì·½·¨Ö®Í⣬Զ³Ì¶ÔÏó±ØÐëÌṩһ¸öÎÞ²ÎÊýµÄ¹¹Ô캯Êý£¬ËüÄÜÒý·¢Ò»¸öjava.rmi.RemoteExceptionÒì³£¡£Õâ¾Í±£Ö¤Á˶ÔÏó¿É»ùÓÚÒ»ÖÖÐòÁл¯×´Ì¬À´Ô¶³Ì¹¹Ôì¡£
Ô¶³Ì¶ÔÏó±ØÐëµ¼³ö£¬ÒÔ½ÓÊÕ´«ÈëµÄÔ¶³Ì·½·¨µ÷Óá£Îª´Ë£¬Äãͨ³£ÐèÒªÀ©Õ¹java.rmi.server.UnicastRemoteObject»òÕßjava.rmi.activation.Activatable¡£Í¨¹ý¶ÔÆäÖÐÈκÎÒ»¸öÀà½øÐÐÀ©Õ¹£¬Ô¶³Ì¶ÔÏó¾Í¿ÉÔÚ´´½¨Ê±×Ô¶¯µ¼³ö¡£
ÒÔϽӿڶ¨ÒåչʾÁËjava.rmi.Remote½Ó¿Ú×îµäÐ͵ÄÓ÷¨£º
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface TimeKeeper extends Remote
{
public String currentDate() throws RemoteException;
public String currentTime() throws RemoteException;
}
ÓÉÓÚStringÀàÉùÃ÷ΪʵÏÖÁËjava.io.Serializable½Ó¿Ú£¬ËùÒÔStringÊÇÔ¶³Ì·½·¨µÄÓÐЧ·µ»ØÀàÐÍ¡£
ÒÔÏ´úÂëչʾÁËÈçºÎʵÏÖTimeKeeper½Ó¿Ú£¬ÒԱ㶨ÒåÒ»¸öÓÐЧµÄÔ¶³Ì¶ÔÏó£º
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TimeKeeperImpl implements TimeKeeper
{
public TimeKeeperImpl()
throws RemoteException
{
}
public String currentDate() throws RemoteException
{
Calendar cal = new GregorianCalendar();
String retVal = (cal.get(Calendar.MONTH) + "/" +
cal.get(Calendar.DAY_OF_MONTH) + "/" +
cal.get(Calendar.YEAR));
return retVal;
}
public String currentTime() throws RemoteException
{
Calendar cal = new GregorianCalendar();
String retVal = (cal.get(Calendar.HOUR_OF_DAY) + ":" +
cal.get(Calendar.MINUTE) + ":" +
cal.get(Calendar.SECOND));
return retVal;
}
}