- Registry 绑定方式
- UserManagerImpl userManager=new UserManagerImpl();
- UserManagerInterface userManagerI=(UserManagerInterface)UnicastRemoteObject.exportObject(userManager,0); //固定端口设置
- // Bind the remote object's stub in the registry
- Registry registry = LocateRegistry.createRegistry(2001);
- registry.rebind("userManager", userManagerI);
- System.out.println("server is ready");
获取对象
Registry reg = LocateRegistry.getRegistry("localhost");
stub = (Payment) reg.lookup("Mortgage");Naming 绑定对象
public class Server extends java.rmi.server.UnicastRemoteObject implements aRemoteInterface{
public Server(int port) { super(port); } .... Naming.bind(uniqueName, this); ....}Naming获取对象Naming.lookup()
固定端口这只
- publicclass SMRMISocket extends RMISocketFactory {
- public Socket createSocket(String host, int port) throws IOException{
- returnnew Socket(host,port);
- }
- public ServerSocket createServerSocket(int port) throws IOException {
- if (port == 0)
- port = 10990; //不指定就随机分配了
- returnnew ServerSocket(port);
- }
- }
在实例化UnicastRemoteObject的子类前加入一下代码:
- try {
- RMISocketFactory.setSocketFactory(new SMRMISocket());
- } catch (Exception ex) {
- }