Flight.java
public interface Flight extends Remote { // 读取航班号 public String getFlightNumber() throws RemoteException; // 读取始发站 public String getOrigin() throws RemoteException; // 读取终点站 public String getDestination() throws RemoteException; // 读取计划出发时间 public String getSkdDeparture() throws RemoteException; // 读取计划到达时间 public String getSkdArrival() throws RemoteException; public void setOrigin(String origin) throws RemoteException; public void setDestination(String destination) throws RemoteException; public void setSkdDeparture(String skdDeparture) throws RemoteException; public void setSkdArrival(String skdArrival) throws RemoteException; }
FlightFactory.java
import java.rmi.*; public interface FlightFactory extends Remote{ public Flight getFlight(String flightNumber)throws RemoteException; }
FlightFactoryImpl.java
import java.rmi.*; import java.rmi.server.*; import java.util.*; public class FlightFactoryImpl extends UnicastRemoteObject implements FlightFactory{ protected Hashtable<String,Flight> flights; //存放Flight对象的缓存 public FlightFactoryImpl()throws RemoteException{ flights = new Hashtable<String,Flight>(); } public Flight getFlight(String flightNumber)throws RemoteException{ Flight flight = flights.get(flightNumber); if (flight != null) return flight; flight = new FlightImpl(flightNumber, null, null, null, null); flights.put(flightNumber, flight); return flight; } }
FlightImpl.java
import java.rmi.*; import java.rmi.server.*; public class FlightImpl extends UnicastRemoteObject implements Flight{ protected String flightNumber; protected String origin; protected String destination; protected String skdDeparture; protected String skdArrival; public FlightImpl(String aFlightNumber, String anOrigin, String aDestination, String aSkdDeparture, String aSkdArrival) throws RemoteException{ flightNumber = aFlightNumber; origin = anOrigin; destination = aDestination; skdDeparture = aSkdDeparture; skdArrival = aSkdArrival; } public String getFlightNumber()throws RemoteException{ System.out.println("调用getFilghtNumber(),返回"+flightNumber); return flightNumber; } public String getOrigin()throws RemoteException{ return origin; } public String getDestination()throws RemoteException{ return destination; } public String getSkdDeparture()throws RemoteException{ return skdDeparture; } public String getSkdArrival()throws RemoteException{ return skdArrival; } public void setOrigin(String origin)throws RemoteException{ this.origin=origin; } public void setDestination(String destination)throws RemoteException{ this.destination=destination; } public void setSkdDeparture(String skdDeparture)throws RemoteException{ this.skdDeparture=skdDeparture; } public void setSkdArrival(String skdArrival)throws RemoteException{ this.skdArrival=skdArrival; } }
SimpleServer.java
import java.rmi.*; import java.rmi.registry.LocateRegistry; import javax.naming.*; public class SimpleServer { public static void main(String args[]) { try { LocateRegistry.createRegistry(8899); FlightFactory factory = new FlightFactoryImpl(); Context namingContext = new InitialContext(); namingContext.rebind("rmi://localhost:8899/FlightFactory", factory); System.out.println("服务器注册了一个FlightFactory对象"); } catch (Exception e) { e.printStackTrace(); } } }
SimpleClient.java
import java.rmi.*; import javax.naming.*; public class SimpleClient { public static void main(String[] args) { String url = "rmi://localhost:8899/"; try { Context namingContext = new InitialContext(); FlightFactory factory = (FlightFactory) namingContext.lookup(url + "FlightFactory"); Flight flight1 = factory.getFlight("795"); flight1.setOrigin("Shanghai"); flight1.setDestination("Beijing"); System.out.println("Flight " + flight1.getFlightNumber() + ":"); System.out.println("From " + flight1.getOrigin() + " to " + flight1.getDestination()); Flight flight2 = factory.getFlight("795"); System.out.println("Flight " + flight2.getFlightNumber() + ":"); System.out.println("From " + flight2.getOrigin() + " to " + flight2.getDestination()); System.out.println("flight1是" + flight1.getClass().getName() + "的实例"); System.out.println("flight2是" + flight2.getClass().getName() + "的实例"); System.out.println("flight1==flight2:" + (flight1 == flight2)); System.out.println("flight1.equals(flight2):" + (flight1.equals(flight2))); } catch (Exception e) { e.printStackTrace(); } } }
当SimpleClient第一次在调用factory.getFlight("795");方法的时候,在服务器端还不存在相应的FlightImpl对象,因此会先创建一个对象,当第二次调用factory.getFlight("795");时,就不会再次创建对象了,会直接使用第一次创建的对象。
总结:
1、客户端每次访问同一个远程对象时,都会得到一个新的存根对象,尽管他们为同一个远程对象提供代理
2、存根类覆盖了Object类的equals()方法,存根类中的equals()方法的比较原则为:如果两个存根对象为同一个远程提供的代理,就返回true,否则返回false
爱美丽女性网 www.5aml.net
爱美丽女性网 美容 彩妆 发型 护肤 服饰 情感|分享生活美学,专注女性时尚