Java RMI Client Server Kommunikation


Über eine zentrale Registry melden sich Client und Server an. Dieser MArktplatz oder ORB (Object Request Broker) verwaltet die Dienste und vergibt die Zuschläge für die Nutzung der registrierten Dienste.

Client:

public class Client
{
    public static void main(String[] args) throws RemoteException, NotBoundException 
    {
    RmiService rmiService = new RmiService();
    ParkhausService parkhausService = rmiService.getServiceViaRmi("ParkhausService");

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));    
    String fromUser = null;
         while (true) {
                //schicke Input zum Server
                    try {
                        fromUser = stdIn.readLine();
                    } catch (IOException ex) {
                        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if(!handleUserinput(fromUser,  parkhausService))
                    {
                        //beenden
                        break;
                    }

        }
        try {
            stdIn.close();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



    public static boolean handleUserinput(String fromUser,  ParkhausService parkhausService) throws NumberFormatException, RemoteException {
        Integer parameter1 = null;
        String[] wordsFromUser = Pattern.compile(" ").split(fromUser);
        if (wordsFromUser.length == 1) {
            fromUser = wordsFromUser[0];
        } else if (wordsFromUser.length > 1) {
            fromUser = wordsFromUser[0];
            parameter1 = Integer.parseInt(wordsFromUser[1]);
        }
        if (parameter1 != null) {
            if (fromUser.equals("in")) {
                System.out.println(parkhausService.in(parameter1));
            } else if (fromUser.equals("out")) {
                System.out.println(parkhausService.out(parameter1));
            }
        } else {
            if (fromUser.equals("free")) {
                System.out.println(parkhausService.free());
            } else if (fromUser.equals("quit")) {
                return false;
            } else {
                //do nothing
            }
        }
        return true;
    }
}

Server:

public class Server {
    public static void main(String[] args) throws RemoteException, AlreadyBoundException  {
        RmiService rmiService = new RmiService();
        rmiService.registerService("ParkhausService");

        System.out.println( "ParkhausService angemeldet" );
        //kein busy waiting
        try {
            Thread.sleep(10000000);
        } catch (InterruptedException ex) {
            
        }
    }
}

RMIService:

public class RmiService {

    public void registerService(String serviceName) throws RemoteException, AlreadyBoundException {
        Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        RemoteServer.setLog( System.out );
        registry.rebind(serviceName, new ParkhausServiceImpl());

    }
        public ParkhausService getServiceViaRmi(String serviceName) throws RemoteException, NotBoundException {
        //http://www.eshca.net/java/books/javainsel8/javainsel_19_004.htm
        Registry registry = LocateRegistry.getRegistry();
        ParkhausService parkhausService = (ParkhausService) registry.lookup(serviceName);
        return parkhausService;
    }
}

ParkhausService:

public interface ParkhausService extends Remote {

     public String in(int anzahlAutos) throws RemoteException;

     public String out(int anzahlAutos) throws RemoteException;

     public String free() throws RemoteException;
}