Play and learn 300 000+ tabs online

Saturday, January 23, 2010

Remote Method Invocation

100)    What is RMI and steps involved in developing an RMI object?
Ans: Remote Method Invocation (RMI) allows java object that executes on one machine and to invoke the method
of a Java object to execute on another machine.The steps involved in developing an RMI object are:
a)    Define the interfaces
b)    Implementing these interfaces
c)    Compile the interfaces and their implementations with the java compiler
d)    Compile the server implementation with RMI compiler
e)    Run the RMI registry
f)    Run the application
101)    What is RMI architecture?
Ans: - RMI architecture consists of four layers and each layer performs specific functions:
            a) Application layer             ---- contains the actual object definition
            b) Proxy layer                      ---- consists of stub and skeleton
c)    Remote Reference layer  ---- gets the stream of bytes from the transport layer and sends it to the                                                                   proxy  layer
d)    Transportation layer        ---- responsible for handling the actual machine-to-machine communication

102)    what is UnicastRemoteObject?
Ans: All remote objects must extend UnicastRemoteObject, which provides functionality that is needed to make objects available from remote machines.
103)    Explain the methods, rebind( ) and lookup() in Naming class?
Ans: rebind( ) of the Naming class(found in java.rmi) is used to update the RMI registry on the server machine.
Naming. rebind(“AddServer”, AddServerImpl); lookup() of the Naming class accepts one argument, the rmi URL and returns a  reference to an object of t type AddServerImpl.


5.    What is Bootstrapping in RMI?

Dynamic loading of stubs and skeletons is known as Boot Strapping.

30.RemoteObjects: Objects that have methods that can be called accross virtual machines are Remote Objects.An object becomes Remote by implementing Remote Interface.
50. Skeletons are server side proxies and stubs are client side proxies.
64.What is a policy?.
It’s an abstract class for representing the system security policy for a Java application environment(specifying which permissions are available for code from various sources). Java security properties file resides in <JAVA-HOME>/lib/security/java.security directory. Value of “policy.provider” should be changed.
90.    RPC stands for Remote Procedure Call.
91.    The three layers in RMI are Application Layer,RemoteReferenceLayer and Network Layer.



Serializing an Object
The object to be serialized must implement java.io.Serializable.
try {
ObjectOutput out = new ObjectOutputStream(
new FileOutputStream(“filename.ser”));
out.writeObject(object);
out.close();
} catch (IOException e) {

}



Deserializing an Object
This example deserializes a java.awt.Button object.
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(“filename.ser”));
AnObject object = (AnObject) in.readObject();
in.close();
} catch (ClassNotFoundException e) {
} catch (IOException e) {

}




















Defining and Exporting a Remote Object
1. Define the remote interface.
import java.rmi.*;
public interface RObject extends Remote {
void aMethod() throws RemoteException;
    }

Looking Up a Remote Object and Invoking a Method
try {
// Look up remote object
RObject robj = (RObject) Naming.lookup(
“//localhost/RObjectServer”);

// Invoke method on remote object
robj.aMethod();
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (NotBoundException e) {
} catch (RemoteException e) {
 }

Passing Parameters to a Remote Method
Arguments to remote methods must be primitive, serializable, or Remote. This example demonstrates the declaration and use of all three parameter types.

1. Define the remote interface.
import java.rmi.*;
public interface RObject extends Remote {
// This parameter is primitive.
void primitiveArg(int num) throws RemoteException;
// This parameter implements Serializable.
void byValueArg(Integer num) throws RemoteException;
// This parameter implements Remote.
void byRefArg(ArgObject arg) throws RemoteException;
    }

public interface ArgObject extends Remote {
int aMethod() throws RemoteException;
    }

2. Define the remote object implementation.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class RObjectImpl extends UnicastRemoteObject implements RObject {
public RObjectImpl() throws RemoteException {
super();
    }
public void primitiveArg(int num) throws RemoteException {
   }

public void byValueArg(Integer num) throws RemoteException {
    }
public void byRefArg(ArgObject arg) throws RemoteException {
    }

    }

3. Compile the remote object implementation.
•    javac RObject.java RObjectImpl.java

4. Generate the skeletons and stubs.
•    rmic RObjectImpl

5. Create an instance of RObjectImpl and bind it to the RMI Registry.
try {
RObject robj = new RObjectImpl();
Naming.rebind(“//localhost/RObjectServer”, robj);
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (RemoteException e) {
    }

6. Look Up the Remote object and pass the parameters.
try {
// Look up the remote object
RObject robj = (RObject) Naming.lookup(“//localhost/RObjectServer”);

// Pass a primitive value as argument
robj.primitiveArg(1998);

// Pass a serializable object as argument
robj.byValueArg(new Integer(9));

// Pass a Remote object as argument
robj.byRefArg(new ArgObjectImpl());
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (NotBoundException e) {
} catch (RemoteException e) {
    }

Returning Values from a Remote Method
Return values from remote methods must be primitive, serializable, or Remote.
This example demonstrates the declaration and use of all three return types. 1.
Define the remote interface.

import java.rmi.*;
public interface RObject extends Remote {
// This return value is primitive.
int primitiveRet() throws RemoteException;

// This return value implements Serializable.
Integer byValueRet() throws RemoteException;

// This return value implements Remote.
ArgObject byRefRet() throws RemoteException;
    }

public interface ArgObject extends Remote {
int aMethod() throws RemoteException;
    }

2. Define the remote object implementation.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class RObjectImpl extends UnicastRemoteObject
implements RObject {
public RObjectImpl() throws RemoteException {
super();
     }
public int primitiveRet() throws RemoteException {
return 3000;
    }
public Integer byValueRet() throws RemoteException {
return new Integer(2000);
   }
public ArgObject byRefRet() throws RemoteException {
return new ArgObjectImpl();
   }
  }


3. Compile the remote object implementation.
•    javac RObject.java RObjectImpl.java

4. Generate the skeletons and stubs.
•    rmic RObjectImpl

5. Create an instance of RObjectImpl and bind it to the RMI Registry.
try {
RObject robj = new RObjectImpl();
Naming.rebind(“//localhost/RObjectServer”, robj);
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (RemoteException e) {
 }

6. Look Up the Remote object, invoke the methods, and receive the return
values.
try {
// Look up the remote object
RObject robj = (RObject) Naming.lookup(
“//localhost/RObjectServer”);
// Receive the primitive value as return value
int r1 = robj.primitiveRet();
// Receive the serializable object as return value
Integer r2 = robj.byValueRet();
// Receive the Remote Object as return value
ArgObject aobj =  robj.byRefRet();
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (NotBoundException e) {
} catch (RemoteException e) {
    }

Throwing an Exception from a Remote Method
1. Define the remote interface.
import java.rmi.*;
public interface RObject extends Remote {
void aMethod() throws RemoteException;
    }
2. Define the remote object implementation.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class RObjectImpl extends
UnicastRemoteObject implements RObject {
public RObjectImpl() throws RemoteException {
super();

 }

public void aMethod() throws RemoteException {
// The actual exception must be wrapped in
// a RemoteException
throw new RemoteException(
“message”, new FileNotFoundException(“message”));

     }

    }

3. Compile the remote object implementation.
•    javac RObject.java RObjectImpl.java


4. Generate the skeletons and stubs.
•    rmic RObjectImpl

5. Create an instance of RObjectImpl and bind it to the RMI Registry.
try {
RObject robj = new RObjectImpl();
Naming.rebind(“//localhost/RObjectServer”, robj);
} catch (MalformedURLException e) {
} catch (UnknownHostException e) {
} catch (RemoteException e) {
    }

14

1.what  is a transient variable?
A transient variable is a variable that may not be serialized.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.