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

时间:2011-02-04 07:51:15

标签: java rmi

例如

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

下一步

String si1User = si1.getUser();

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

1 个答案:

答案 0 :(得分:3)

您的案例中的简单答案是否。您仍然在引用绑定到该地址的注册表中引用相同的远程对象。开始学习更多关于RMI架构的好地方> here

编辑1

简单的RMI工厂示例我很快就掀起了......

EchoService的

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;
    }
}

EchoServiceFactory

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());
    }
}

}

EchoServiceClient

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)