當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java java.rmi.Naming用法及代碼示例


Java.rmi.Naming 類包含用於將名稱與遠程注冊表中存在的遠程對象綁定、取消綁定或重新綁定的方法。此類還用於獲取遠程注冊表中存在的對象的引用或與此注冊表關聯的名稱列表。

用法:類聲明

public final class Naming extends Object

讓我們討論一下這個類的一些主要方法,如下:

最常用的方法如下所述:

  1. bind()
  2. list()
  3. lookup()
  4. rebind()
  5. unbind()

方法一:bind()

用法:

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

Parameters: 遠程注冊表名稱

Exceptions:

  1. AlreadyBoundException 在名稱已被綁定時發生
  2. URL Class當格式為 名稱不是 URL
  3. 當與遠程注冊表的聯係失敗時發生RemoteException
  4. 當不允許訪問此操作時發生AccessException

方法二: list() 用於獲取 與注冊表關聯的名稱列表

用法:

static string[] list(String name)

Parameters: 遠程注冊表的名稱

Return Type: 與此注冊表綁定的名稱數組

異常:

  1. 當名稱格式不是 URL 時,會出現MalformedURLException
  2. 當與遠程注冊表的聯係失敗時,會發生RemoteException

方法三: lookup()查找與該名稱關聯的遠程對象的引用

用法:

static remote lookup(String name)

Parameters: 遠程注冊表的名稱

Return Type: 遠程對象的引用

例外情況:

  1. NotBoundException 當名稱未與當前操作綁定時出現
  2. 當與遠程注冊表的聯係失敗時發生RemoteException
  3. 當不允許訪問此操作時發生AccessException
  4. 當名稱格式不是 URL 時出現MalformedURLException

方法4:rebind()方法將此名稱與關聯的遠程對象重新綁定

用法:

static void rebind(String name, remote object)

Parameters: 它有兩個參數,即名稱和對象。

  • 遠程注冊表的名稱
  • 與名稱關聯的遠程對象

異常:

  1. 當名稱格式不是 URL 時出現MalformedURLException
  2. 當與遠程注冊表的聯係失敗時發生RemoteException
  3. 當不允許訪問此操作時發生AccessException

方法五: unbind()解除該名稱與關聯遠程對象的綁定

用法:

static void unbind(String name) 

參數:遠程注冊表的名稱

Exceptions:

  1. NotBoundException 當名稱未與當前操作綁定時出現
  2. 當名稱格式不是 URL 時出現MalformedURLException
  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 successfully
            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());
        }
    }
}

輸出:

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


相關用法


注:本文由純淨天空篩選整理自abhinavjain194大神的英文原創作品 java.rmi.Naming Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。