RMI

PREVIOUS


RMI

(Remote Method Invocation)

Introduction :
          RMI allows a Java Object that executes on one machine to invoke a method of a Java object that executes on another machine.
          It allows to build Distributed Applications (i.e., comprised of server and client)

How RMI Works :

          A Server application creates some remote objects, makes references to them accessible, and waits for clients to invoke methods on these remote objects.
          A Client application gets a remote reference to one or more remote objects in the server and then invokes methods on them.






Locating Remote Objects:
          To Obtain reference to remote objects, an application must register its
Remote objects with the rmi registry.

Rmi register:
It’s a container class, it consist of collection of java objects. To start the registry and transmit the java object through the specific location of the port number.

Remote Interfaces, Objects  and Methods:
          Like any other application, a distributed application also made up of interfaces and classes.
1.     The interfaces define methods. And the classes implements the methods defined in the interface.
           
1.     Objects that have methods that can be called across virtual machines are remote objects. An Object becomes remote by implementing a remote interface.

Characteristics of Remote Interface :
          1. Extends the interface java.rmi.Remote
          2. Each method of the Interface declares java.rmi.RemoteException in its throws clause
          Before Starting the distributed application, we must generate the necessary stub and skeleton.
          A Stub is a Java object that resides on the Client machine.
          A Skeleton is Java object that resides on the Server machine.



Creating Distributed Applications Using RMI :
Steps Involved:
          1. Design and Implement the Components of Distributed application.
          2. Compile sources and generate stubs.
          3. Make classes network accessible.
          4. Start the application.
Design and Implement the Components of Distributed application:
          Decide the Application Architecture and determine which components are local objects and which ones are remotely accessible.
          Compile sources and generate stubs:
          First compile the source code of server & client using javac compiler.
          Using rmic compiler to create Stubs.
          RMI uses a remote object’s stub class as a proxy in client, so clients can communicate  with a remote object.
Make Classess Network Accessible:
          Download the necessary class files and associated remote interfces, stubs to clients. So that the remote objets are ready to accept remote methods.
Start the Application:
          Before starting the server program, start the RMI’s registry using rmiregistry command.

A Simple RMI Application :
          A simple client/server application, the server receives a request from a client, process it and returns a result. In this example, the request specifies two numbers. The server adds these together and return the sum.
          All remote interfaces must extend the Remote interface, which is part of java.rmi.  Its purpose is simply to specify that an interface uses remote methods. All remote methods throw a RemoteException.
File1 : AddServerIntf.java

import java.rmi.*;
public interface AddServerIntf extends Remote
{
          double add(double d1, double d2) throws RemoteException;
}

Implementing the Remote Interface : All Remote objects must extend UnicastRemoteObject, which provide functionality that is needed to make objects available from remote machines.
File2 : AddServerImpl.java

import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServreIntf
{
          public AddServerImpl() throws RemoteException { }
          public double add(double d1, double d2)
          {
                   return d1+d2;
          }
}

Update RMI Registry on the server Machine : It is done by using rebind() method of Naming class. It takes two arguments one specifies the name of the server another one specifies reference to an instance of AddServerImpl.
File3: AddServer.java

import java.net.*;
import java.rmi.*;
public class AddServer
{
          public static void main(String a[])
          {
                   try
                   {
                             AddServerImpl addServerImpl = new AddServerImpl();
                             Naming.rebind(“AddServer”,addServerImpl);
                   }catch(Exception e)
                   {
                             System.out.println(“Exception : “ + e);     
                   }
          }
}
Creation of Client : This program invokes the lookup( ) method of the Naming class. This method accepts one argument, the rmi URL, and returns a reference to an object of type AddServerIntf. All remote method invocations can then be directed to this object.
//File4: AddClient.java

import java.rmi.*;
public class AddClient
{
public static void main(String a[])
 {
  try
  {
      String url="rmi://"+a[0]+"/AddServer";

      AddServerIntf addServerIntf=(AddServerIntf)Naming.lookup(url);
      System.out.println("The First No. Is : "+a[1]);

      double d1=Double.parseDouble(a[1]);
      System.out.println("The Second No. Is : "+a[2]);
      double d2=Double.parseDouble(a[2]);
      System.out.println("Sum is : "+addServerIntf.add(d1,d2));
       }catch(Exception e)
          {
             System.out.println("Exception : "+e);
          }
    }
 }
  
I. After the creation of these files compile all the files using javac.

Step II : Generate Stub & Skeleton :
          To generate stubs and skeletons, use a tool called RMI compiler, which is invoked from the command line.
v rmic AddServerImpl
This command generates two files: AddServerImpl_Skel.class(Skeleton) and AddServerImpl_Stub.class(Stub).

Step III : Installing Files on the Client & Server Machines
Files on the Client Machine : AddClient.class, AddServerImpl_Stub.class and AddServerIntf.class.
Files on the Server Machine : AddServerIntf.class, AddServerImpl.class, AddServerImpl_Skel.class, AddServerImpl_Stub.class and AddServer.class.

Step IV : Starting the RMI registry by invoking the following command in the Server Machine.(server folder)
v start rmiregistry
Step V : Start the server by invoking
v java AddServer
Step VI : Start the client by invoking (client folder)
v java AddClient <server-name> <number1> <number2>
·        server name->:mycomputer (right click)->computer name->full computer name(select the name and copy) and paste the <server name> place




JAR
(Java Archive)
          JAR file format enables you to bundle multiple files into a single archive file. Typically a JAR file will contain the class files and auxiliary resources associated with applets and applications.
Advantages of JAR File format:
          1. Applet and its related files are bundled in a single file. So, the file is downloaded in a single HTTP transaction.
          2. JAR format compress the file contents for efficient storage.
          3. Package Sealing: It means that all the class files in a package must be in a same JAR file.
Creating JAR File :
          jar cf <jar-file> <input-file(s)>
jar cf anu c*.class
          c        -        This option indicates that to create a JAR file.
          f        -        The Output is send to a file.
   <Jar-file> -        Name of the result file.
   <Input-file(s)>  - List of files that are to be stored in the Jar File.

          This command will generate a default Manifest file for the Archive.

          v        -        List the name of the files added to the Jar while building                                     the Jar file.   (eg: jar  cvf  jj  d*.class)
          0(Zero) -     Indicate the Jar file cannot be compressed.(Turn off                                   compression.)  
·          jar cvf anu a*.classàaftr compress capasity
·          jar cv0f anu a*.classàbefore compress capacity


Viewing the Contents of a JAR file:
          Syntax:
·        jar tf <jar-file>
·        jar tf anuàto view contents
    
                   t        -        Indicates to view the contents of the jar file.
                   f        -        Specifies the name of the jar file to display.
          The JAR file contains a manifest file -> META-INF/MANIFEST.MF,                  which is by default automatically created.

The resulting jar file would have the contents :
eg.
META-INF/MANIFEST.MF
F:\poovi\demo.class
F:\poovi\dee.class
...
jar tvf <jar-file> àTo view date and time.

Updating a Jar File:
          User can update the contents of an exsisting JAR file by modifying its manifest file or by adding files.
For Adding files:
          *  jar uf <jar-file> <files>
              jar uf anu

          u        -        Indicates updation.
          f        -        Indicates jar file to update is specified in the command line.

Extracting the Contents of the JAR file:
          To extract all the files from the specified Archive use the following
syntax:
          jar xf <jar-file>
·          jar xf  anu
·          goto editàopen the folder (now created metainfo folder)àmanifest(cme bak new page)

·          Main-Class:  <classname>
 eg:
Main-Class:  con2(type this statement)
save: pri.txt(extension wil be the text format)



          x        -        Indicates extraction.
          f        -        Indicates the following argument contains jar filename.
         
         
How to run the class file (only application)
To indicate which class is the application’s entry point, you must add a Main-Class header to the jar file’s manifest.

For Update Manifest:
          jar umf <manifest>  <jar-file>
          u        -        Indicates updation
          m       -        Indicates updation of jar’s manifest file.
First  create a text file containing the information to be updated in the manifest file. Then give
jar umf <textfilename> <jar file>
jar umf pri.txt anu
Run cmd:
java -jar <jar-file>
java –jar anu



How to run the applet:
It can be executed using the appletviewer by including archive attributes.
Eg.
<Applet code=“draw1.java” Archive=“king.jar” width=300 height=300>
</applet>

save :
File_name.html
         

No comments:

Post a Comment