Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavajava.rmi.Naming Class in Java

java.rmi.Naming Class in Java

Java.rmi.Naming class contains a method to bind, unbind or rebind names with a remote object present at the remote registry. This class is also used to get the reference of the object present at remote registries or the list of name associated with this registry.

Syntax: Class declaration

public final class Naming extends Object

Let us do discuss some major methods of this class which are as follows:

The most commonly used methods are described below as follows:

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

Method 1: bind() 

Syntax:

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

Parameters: Name of the remote registry

Exceptions:

  1. AlreadyBoundException occurs when the name was already bounded
  2. MalformedURLException occurs when the format of the name is not an URL
  3. RemoteException occurs when the contact to remote registry fails
  4. AccessException occurs when access to this operation is not permitted

Method 2: list() Used to get the list of the names associated with the registry

Syntax:

static string[] list(String name)

Parameters: The name of the remote registry

Return Type: An array of name bounded with this registry

Exceptions:

  1. MalformedURLException occurs when the format of the name is not an URL
  2. RemoteException occurs when hen the contact to remote registry fails

Method 3: lookup() looks for the reference of the remote object to which this name is associated

Syntax:

static remote lookup(String name)

Parameters: The name of the remote registry

Return Type: The reference for a remote object

Exceptions:

  1. NotBoundException occurs when the name does not bound with the current operation
  2. RemoteException occurs when the contact to remote registry fails
  3. AccessException occurs when access to this operation is not permitted
  4. MalformedURLException occurs when the format of the name is not an URL

Method 4: rebind() method rebinds this name with the associated remote object

Syntax:

static void rebind(String name, remote object)

Parameters: It takes two parameters namely name and object.

  • The name of the remote registry
  • The remote object to associate with the name

Exceptions:

  1. MalformedURLException occurs when the format of the name is not an URL
  2. RemoteException occurs when the contact to remote registry fails
  3. AccessException occurs when access to this operation is not permitted

Method 5: unbind() unbinds this name with the associated remote object

Syntax:

static void unbind(String name) 

Parameters: The name of the remote registry

Exceptions:

  1. NotBoundException occurs when the name does not bound with the current operation
  2. MalformedURLException occurs when the format of the name is not an URL
  3. RemoteException occurs when the contact to remote registry fails
  4. AccessException occurs when access to this operation is not permitted

Example 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"
                            + "/neveropen",
                        (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());
        }
    }
}


Output: 

registry created successfully

Implementation: Java program to look for the object(Clients’ side)

Example 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) {
        }
    }
}


Output: 

GeeksForGeeks

 

RELATED ARTICLES

Most Popular

Recent Comments