RMI multiple clients - one server object for each of them

前端 未结 1 1740
梦毁少年i
梦毁少年i 2020-12-10 22:26

I am writing a prototype of cryptographic system using RMI.

I have a problem, because when I launch two clients, they got a response from one object in the server fr

相关标签:
1条回答
  • 2020-12-10 22:52

    I called this the Remote Session pattern in my 2001 book. The remote object in the Registry is a kind of login server exporting only a login() method. The login() method, if successful, returns a new remote object per call, which is basically a per-client remote session object. This session object can export a logout() method, which unexports itself, and it can also implement Unreferenced, such that the unreferenced() method also unexports itself (or you can rely on DGC which des the same thing anyway: using Unreferenced gives you a chance to log it). This remote session object exports all the remote methods that a logged in client should have access to, and because it is per-client it can hold client state, hence it is a session.

    public interface RemoteLogin extends Remote
    {
        RemoteSession login() throws RemoteException;
    }
    
    public interface RemoteSession extends Remote
    {
        void logout() throws RemoteException;
        void myMethod(...) throws RemoteException; // whatever you need
    }
    
    public class RemoteLoginImpl extends UnicastRemoteObject implements RemoteLogin
    {
      // ...
      public RemoteSession login()
      {
        // ...
        return new RemoteSessionImpl(); // whatever arguments you need
      }
    }
    
    public class RemoteSessionImpl extends UnicastRemoteObject implements RemoteSession, Unreferenced
    {
      // ...
    }
    
    0 讨论(0)
提交回复
热议问题