【java】Naming.bind和Registry.bind区别

时间:2022-05-05 08:51:06
  1. Naming类和Registry类均在java.rmi包
  2. Naming类通过解析URI绑定远程对象,将URI拆分成主机、端口和远程对象名称,使用的仍是Registry类。

java

public static Remote lookup(String name)

throws NotBoundException,

java.net.MalformedURLException,

RemoteException

{

ParsedNamingURL parsed = parseURL(name);

Registry registry = getRegistry(parsed);

    if (parsed.name == null)
return registry;
return registry.lookup(parsed.name);
}

public static void bind(String name, Remote obj)

throws AlreadyBoundException,

java.net.MalformedURLException,

RemoteException

{

ParsedNamingURL parsed = parseURL(name);

Registry registry = getRegistry(parsed);

    if (obj == null)
throw new NullPointerException("cannot bind to null"); registry.bind(parsed.name, obj);
} private static Registry getRegistry(ParsedNamingURL parsed)
throws RemoteException
{
return LocateRegistry.getRegistry(parsed.host, parsed.port);
}