RMI-IIOP has previously, only two options for RMI and CORBA for distributed programming. RMI-IIOP integrates the advantages of RMI and CORBA, overcoming their shortcomings, making programmers to write distributed programming for distributed computing. First, RMI-IIOP integrates the simplicity of RMI and CORBA multi-language (compatibility), followed by RMI-IIOP overcomes RMI only for Java's shortcomings and CORBA complexity (you don't have to master IDL). A very simple RMI-IIOP program, which is completed on the previous example (Getting Started by Java2 RMI), which can be compared to the difference between the two programs. 1. Implement a remote interface, generate a remote object, stub, and frame (SKELETON) implementation remote interface, the remote interface tells the JVM: The object that implements the interface can be called remotely and what methods can be called. Sayhello () is defined in this example. Since the remote call involves network communication, these methods must throw RemoteException. Remote interfaces and remote objects can be developed by A and packed the remote interface (Hello) to the Client End Developer B. Build a F: MI_IIOP directory, copy hello.java and helloimpl.java to this directory.
// Hello.java package jdeveloper.rmi; import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote {String sayHello () throws RemoteException;} generated remote object // HelloImpl.java package jdeveloper.. rmi_iiop; import javax.naming *;. import java.rmi.RemoteException; import java.rmi.RMISecurityManager; // import java.rmi.server.UnicastRemoteObject; import javax.rmi.PortableRemoteObject; public class HelloImpl extends PortableRemoteObject implements Hello {public HelloImpl () throws RemoteException {super ();} public String sayHello () {return "Hello World!";} public static void main (String args []) {// Create and install a security manager if (System.getSecurityManager ( ) == null) {System.setSecurityManager (New RMISecurityManager ());} Try { Hello Obj = new helloimpl (); // bind this object instance to the name "helloServer" // ***** Old code for rmi // naming.rebind ("HelloServer", obj); // **** * new code for rmi-iiop Context initialNamingContext = new InitialContext (); initialNamingContext.rebind ( "HelloServer", obj); System.out.println ( "HelloServer bound in registry");} catch (Exception e) {System.out .println ("HelloImpl Err:" E.GETMESSAGE ()); E.PrintStackTrace ();}}} stub (stub)
And frame (SKELETON) F: CD MI_IIOP SET CLASSPATH =.;% Classpath% javac -d. Hello.java javac -d. HelloImpl.java rmic -iiop -d. Jdeveloper.rmi_iiop.HelloIMPL will generate <_interface> _stub .class, <_ InterfaceImpl> _Tie.class: _Hello_Stub.class implemented _HelloImpl_Tie.class 2. Client and server program // HelloClient.java package jdeveloper.rmi_iiop; import java.rmi.RMISecurityManager; import java.rmi.Naming; import java .rmi.RemoteException; import java.rmi.NotBoundException; import javax.rmi.PortableRemoteObject; import javax.naming *;. public class HelloClient {public static void main (String args []) throws Exception {System.setSecurityManager (new RMISecurityManager ( ))); Context initialnamingcontext = new initialcontext (); hello remoteobj = (hello) PortableRemoteObject.narrow (Init) IalnamingContext.lookup ("IIOP: //" Args [0] "/ HelloServer"), Hello.class); System.Out.println (Remoteobj.Sayhello ());}} Copy HelloClient.java to Directory F : MI_IIOP.