public Peer() { try { share = new PeerImpl(); // check usable port and register the port for (int i = 0; i < PeerInfo.rmi.portList.length; i++) { if (checkPort(i)) { // register PeerInfo.rmi.port = PeerInfo.rmi.portList[i]; PeerInfo.rmi.fileSharing = "rmi://localhost:" + PeerInfo.rmi.port + "/share"; LocateRegistry.createRegistry(PeerInfo.rmi.port); break; } if (i == 2) { System.out.println("All ports are occupied!"); System.exit(-1); } } // bind Naming.bind(PeerInfo.rmi.fileSharing, share); System.out.println("File sharing service start!"); } catch (RemoteException e) { e.printStackTrace(); System.out.println("Fail to create remote object!"); } catch (MalformedURLException e) { e.printStackTrace(); System.out.println("URL error!"); } catch (AlreadyBoundException e) { e.printStackTrace(); System.out.println("Service already bound!"); } }
private static void startServerMode() { System.out.println("start server mode - initializing rmi..."); try { localServer.initRMI(); activeServerIndex = ClientConnection.getServerIndex(localServer.getServerName()); System.out.println("Server[" + activeServerIndex + "] is listening..."); } catch (RemoteException ex) { System.out.println("RMI creation remote exception occured"); System.out.println(ex.getMessage()); } catch (AlreadyBoundException ex) { System.out.println("Already Bound"); System.out.println(ex.getMessage()); } }
public static void main(String[] args) { try { // 创建一个远程对象 IHello rhello = new HelloImpl(); // 生成远程对象注册表Registry的实例,并指定端口为8888(默认端口是1099) LocateRegistry.createRegistry(6789); // 把远程对象注册到RMI注册服务器上,并命名为RHello // 绑定的URL标准格式为:rmi://host:port/name(协议名可以省略) Naming.bind("rmi://localhost:6789/RHello", rhello); System.out.println(">>INFO:远程IHello对象绑定成功!"); } catch (RemoteException e) { System.out.println("创建远程对象发生异常!"); e.printStackTrace(); } catch (MalformedURLException e) { System.out.println("发生重复绑定对象异常!"); e.printStackTrace(); } catch (AlreadyBoundException e) { System.out.println("发生URL畸形异常!"); e.printStackTrace(); } }
public static void main(String[] args) { try { // Creation d'un "annuaire" des services // remote qu'on va fournir LocateRegistry.createRegistry(1099); if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } // J'instancie mon fournisseur de service meteo MeteoServiceImpl meteoserv = new MeteoServiceImpl(); // Je construis l'url de mon service String adresse = "rmi://" + InetAddress.getLocalHost().getHostAddress() + "/meteoRMI"; System.out.println("url = " + adresse); Naming.bind(adresse, meteoserv); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AlreadyBoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Initialize this service exporter, registering the service as RMI object. * * <p>Creates an RMI registry on the specified port if none exists. * * @throws RemoteException if service registration failed */ public void prepare() throws RemoteException { checkService(); if (this.serviceName == null) { throw new IllegalArgumentException("Property 'serviceName' is required"); } // Check socket factories for exported object. if (this.clientSocketFactory instanceof RMIServerSocketFactory) { this.serverSocketFactory = (RMIServerSocketFactory) this.clientSocketFactory; } if ((this.clientSocketFactory != null && this.serverSocketFactory == null) || (this.clientSocketFactory == null && this.serverSocketFactory != null)) { throw new IllegalArgumentException( "Both RMIClientSocketFactory and RMIServerSocketFactory or none required"); } // Check socket factories for RMI registry. if (this.registryClientSocketFactory instanceof RMIServerSocketFactory) { this.registryServerSocketFactory = (RMIServerSocketFactory) this.registryClientSocketFactory; } if (this.registryClientSocketFactory == null && this.registryServerSocketFactory != null) { throw new IllegalArgumentException( "RMIServerSocketFactory without RMIClientSocketFactory for registry not supported"); } // Determine RMI registry to use. if (this.registry == null) { this.registry = getRegistry( this.registryHost, this.registryPort, this.registryClientSocketFactory, this.registryServerSocketFactory); } // Initialize and cache exported object. this.exportedObject = getObjectToExport(); if (logger.isInfoEnabled()) { logger.info("Binding service '" + this.serviceName + "' to RMI registry: " + this.registry); } // Export RMI object. if (this.clientSocketFactory != null) { UnicastRemoteObject.exportObject( this.exportedObject, this.servicePort, this.clientSocketFactory, this.serverSocketFactory); } else { UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort); } // Bind RMI object to registry. try { if (this.replaceExistingBinding) { this.registry.rebind(this.serviceName, this.exportedObject); } else { this.registry.bind(this.serviceName, this.exportedObject); } } catch (AlreadyBoundException ex) { // Already an RMI object bound for the specified service name... unexportObjectSilently(); throw new IllegalStateException( "Already an RMI object bound for name '" + this.serviceName + "': " + ex.toString()); } catch (RemoteException ex) { // Registry binding failed: let's unexport the RMI object as well. unexportObjectSilently(); throw ex; } }