JAVA RMI


Überblick

RMI

  1. Interface und Implementation

  2. rmic
    Stub und Skel

  3. rmiregistry

  4. rmi.Naming.bind("name", object);

  5. Interface object = (Interface) rmi.Naming.lookup("url");

  6. result = object.method(parm);
    ===
    channel.send(parm); result = channel.receive();


Definition eines Interface

import java.rmi.*;

public interface RemoteInterface extends Remote {

  public int tuwas(int l) throws RemoteException;

}


Implementierung des Interface

import java.rmi.*;
import java.rmi.server.*;

public class RemoteExam 
       extends UnicastRemoteObject 
       implements RemoteInterface {

  private int id = -1;

  public RemoteExam() throws RemoteException {
    super();
    id = 0;
  }  

  public int tuwas(int l) throws RemoteException {
    return id+l;
  }

}


Server

  private int l;
  public RemoteExam ex;

  public void register() {

    try { ex = new RemoteExam(); }
    catch (RemoteException re) {
      System.err.println("Can not create RemoteExam.");
      System.exit(-1);
      } 

    /* register */
    try { Naming.rebind("remote", ex); }
    catch (RemoteException re) {
      System.err.println("RemoteException in bind.");
      System.exit(-1);
      } 
    catch (MalformedURLException u) {
      System.err.println("MalformedURLException in bind.");
      System.exit(-1);
      } 

    System.out.println("method registered");
  }


Client

  private int l, m;
  RemoteInterface ex;

  void use() {

    try { ex = (RemoteInterface) Naming.lookup("rmi://localhost/remote"); }
    catch (RemoteException re) {
      System.err.println("RemoteException in ex20c " + re);
      System.exit(-1);
      } 
    catch (NotBoundException nb) {
      System.err.println("NotBoundException " + nb);
      System.exit(-1);
      } 
    catch (MalformedURLException u) {
      System.err.println("MalformedURLException in ex20c " + u);
      System.exit(-1);
      } 
    catch (ClassCastException c) {
      System.err.println("ClassCastException in ex20c " + c);
      System.exit(-1);
      } 

    System.out.println("method found");

    try { m = ex.tuwas(3); 
          System.out.println("answer from tuwas = " + m);
    }
    catch (RemoteException re) {
      System.err.println("RemoteException in ex20c " + re);
      System.exit(-1);
      } 

    System.out.println("method used");
  }


Remote Interface, Remote Example, ex20s.java Server, ex20c.java Client.

listrmireg.java List RMI registry.

SmallPrime Interface, SmallPrime Implementation, ex21s.java Server, ex21c.java Client.

ex21.out Beispiel trumpf-10, ex21-c.out Beispiel comyc.



[Previous] [Next] [Contents]