浏览 1753 次
锁定老帖子 主题:RMI 服务接口 例子
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-03-13
最后修改:2010-03-13
import java.rmi.AlreadyBoundException; import java.rmi.Naming; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.server.UnicastRemoteObject; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.HashMap; public class ServiceDeployer { public static java.util.List<java.util.Map<String, Object>> services; public static int DEFAULT_RMI_PORT = 10990; static { services = new ArrayList<Map<String, Object>>(); java.util.Map<String, Object> map = new java.util.HashMap<String, Object>(); map.put("NAME", "RemotePingTaskService"); map.put("CLASS_NAME", "RemotePingTaskServiceImpl"); //map.put("PORT", 11000); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10990); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService1"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10991); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService2"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10992); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService3"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); //map.put("PORT", 10993); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService4"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10994); //services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService5"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10995); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService6"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10996); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService7"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10997); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService8"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); //map.put("PORT", 10998); services.add(map); map = new java.util.HashMap<String, Object>(); map.put("NAME", "FPingTaskService9"); map.put("CLASS_NAME", "FPingTaskServiceImpl"); map.put("PORT", 10999); services.add(map); } public static void main(String[] args) throws Exception { System.out.println("Create registry for localhost on default port " + DEFAULT_RMI_PORT); try { java.rmi.registry.LocateRegistry.createRegistry(DEFAULT_RMI_PORT); } catch (java.rmi.server.ExportException e) { System.out.println("Registry for localhost on defalut port " + DEFAULT_RMI_PORT + " already exists"); } try { for (int i = 0, size = services.size(); i < size; i++) { Map<String, Object> service = services.get(i); String serviceName = (String) service.get("NAME"); String className = (String) service.get("CLASS_NAME"); Object o = service.get("PORT"); int port = -1; if (o != null) port = ((Integer) o).intValue(); Class clazz = Class.forName(className); Object object = clazz.newInstance(); if (object instanceof Remote) { if (port != -1) { //java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.getRegistry("localhost", port); //System.out.println("--->" + registry); //if (registry == null) { System.out.println("Create registry for localhost on " + port); try { java.rmi.registry.LocateRegistry.createRegistry(port); } catch (java.rmi.server.ExportException e) { System.out.println("Registry for localhost on " + port + " already exists"); } //} } Remote stub = (Remote) object; if (port == -1) UnicastRemoteObject.exportObject(stub); else UnicastRemoteObject.exportObject(stub, port); if (port == -1) Naming.bind("rmi://localhost:" + DEFAULT_RMI_PORT + "/" + serviceName, stub); else Naming.bind("rmi://localhost:" + port + "/" + serviceName, stub); } else { System.out.println("\"" + className + "\" is not a available RMI service"); } if (port == -1) System.out.println("RMI service '" + serviceName + "' on (default port) localhost:" + DEFAULT_RMI_PORT + " is binding..."); else System.out.println("RMI service '" + serviceName + "' on localhost:" + port + " is binding..."); System.out.println("------------------------------------------------------------------------------------------------"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (AlreadyBoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { } } } 运行结果: D:\home\admin\dev\classes>java ServiceDeployer Create registry for localhost on default port 10990 RMI service 'RemotePingTaskService' on (default port) localhost:10990 is binding... ------------------------------------------------------------------------------------------------ Create registry for localhost on 10990 Registry for localhost on 10990 already exists RMI service 'FPingTaskService' on localhost:10990 is binding... ------------------------------------------------------------------------------------------------ Create registry for localhost on 10991 RMI service 'FPingTaskService1' on localhost:10991 is binding... ------------------------------------------------------------------------------------------------ Create registry for localhost on 10992 RMI service 'FPingTaskService2' on localhost:10992 is binding... ------------------------------------------------------------------------------------------------ RMI service 'FPingTaskService3' on (default port) localhost:10990 is binding... ------------------------------------------------------------------------------------------------ Create registry for localhost on 10995 RMI service 'FPingTaskService5' on localhost:10995 is binding... ------------------------------------------------------------------------------------------------ Create registry for localhost on 10996 RMI service 'FPingTaskService6' on localhost:10996 is binding... ------------------------------------------------------------------------------------------------ Create registry for localhost on 10997 RMI service 'FPingTaskService7' on localhost:10997 is binding... ------------------------------------------------------------------------------------------------ RMI service 'FPingTaskService8' on (default port) localhost:10990 is binding... ------------------------------------------------------------------------------------------------ Create registry for localhost on 10999 RMI service 'FPingTaskService9' on localhost:10999 is binding... ------------------------------------------------------------------------------------------------ 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |