Presentation is loading. Please wait.

Presentation is loading. Please wait.

RMI Packages Overview java.rmi java.rmi.server java.rmi.registry

Similar presentations


Presentation on theme: "RMI Packages Overview java.rmi java.rmi.server java.rmi.registry"— Presentation transcript:

1 RMI Packages Overview java.rmi java.rmi.server java.rmi.registry
General RMI classes and exceptions. java.rmi.server RMI server-specific classes and interfaces. java.rmi.registry To access, launch, and locate RMI registries. java.rmi.activation To start remote services on demand. java.rmi.dgc To support distributed object garbage collection. Netprog Java RMI

2 java.rmi Package Remote interface RemoteException class
To identify a service as remotely accessible. RemoteException class java.io.IOException subclass, superclass of most RMI exceptions. Netprog Java RMI

3 java.rmi Package Naming class
Static methods to assign or retrieve object references of the RMI object registry (rmiregistry). bind(String url, Remote obj) Inserts a registry entry and binds it to given obj. rebind(String url, Remote obj) Does not throw AlreadyBoundException. Remote lookup(String url) Returns a reference for the remote object Also unbind(url), list(url) Netprog Java RMI

4 java.rmi Exceptions ServerError class ServerException class
An error in the RMI server was thrown (e.g. out of memory) ServerException class When a method call to an RMI server throws a RemoteException, a ServerException is thrown. UnexpectedException class Used by clients to represent an exception thrown by the remote method but not declared in the RMI interface. Netprog Java RMI

5 java.rmi Exceptions AccessException class AlreadyBoundException class
Thrown by naming to indicate that a registry operation cannot be performed. AlreadyBoundException class A remote object is already bound to a registry entry. ConnectException class Inability to connect to a remote service, such as a registry. NotBoundException class Attempts to lookup or unbind a non-existent registry entry. Netprog Java RMI

6 java.rmi Exceptions UnknownHostException class
A client making a remote method request can’t resolve the hostname. StubNotFoundException class Stub not in local file system or externally (if using dynamic class loading). ConnectIOException class Inability to connect to a remote service to execute a remote method call. Netprog Java RMI

7 REMOTE METHOD INVOCATION – 6 STEPS
Create the remote interface Provide the implementation of the remote interface Compile the implementation class and create the stub and skeleton objects using the rmic tool Start the registry service by rmiregistry tool Create and start the remote application Create and start the client application

8 create the remote interface
For creating the remote interface, extend the Remote interface and declare the RemoteException with all the methods of the remote interface. Here, we are creating a remote interface that extends the Remote interface. There is only one method named add() and it declares RemoteException. import java.rmi.*;   public interface Adder extends Remote{   public int add(int x,int y)throws RemoteException;   }  

9 Provide the implementation of the remote interface
For providing the implementation of the Remote interface, we need to Either extend the UnicastRemoteObject class,or use the exportObject() method of the UnicastRemoteObject class In case, you extend the UnicastRemoteObject class, you must define a constructor that declares RemoteException. import java.rmi.*;   import java.rmi.server.*;   public class AdderRemote extends UnicastRemoteObject implements Adder{   AdderRemote()throws RemoteException{  

10 3) Create the stub and skeleton objects using the rmic tool.
rmic AdderRemote   4) Start the registry service by the rmiregistry tool Start rmiregistry

11 ACTIVATION MODELS Object activation is a mechanism for providing persistent references to objects and managing the execution of object implementations When an activatable remote object is accessed (via a method invocation) if that remote object is not currently executing, the system initiates the object's execution inside an appropriate JVM Active object is a remote object that is instantiated and exported in a JVM on some system. A passive object is one that is not yet instantiated (or exported)in a JVM, but which can be brought into an active state. Transforming a passive object into an active object is a process known as activation.

12 Activation Protocol In order to make a remote object that can be accessed via an activation identifier over time, a developer needs to:register an activation descriptor for the remote object, and include a special constructor in the object's class that the RMI system calls when it activates the activatable object The ActivationDesc Class The ActivationID Class The Activatable Class Activatable Class Methods Constructing an Activatable Remote Object Registering an Activation Descriptor Without Creating the Object

13

14

15 RMI CUSTOM SOCKETS Custom socket factories can be used to control how remote method invocations are communicated at the network level. For example, they can be used to set socket options, control address binding, control connection establishment (such as to require authentication), and to control data encoding (such as to add encryption or compression). When a remote object is exported, such as with the constructors or exportObject methods of java.rmi.server.UnicastRemoteObject  or java.rmi.activation.Activatable, then it is possible to specify a custom client socket factory (ajava.rmi.server.RMIClientSocketFactory instance) and a custom server socket factory (a java.rmi.server.RMIServerSocketFactory instance) to be used when communicating remote invocations for that remote object.

16 A client socket factory controls the creation of sockets used to initiate remote invocations and thus can be used to control how connections are established and the type of socket to use. A server socket factory controls the creation of server sockets used to receive remote invocations and thus can be used to control how incoming connections are listened for and accepted as well as the type of sockets to use for incoming connections.

17 Implementing a Custom Socket Factory
Below are three steps for implementing a pair of custom socket factory classes: Implement a custom ServerSocket and Socket. Implement a custom RMIClientSocketFactory. Implement a custom RMIServerSocketFactory.

18 Object Serialization To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializableif its class or any of its superclasses implements either the java.io.Serializableinterface or its subinterface, java.io.Externalizable.

19

20 java.io.Serializable interface
Serializable is a marker interface (has no data member and method). It is used to "mark" java classes so that objects of these classes may get certain capability. It must be implemented by the class whose object you want to persist. The String class and all the wrapper classes implements java.io.Serializable interface by default.

21 import java.io.Serializable;  
public class Student implements Serializable{    int id;    String name;    public Student(int id, String name) {     this.id = id;     this.name = name;    }   }  

22 import java.io.*; class Persist{ public static void main(String args[])throws Exception{ Student s1 =new Student(211,"ravi"); FileOutputStream fout=new FileOutputStream("f.txt"); ObjectOutputStream out=new ObjectOutputStream(fout); out.writeObject(s1); out.flush(); System.out.println("success"); } }

23 ObjectOutputStream class
The ObjectOutputStream class is used to write primitive data types and Java objects to an OutputStream. Only objects that support the java.io.Serializable interface can be written to streams. Important Methods Method Description 1) public final void writeObject(Object obj) throws IOException {} writes the specified object to the ObjectOutputStream. 2) public void flush() throws IOException {} flushes the current output stream. 3) public void close() throws IOException {} closes the current output stream.


Download ppt "RMI Packages Overview java.rmi java.rmi.server java.rmi.registry"

Similar presentations


Ads by Google