rmi编写java分布式系统时,采用工厂模式的使用示例,参考孙卫琴的《java网络编程精解》,比较简单,仅供日后备忘。
类图:
远程接口
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IFlight extends Remote{
public String getFlightNumber() throws RemoteException;
public String getOrigin() throws RemoteException;
public void setOrigin(String origin) throws RemoteException;
public String getDestination() throws RemoteException;
public void setDestination(String destination) throws RemoteException;
public void setFlightNumber(String flightNumber) throws RemoteException;
}
远程对象:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import org.gogo.protocol.rmi.IFlight;
public class FlightImpl extends UnicastRemoteObject implements IFlight{
protected String flightNumber;
protected String origin;
protected String destination;
public FlightImpl(String flightNumber, String origin, String destination) throws RemoteException{
this.flightNumber = flightNumber;
this.origin = origin;
this.destination = destination;
}
public String getFlightNumber() throws RemoteException {
System.out.println("~~~~ 调用 getFlightNumber, 返回 "+ flightNumber);
return this.flightNumber;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
}
远程接口(工厂):
import java.rmi.Remote;
import java.rmi.RemoteException;
import org.gogo.protocol.rmi.IFlight;
public interface IFlightFactory extends Remote {
public IFlight getFlight(String flightNumber) throws RemoteException;
}
远程对象(工厂):
public class FlightFactoryImpl extends UnicastRemoteObject implements IFlightFactory{
protected Hashtable<String, IFlight> flights;
public FlightFactoryImpl() throws RemoteException {
flights = new Hashtable<String, IFlight>();
}
public IFlight getFlight(String flightNumber) throws RemoteException {
IFlight flight = flights.get(flightNumber);
if(flight == null)
flight = new FlightImpl(flightNumber, null, null);
flights.put(flightNumber, flight);
return flight;
}
}
rmi server:
import java.rmi.registry.LocateRegistry;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.gogo.protocol.rmi.factory.FlightFactoryImpl;
import org.gogo.protocol.rmi.factory.IFlightFactory;
public class SimpleServer {
public static void main(String[] args) throws Exception {
IFlightFactory factory = new FlightFactoryImpl();
LocateRegistry.createRegistry(1099);
Context ctx = new InitialContext();
ctx.rebind("rmi://localhost:1099/FlightFactory", factory);
System.out.println("~~~~ 服务器注册了一个FlightFactory对象");
}
}
rmi client:
import javax.naming.Context;
import javax.naming.InitialContext;
import org.gogo.protocol.rmi.IFlight;
import org.gogo.protocol.rmi.factory.IFlightFactory;
public class SimpleClient {
public static void main(String[] args){
String url = "rmi://localhost:1099/";
try{
Context ctx = new InitialContext();
Object obj = ctx.lookup(url +"FlightFactory");
if(obj == null)
return;
IFlightFactory remoteObjFactory = (IFlightFactory)obj;
if(remoteObjFactory == null)
return;
IFlight flight1 = remoteObjFactory.getFlight("795");
flight1.setOrigin("beijing");
flight1.setDestination("huhuhaote");
IFlight flight2 = remoteObjFactory.getFlight("795");
System.out.println("~~~~ flightNumber : "+ flight2.getFlightNumber());
System.out.println("~~~~ from ["+ flight2.getOrigin() +"[ to ["+ flight2.getDestination() +"]");
System.out.println("~~~~ flight1是 ["+ flight1.getClass() +"]的实例");
System.out.println("~~~~ flight2是 ["+ flight2.getClass() +"]的实例");
System.out.println("~~~~ flight1==flight2 : "+ (flight1==flight2));
System.out.println("~~~~ flight1.equals(flight2) : "+ (flight1.equals(flight2)));
}catch(Exception e){
e.printStackTrace();
}
}
}