gpt4 book ai didi

java - 如何在一个界面中创建 2 个或多个不同的 RMI 远程对象?(工厂模式)

转载 作者:行者123 更新时间:2023-12-04 06:35:48 25 4
gpt4 key购买 nike

例如

SvrInterface si1 = (SvrInterface) Naming.lookup(Address);
SvrInterface si2 = (SvrInterface) Naming.lookup(Address);
si1.setUser ("User1");
si2.setUser ("User2");

接下来
String si1User = si1.getUser();

si1User 的结果会变成“ User1”吗?

最佳答案

在您的情况下,简单的答案是否定的。您仍然在绑定(bind)到该地址的注册表中引用同一个远程对象。开始了解更多关于 RMI 架构的好地方> here .

编辑 1

简单的 RMI 工厂示例我很快就开始了......

回声服务

public interface EchoService extends Remote, Serializable{
String echo(String msg) throws RemoteException;
String getUser() throws RemoteException;
void setUser(String user) throws RemoteException;
}

EchoServiceImpl
public class EchoServiceImpl extends UnicastRemoteObject implements EchoService {

private static final long serialVersionUID = -3671463448485643888L;

private String user;

public EchoServiceImpl() throws RemoteException {
super();
}

@Override
public String echo(String msg) throws RemoteException {
return this.user + ": " + msg;
}

@Override
public String getUser() throws RemoteException {
return this.user;
}

@Override
public void setUser(String user) throws RemoteException {
this.user = user;
}
}

回声服务工厂
public interface EchoServiceFactory extends Remote {
EchoService createEchoService() throws RemoteException;
}

EchoServiceFactoryImpl
public class EchoServiceFactoryImpl extends UnicastRemoteObject implements
EchoServiceFactory {

private static final long serialVersionUID = 6625883990856972736L;

protected EchoServiceFactoryImpl() throws RemoteException {
super();
System.setProperty("java.rmi.server.codebase", EchoServiceFactory.class.getProtectionDomain().getCodeSource().getLocation().toString());

System.setProperty("java.security.policy", "/java.policy");

if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}

try {
Registry registry = LocateRegistry.getRegistry("localhost");
registry.rebind("EchoService", this);
System.out.println("Echo service factory registered.");
} catch (Exception e) {
System.err.println("Error registering echo service factory: "
+ e.getMessage());
}
}

@Override
public EchoService createEchoService() throws RemoteException {
return new EchoServiceImpl();
}

public static void main(String[] args) {
try {
new EchoServiceFactoryImpl();
} catch (RemoteException e) {
System.err.println("Error creating echo service factory: "
+ e.getMessage());
}
}

}

回声服务客户端
public class EchoServiceClient {

public static void main(String[] args) {
try {

System.setProperty("java.security.policy", "/java.policy");

if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}

Registry registry = LocateRegistry.getRegistry("localhost");

EchoServiceFactory serviceFactory =
(EchoServiceFactory) registry.lookup("EchoService");
EchoService echoServiceX = serviceFactory.createEchoService();
echoServiceX.setUser("Tom");
System.out.println(echoServiceX.echo("Hello!"));
EchoService echoServiceY =
serviceFactory.createEchoService();
echoServiceY.setUser("Jerry");
System.out.println(echoServiceY.echo("Hello"));
System.out.println(echoServiceX.echo("Hey There!!!"));
System.out.println(echoServiceY.echo(":o)"));

} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

运行客户端产生如下输出。
Tom: Hello!
Jerry: Hello
Tom: Hey There!!!
Jerry: :o)

关于java - 如何在一个界面中创建 2 个或多个不同的 RMI 远程对象?(工厂模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4895698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com