Search code examples
javarmi

How to create 2 or more different RMI remote objects in one interface?(factory Pattern)


For Example

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

And Next

String si1User = si1.getUser();

Will the result of si1User become "User1" ?


Solution

  • The simple answer in your case is No. You're still referencing the same remote object in registry bound to the address. Good place to begin learning more about RMI architecture > here.

    EDIT 1

    Simple RMI Factory example I whipped up quickly...

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

    Running the client produce output as below.

    Tom: Hello!
    Jerry: Hello
    Tom: Hey There!!!
    Jerry: :o)