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:
- bind()
- list()
- lookup()
- rebind()
- 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:
- AlreadyBoundException occurs when the name was already bounded
- MalformedURLException occurs when the format of the name is not an URL
- RemoteException occurs when the contact to remote registry fails
- 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:
- MalformedURLException occurs when the format of the name is not an URL
- 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:
- NotBoundException occurs when the name does not bound with the current operation
- RemoteException occurs when the contact to remote registry fails
- AccessException occurs when access to this operation is not permitted
- 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:
- MalformedURLException occurs when the format of the name is not an URL
- RemoteException occurs when the contact to remote registry fails
- 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:
- NotBoundException occurs when the name does not bound with the current operation
- MalformedURLException occurs when the format of the name is not an URL
- RemoteException occurs when the contact to remote registry fails
- AccessException occurs when access to this operation is not permitted
Example 1:
Java
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 demo extends Remote {
public String msg(String msg) throws RemoteException;
}
class demoRemote
extends UnicastRemoteObject implements demo {
demoRemote() throws RemoteException { super (); }
public String msg(String msg) throws RemoteException
{
System.out.println( "GeeksForGeeks" );
return null ;
}
}
public class GFG_Server {
public static void main(String args[])
throws RemoteException, NotBoundException,
AlreadyBoundException
{
try {
Registry registry
= LocateRegistry.createRegistry( 3000 );
demo obj = new demoRemote();
+ "/geeksforgeeks" ,
(Remote)obj);
System.out.println(
"registry created successfully" );
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
|
Output:
registry created successfully
Implementation: Java program to look for the object(Clients’ side)
Example 2:
Java
import java.rmi.*;
public class GFG_Client {
public static void main(String args[])
{
try {
demo client = (demo)Naming.lookup(
System.out.println(client.msg());
}
catch (Exception e) {
}
}
}
|
Output:
GeeksForGeeks
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...