📜  Java的.rmi.Naming类在Java中

📅  最后修改于: 2022-05-13 01:54:29.332000             🧑  作者: Mango

Java的.rmi.Naming类在Java中

Java.rmi.Naming类包含将名称与远程注册表中存在的远程对象绑定、解除绑定或重新绑定的方法。此类还用于获取远程注册表中存在的对象的引用或与此注册表关联的名称列表。

语法:类声明

public final class Naming extends Object

让我们来讨论一下这个类的一些主要方法,如下所示:

最常用的方法描述如下:

  1. 绑定()
  2. 列表()
  3. 抬头()
  4. 重新绑定()
  5. 解除绑定()

方法一: bind()



句法:

static void bind(String name, remote object) 
// Bind this name with this remote object

参数:远程注册表的名称

例外:

  1. AlreadyBoundException 在名称已被限制时发生
  2. MalformedURLException 在格式错误时发生 名称不是网址
  3. 当与远程注册表的联系失败时发生 RemoteException
  4. 不允许访问此操作时发生 AccessException

方法二: 列表() 用于获取 与注册表关联的名称列表

句法:

static string[] list(String name)

参数:远程注册表的名称

返回类型:与此注册表绑定的名称数组

例外:



  1. MalformedURLException 当名称的格式不是 URL 时发生
  2. 当远程注册表的联系失败时发生 RemoteException

方法 3: lookup()查找与此名称关联的远程对象的引用

句法:

static remote lookup(String name)

参数:远程注册表的名称

返回类型:远程对象的引用

例外:

  1. 当名称未与当前操作绑定时发生 NotBoundException
  2. 当与远程注册表的联系失败时发生 RemoteException
  3. 不允许访问此操作时发生 AccessException
  4. MalformedURLException 当名称的格式不是 URL 时发生

方法 4: rebind() 方法将此名称与关联的远程对象重新绑定

句法:

static void rebind(String name, remote object)

参数:它需要两个参数,即名称和对象。

  • 远程注册表的名称
  • 要与名称关联的远程对象

例外:

  1. MalformedURLException 当名称的格式不是 URL 时发生
  2. 当与远程注册表的联系失败时发生 RemoteException
  3. 不允许访问此操作时发生 AccessException

方法 5: unbind() 将这个名称与关联的远程对象解除绑定



句法:

static void unbind(String name) 

参数:远程注册表的名称

例外:

  1. 当名称未与当前操作绑定时发生 NotBoundException
  2. MalformedURLException 当名称的格式不是 URL 时发生
  3. 当与远程注册表的联系失败时发生 RemoteException
  4. 不允许访问此操作时发生 AccessException

示例 1:

Java
// Java Program to create a registry
// Server's Side
 
// Importing java.rmi package to enable object of one JVM
// to invoke methods on object in another JVM
import java.rmi.*;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
 
// Interface
// creating remote interface
interface demo extends Remote {
    public String msg(String msg) throws RemoteException;
}
 
// Class 1
// Helper Class
class demoRemote
    extends UnicastRemoteObject implements demo {
    demoRemote() throws RemoteException { super(); }
 
    // @Override
    public String msg(String msg) throws RemoteException
    {
        // Display message only
        System.out.println("GeeksForGeeks");
        return null;
    }
}
 
// Class 2
// Main class
public class GFG_Server {
 
    // Main driver method
    public static void main(String args[])
        throws RemoteException, NotBoundException,
               AlreadyBoundException
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating a new registry by creating
            // an object of Registry class
            Registry registry
                = LocateRegistry.createRegistry(3000);
 
            demo obj = new demoRemote();
 
            // binding obj to remote registry
            Naming.bind("rmi://localhost:3000"
                            + "/geeksforgeeks",
                        (Remote)obj);
 
            // Display message when program
            // is executed succussfully
            System.out.println(
                "registry created successfully");
        }
 
        // Catch block to handle the exceptions
        catch (Exception e) {
 
            // Getting the name of the exception using
            // the toString() method over exception object
            System.err.println(e.toString());
        }
    }
}


Java
// Java Program to look for the object
// Client Side
 
// Importing java.rmi package to enable object of one JVM
// to invoke methods on object in another JVM
import java.rmi.*;
 
// Main class
public class GFG_Client {
 
    // Main driver method
    public static void main(String args[])
    {
        // try block to check for exceptions
        try {
 
            // Looking for object in remote registry
            demo client = (demo)Naming.lookup(
                "rmi://localhost:3000/obj");
 
            // Print and display the client message
            System.out.println(client.msg());
        }
 
        // Catch block to handle the exception
        catch (Exception e) {
        }
    }
}


输出:

registry created successfully

实现:寻找对象的Java程序(客户端)

示例 2:

Java

// Java Program to look for the object
// Client Side
 
// Importing java.rmi package to enable object of one JVM
// to invoke methods on object in another JVM
import java.rmi.*;
 
// Main class
public class GFG_Client {
 
    // Main driver method
    public static void main(String args[])
    {
        // try block to check for exceptions
        try {
 
            // Looking for object in remote registry
            demo client = (demo)Naming.lookup(
                "rmi://localhost:3000/obj");
 
            // Print and display the client message
            System.out.println(client.msg());
        }
 
        // Catch block to handle the exception
        catch (Exception e) {
        }
    }
}

输出:

GeeksForGeeks