Overview of RMI

Embed Size (px)

Citation preview

  • 8/9/2019 Overview of RMI

    1/15

    Overview of RMIThe Remote Method Invocation(RMI) works similar to remote procedure calls(RPC). AllRMI functionality found in the the java.rmi package. The protocol need to run java-only

    implementation is known as Java

    Remote Method Protocol(JRMP) and in order to run in non-JVM, then CORBA is used.

    Generally RMI comprise of two separate programs, a server and a client. A server program creates some remote objects, and makes references to these object which further waits for client to invoke methods through these objects. When server and clientcommunicate and pass their information such type of application is sometimes referred toas distributed object application.

    The Java RMI allow programmer to execute remote function which behave similar to

    local functions calls but the difference is that it exits somewhere else.

    The servers first need to bind its name to the registry because RMI servers on the samehost bind remote object to names. Clients on local and remote hosts can then look upremote objects and can pass message to each other. The client lookup for the server namein the registry to establish the remote references.

    The rmic is a command which generates stubs, skeletons, ties for remote objects usingeither the JRMP or IIOP protocols. These classes files are generated from the compiledJava files classes that contain remote object implementations. The stub is responsible for sending the remote call which passes over to the server-side skeleton The stub opening a

    socket to the remote server, marshaling the object parameters and forwarding the datastream to the skeleton. A skeleton contains a method that receives the remote calls,unmarshals all the parameters, and invokes the actual remote object implementation. Andonce you started the Registry, the server will be started and able to store itself in theRegistry.

    RMI-Example-1This is a very simple example of RMI where you will come to know how RMI works andwhat are the steps of executions. Here listed four java files and steps for executing this

    application

    .

    Code:

    RemoteInterface.java

    http://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-1.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-1.shtmlhttp://www.roseindia.net/tutorials/rmi/Overview-of-RMI.shtml
  • 8/9/2019 Overview of RMI

    2/15

    import java.rmi.*;

    public interface RemoteInterface extends Remote{ public int add( int x,int y) throws Exception;}

    ServerImplements .java

    import java.rmi.*;import java.rmi.server.*;import java.lang.String;

    interface RemoteInterface extends Remote{ public int add( int x,int y) throws Exception;}

    public class ServerImplements extends UnicastRemoteObject implements RemoteInterface{

    public ServerImplements() throws Exception { super (); }

    public int add( int x,int y) { return (x+y); }}

    Server.java

    import java.rmi.*;import java.net.*;

    public class Server{ public static void main(String args[]) { try { ServerImplements s= new ServerImplements(); Naming.rebind( "SERVICE" ,s); System.out.println( "Server Started " ); } catch (Exception e) { System.out.println(e.getMessage()); } }

    http://www.roseindia.net/tutorials/rmi/RMI-Example-1.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-1.shtml
  • 8/9/2019 Overview of RMI

    3/15

    }

    Client.java

    import java.rmi.*;

    import java.io.*;

    public class Client{ public static void main(String args[]) { try { String ip= "rmi://192.168.1.97/RMIAPPLICATION" ; RemoteInterface s=(RemoteInterface)Naming.lookup(ip); System.out.println( "sum: " + s.add( 1,3 )); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } }}

    Steps for executions

    javac RemoteInterface.java

    javac ServerImplements.java

    rmic ServerImplements

    javac Server.java

    javac Client.java

    start rmiregistry

    start java server

    java Client

    Output:

    Sum: 4

    RMI-Example-2

  • 8/9/2019 Overview of RMI

    4/15

    This example demonstrate the RMI Addition Application

    . Well in this application you will use the four java files. List of java files and their codeare shown below:

    Code:RemoteInterface.java

    import java.rmi.*;

    public interface RemoteInterface extends Remote{ public int add( int x,int y) throws Exception;}

    ServerImplements.java

    import java.rmi.*;import java.rmi.server.*;import java.lang.String;

    interface RemoteInterface extends Remote { public int add( int x, int y) throws Exception;}

    public class ServerImplements extends UnicastRemoteObject implementsRemoteInterface { public ServerImplements() throws Exception { super (); }

    public int add( int x, int y) { return (x + y); }}

    Server.java

    import java.rmi.*;import java.net.*;

    public class Server { public static void main(String args[]) { try { ServerImplements s = new ServerImplements(); Naming.rebind( "RMIAPPLICATION" , s); System.out.println( "Server has been started" ); } catch (Exception e) { System.out.println(e.getMessage()); } }

    http://www.roseindia.net/tutorials/rmi/RMI-Example-2.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-2.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-2.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-2.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-2.shtml
  • 8/9/2019 Overview of RMI

    5/15

    }

    Client.java

    import java.rmi.*;

    import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;

    public class Client extends JFrame { TextField t1 = new TextField( 30 ); TextField t2 = new TextField( 30 ); Label rs = new Label( "Sum= 0" ); JButton b = new JButton( "Add" ); Panel p = new Panel( new GridLayout( 4 , 1 , 5 , 5 )); RemoteInterface s;

    public Client() { super ( "Client Side" ); setSize( 250 , 250 ); setLocation( 300 , 300 ); getContentPane().add(p, "North" ); p.add(t1); p.add(t2); p.add(rs); p.add(b);

    try { String ipp = JOptionPane .showInputDialog( "Please enter the IP Address to Connect" ); String ip = "rmi://" + ipp + "/RMIAPPLICATION" ;

    s = (RemoteInterface) Naming.lookup(ip);

    } catch (Exception exp) { JOptionPane.showMessageDialog(null, exp.getMessage()); }

    b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); try { int r = s.add(a, b); rs.setText( "Sum of two no =" + r); } catch (Exception epx) { } } }); }

    public static void main(String args[]) { Client c = new Client(); c.setDefaultCloseOperation(EXIT_ON_CLOSE); c.setVisible( true );

  • 8/9/2019 Overview of RMI

    6/15

    }}

    Steps for execution:

    Step-1

    javac RemoteInterface.java javac ServerImplements.java

    Step-2

    rmic ServerImplements

    This steps create Stub/ Skeleton

    Step-3

    javac Server.java javac Client.java

    Step-4

    Open Two More Consoles. by writing 'start' twice.

    Step-5

    On first consolermiregistry

    Step-6

    On second console java Server

    Step-7

    On third console

    java Client

    This steps can be seen in the following pictures also:

  • 8/9/2019 Overview of RMI

    7/15

    RMI-Example-3

  • 8/9/2019 Overview of RMI

    8/15

    In this example you will see function calls from client to server and vice versa. Thisapplicationfor running requires a Remote Machine.For running this application lets assume there are two machine one with 192.168.10.97and other 192.168.10.113.Here machine having IP address 192.168.10.113 is a server and machine having IPaddress 192.168.10.97 is a client. So we divide the programs in two categories one for server and other for client.Well in this application there are seven java files. List of java files and their code areshown below:

    Code for Server and Client Machine:

    RemoteInterface.java

    import java.rmi.*;

    public interface RemoteInterface extends Remote { public int add( int x, int y) throws Exception;}

    RemoteInterface1.java

    import java.rmi.*;

    public interface RemoteInterface2 extends Remote { public int sub( int x, int y) throws Exception;}

    ServerImplements.java

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

    interface RemoteInterface extends Remote { public int add( int x, int y) throws Exception;}

    public class ServerImplements extends UnicastRemoteObject implementsRemoteInterface {

    public ServerImplements() throws Exception { super (); }

    public int add( int x, int y) { return (x + y); }}

    ServerImplements2.java

    http://www.roseindia.net/tutorials/rmi/RMI-Example-3.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-3.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-3.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-3.shtmlhttp://www.roseindia.net/tutorials/rmi/RMI-Example-3.shtml
  • 8/9/2019 Overview of RMI

    9/15

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

    interface RemoteInterface2 extends Remote { public int sub( int x, int y) throws Exception;}

    public class ServerImplements2 extends UnicastRemoteObject implements RemoteInterface2 {

    public ServerImplements2() throws Exception { super (); }

    public int sub( int x, int y) { return (x - y); }}

    Server.java

    import java.rmi.*;import java.net.*;

    public class Server { public static void main(String args[]) { try { ServerImplements s = new ServerImplements(); Naming.rebind( "SERVICE" , s); System.out.println( "Server Started 1" ); } catch (Exception e) { System.out.println(e.getMessage());

    } }}

    Server2.java

    import java.rmi.*;import java.net.*;

    public class Server2 { public static void main(String args[]) { try { ServerImplements2 s2 = new ServerImplements2(); Naming.rebind( "SERVICE2" , s2); System.out.println( "Server Started 2" ); } catch (Exception e) { System.out.println(e.getMessage()); } }}

  • 8/9/2019 Overview of RMI

    10/15

    Code only for Client Machine:

    client.java

    import java.rmi.*;

    import java.io.*; public class Client {

    public static void main(String args[]) { try { String ip = "rmi://192.168.10.97/SERVICE" ; String ip2 = "rmi://192.168.10.113/SERVICE2" ;

    RemoteInterface s = (RemoteInterface) Naming.lookup(ip); RemoteInterface2 s2 = (RemoteInterface2) Naming.lookup(ip2);

    System.out.println( "Add:" + s.add( 1 , 4 )); System.out.println( "sub:" + s2.sub( 3 , 1 )); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } }}

    Steps for execution at Server machine:

    javac RemoteInterface.java

    javac RemoteInterface1.java

    javac ServerImplements.java

    javac ServerImplements2.java

    rmic ServerImplements

    rmic ServerImplements2

    javac Servet2.java

    start rmiregistry

    java server2

    Steps for execution at Client machine:

    javac RemoteInterface.java

  • 8/9/2019 Overview of RMI

    11/15

    javac RemoteInterface1.java

    javac ServerImplements.java

    javac ServerImplements2.java

    rmic ServerImplements

    rmic ServerImplements2

    javac Server.java

    start rmiregistry

    start java server

    javac Client.java

    java Client

    Output will be shown at client machine

    Add:5

    Sub:2

    Displaying Hello using RMIThis Example describes the way to display Hello message using RMI. By RMI we meanRemote Method Invocation. RMI serves as a basic technique for supporting distributedobjects in java. The steps involved in displaying message Hello are described below:-

    Step1. Create a Remote interface named HelloInterface.java in the Directory.

    HelloInterface.java:-

  • 8/9/2019 Overview of RMI

    12/15

    import java.rmi.*; public interface HelloInterface extends Remote {

    public String say() throws RemoteException;}

    Step2. Create an Remote Class implementation for HelloWorld named Hello.java in theDirectory.

    Hello.java

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

    public class Hello extends UnicastRemoteObject implements HelloInterface{ private String message;

    public Hello (String msg) throws RemoteException { message = msg; } public String say() throws RemoteException { return message; }}

    Step3. Compile the above two Source file named HelloInterface.java and Hello.java.

    Step4. After compiling the above two classes type the following command i.e-"rmicHello" in console just like displayed below.

  • 8/9/2019 Overview of RMI

    13/15

    Your Directory Structure will be like this.By running the "rmic Hello" command a newclass will be created i.e "Hello_Stub.class" in the directory

    Step5. Create Server application named HelloServer.java

    HelloServer.java

    import java.rmi.Naming;

    public class HelloServer{ public static void main (String[] argv)

    { try { Naming.rebind ( "Hello" , new Hello ( "Hello,From Roseindia.net pvtltd!" ));

    http://www.roseindia.net/tutorials/rmi/rmi-helloworld.shtmlhttp://www.roseindia.net/tutorials/rmi/rmi-helloworld.shtmlhttp://www.roseindia.net/tutorials/rmi/rmi-helloworld.shtml
  • 8/9/2019 Overview of RMI

    14/15

    System.out.println ( "Server is connected and ready for operation."); }

    catch (Exception e) { System.out.println ( "Server not connected: " + e); } }}

    Step6. Create Client application named HelloClient.java

    HelloClient.java

    import java.rmi.Naming;

    public class HelloClient

    { public static void main (String[] argv) { try { HelloInterface hello =(HelloInterface) Naming.lookup ( "//192.168.10.201/Hello" ); System.out.println (hello.say()); }

    catch (Exception e){ System.out.println ( "HelloClient exception: " + e);} }}

    Step6. Compile both of the files.

    Step7. Type "rmicregistry" on commandprompt and press ENTER.

    Step8. Type java HelloServer in commandprompt and press ENTER.The followingmessage will be displayed on console.

  • 8/9/2019 Overview of RMI

    15/15

    Step9. Now,open another separate command terminal,and run the client application likeshown in the figure given below:-

    Step10. If the message similar to the above appears in figure comes means that you haveimplemented your RMI application.