RMI 服务接口 例子

import java.net.MalformedURLException;
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...
------------------------------------------------------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值