怎么样处理rmiregistry的问题?
问: 运行RMI应用时,可不可以不手工启动名字服务rmiregistry,而是从程序中启动之?
答: 可以. java.rmi包中提供了类java.rmi.registry.LocateRegistry,用于获取名字服务或创建名字服务.调用LocateRegistry.createRegistry(int port)方法可以在某一特定端口创建名字服务,从而用户无需再手工启动rmiregistry.此外,LocateRegistry.getRegistry(String host,int port)方法可用于获取名字服务.
以下是我亲自测试过的一个例子:
package test;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MyRMIRegistry {
public MyRMIRegistry() {
super();
// TODO Auto-generated constructor stub
}
public int RegistryRMI(int i)
{
System.out.println("开始获得监听服务.....,i="+i);
int j = i;
//Object obj = null;
try
{
Registry registry = LocateRegistry.getRegistry(j);
registry.list();
}
catch (RemoteException remoteexception)
{
System.out.println("获得监听的服务出错.......");
Registry registry1 = null;
do
{
if (registry1 != null)
break;
try
{
registry1 = LocateRegistry.createRegistry(j);
registry1.list();
System.out.println("服务创建成功......");
}
catch (RemoteException remoteexception1)
{
remoteexception1.printStackTrace();
//throw remoteexception1;
}
} while (true);
}
System.out.println("服务监听结束.......");
return j;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRMIRegistry myRMI=new MyRMIRegistry();
myRMI.RegistryRMI(1093);
}
}
本文地址:http://www.45fan.com/a/question/68301.html