3 Replies Latest reply on Apr 23, 2003 2:36 PM by Shigeru Chiba

    sub code generation with javassist.rmi.StubGenerator

    mingqin Newbie

      In article "Load-time Structural Reflection in Java" by Shigeru Chiba, Remote Method Invocation (section 4.3), author mentioned javasssit enables programmers to use customized protocol (Sun's RMI default protocol is JRMI), and produce sub code( those cods reside on client VM side generated by RMIC compiler to communicate with remote object in server Virtual Machine) at compile-time and even runtime.

      Can javassist experts give some demo codes show how to use javassist to get rid of rmic compile process ?
      For example:
      In ordinary RMI programming, first we define an interface extends from java.rmi.Remote(Calculator.java) and write implementation codes( CalculatorImpl.java) for that interface, once we compiled CalculatorImp.java, we need to generate sub class CalculatorImp_Stub.clas by running rmic compiler. Then we package Calculator.class and CalculatorImpl_Stub to another Virtul Machine which CalcaulatorClient.java is installed.
      By using javassist's javassist.rmi.StubGenerator, Can we skip the rmic compiling execution and still generate CalculatorImp_Stub.class or sort of class as it for CaculatorClient.class to communicate with remote object?

      //Calculator.java define a remote object
      public interface Calculator
      extends java.rmi.Remote {
      public long add(long a, long b)
      throws java.rmi.RemoteException;

      public long sub(long a, long b)
      throws java.rmi.RemoteException;

      public long mul(long a, long b)
      throws java.rmi.RemoteException;

      public long div(long a, long b)
      throws java.rmi.RemoteException;
      }


      //CalculatorImpl.java remote object implementation
      public class CalculatorImpl
      extends
      java.rmi.server.UnicastRemoteObject
      implements Calculator {

      // Implementations must have an
      //explicit constructor
      // in order to declare the
      //RemoteException exception
      public CalculatorImpl()
      throws java.rmi.RemoteException {
      super();
      }

      public long add(long a, long b)
      throws java.rmi.RemoteException {
      return a + b;
      }

      public long sub(long a, long b)
      throws java.rmi.RemoteException {
      return a - b;
      }

      public long mul(long a, long b)
      throws java.rmi.RemoteException {
      return a * b;
      }

      public long div(long a, long b)
      throws java.rmi.RemoteException {
      return a / b;
      }
      }

      //CalculatorServer.java Host Calculator object on Sever
      import java.rmi.Naming;

      public class CalculatorServer {

      public CalculatorServer() {
      try {
      Calculator c = new CalculatorImpl();
      Naming.rebind("rmi://localhost/CalculatorService", c);
      } catch (Exception e) {
      System.out.println("Trouble: " + e);
      }
      }

      public static void main(String args[]) {
      new CalculatorServer();
      }
      }

      //CalculatorClient.java a client on another Virtual //Machien to use Calculator remote object.
      import java.rmi.Naming;
      import java.rmi.RemoteException;
      import java.net.MalformedURLException;
      import java.rmi.NotBoundException;

      public class CalculatorClient {

      public static void main(String[] args) {
      try {
      Calculator c = (Calculator)
      Naming.lookup(
      "rmi://localhost:1099/CalculatorService");
      System.out.println( c.sub(4, 3) );
      System.out.println( c.add(4, 5) );
      System.out.println( c.mul(3, 6) );
      System.out.println( c.div(9, 3) );
      }
      catch (MalformedURLException murle) {
      System.out.println();
      System.out.println(
      "MalformedURLException");
      System.out.println(murle);
      }
      catch (RemoteException re) {
      System.out.println();
      System.out.println(
      "RemoteException");
      System.out.println(re);
      }
      catch (NotBoundException nbe) {
      System.out.println();
      System.out.println(
      "NotBoundException");
      System.out.println(nbe);
      }
      catch (
      java.lang.ArithmeticException
      ae) {
      System.out.println();
      System.out.println(
      "java.lang.ArithmeticException");
      System.out.println(ae);
      }
      }
      }