关于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);
 }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值