0% found this document useful (0 votes)
85 views13 pages

Remote Method Invocation: Working of An RMI Application

Remote Method Invocation (RMI) allows a Java object running on one machine to invoke methods of a Java object running on another machine, enabling the creation of distributed applications. RMI uses stubs and skeletons, where the stub represents the remote object on the client side and the skeleton represents the client on the server side. The stub handles method calls by passing details through a serialization stream to the remote reference layer, while the skeleton sends parameters to the method and returns values and exceptions.

Uploaded by

Manoj Sundar G
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views13 pages

Remote Method Invocation: Working of An RMI Application

Remote Method Invocation (RMI) allows a Java object running on one machine to invoke methods of a Java object running on another machine, enabling the creation of distributed applications. RMI uses stubs and skeletons, where the stub represents the remote object on the client side and the skeleton represents the client on the server side. The stub handles method calls by passing details through a serialization stream to the remote reference layer, while the skeleton sends parameters to the method and returns values and exceptions.

Uploaded by

Manoj Sundar G
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Remote Method Invocation

Which allows a Java object that executes on one machine to invoke a method of a
Java object that executes on another Machine. In this way RMI supports Distributed
applications.

DCOM -> Distributed component Object model


CORBA -> Common Object Request Broker Architecture.

Java C++

Working of an RMI Application

* Server side
* Client Side.

Server side

Method definitions.

Three files are created for server


(ie)
1. Interface file which contains the declaration of the method. That interface can
be extend with the class of Remote (ie) the methods are accessed using remote object.
The methods also declared with the exception of RemoteException

2. A Class file that implements the interface. This class extends the
UnicastRemoteObject class.

3. The third file creates the object of the class and binds the object in RMI
Registry.

Client

Accessing the method that is defined in the server.

A file that creates a class that access the remote object through rmi registry.

Starting RMI Registry

jdk\bin> rmiregistry

Understanding RMI Architecture


Application Layer -> Responsible for actual representation of the client and server
implementation.

Stub/Skeleton Layer -> The stub acts as a representative of a server on the client
side and skeleton acts as a representative of a client on the server side.

Stub Class

It is responsible for initiating a call to the remote object that it represents by


way of the remote reference layer. The stub is responsible for passing the method details
such as its name, parameters and the return type ofthe remote reference layer through a
marshall stream. A marshal stream is simply a stream object that is used to transport
parameters, exceptions and errors needed for method dispatch and returning the results.

Skeleton Class

The skeleton is responsible for sending parameter to the method


implementation and for sending return values and exception back to the client that made
the call.

Remote Reference layer

The remote reference layer acts as a layer of abstraction between the stub
and skeleton classes and the communication protocols that are handled by the transport
layer.
Transport layer

This layer is responsible for setting up connection and handling the


transport of data from one machine to another.

Client
Application
Server Appln
Application Layer

Stub/ Skeleto
Stub Skeleton n
Layer

Remote Reference Layer

Transport Layer
Programs

SimpleIntf.java

import java.rmi.*;

public interface SimpleIntf extends Remote


{
public int add(int a,int b) throws RemoteException;
public int sub(int a1,int b1) throws RemoteException;
}

SimpleImpl.java

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

public class SimpleImpl extends UnicastRemoteObject implements SimpleIntf


{
public SimpleImpl() throws RemoteException
{}

public int add(int a,int b) throws RemoteException


{
return a+b;
}

public int sub(int a1,int b1) throws RemoteException


{
return a1-b1;
}
}

Server.java

import java.rmi.*;

class Server
{
public static void main(String args[]) throws Exception
{
SimpleImpl s1 = new SimpleImpl();
System.out.println("Server Initializing....");
Naming.rebind("sys1",s1);
System.out.println("Server Registered...");
}
}

Client.java

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

class Client
{
public static void main(String args[]) throws Exception
{
String url = "rmi://system6/sys1";
SimpleIntf intf1 = (SimpleIntf) Naming.lookup(url);
int c = intf1.add(10,20);
int d = intf1.sub(40,30);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

javac SimpleIntf.java
javac SimpleImpl.java
rmic SimpleImpl
javac Server.java
javac Client.java

First Command Prompt

>rmiregistry

Second Command Prompt

>java Server

Third Command Prompt


>java Client

ChatIntf.java

import java.rmi.*;

public interface ChatIntf extends Remote


{
public String get() throws RemoteException;
public void set(String str) throws RemoteException;
}

ChatImpl.java

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

public class ChatImpl extends UnicastRemoteObject implements ChatIntf


{
String s = "";

public ChatImpl() throws RemoteException


{}

public String get() throws RemoteException


{
return s;
}

public void set(String str) throws RemoteException


{
s = s + str + "\n";
}
}

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

public class ChatServer


{
public static void main(String args[]) throws Exception
{
ChatImpl impl = new ChatImpl();
System.out.println("Initializing server....");
Naming.rebind("chat",impl);
System.out.println("Registered....");
}
}

ChatClient.java

import java.rmi.*;
import java.rmi.server.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class ChatClient extends Applet implements ActionListener


{
Label l1,l2;
TextField tf,tf1;
TextArea ta;
ChatIntf ci;
String Name="";

public void init()


{
try
{
ci = (ChatIntf)

Naming.lookup("rmi://system16/chat");
}
catch(Exception e)
{
System.out.println("Lookup Error");
}
l1 = new Label("Username : ");
l2 = new Label("Message : ");
tf = new TextField(20);
tf1 = new TextField(30);
ta = new TextArea();
add(l1);
add(tf);
add(l2);
add(tf1);
add(ta);

tf.addActionListener(this);
tf1.addActionListener(this);

receiveClient r1 = new receiveClient(this,ci);


r1.start();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == tf)
{
String x = tf.getText();
try
{
Name = Name + x + " : ";
ci.set(Name);
}catch(Exception e) {}
tf.setText("");
}
else if(ae.getSource() == tf1)
{
Name += tf1.getText();
try
{
ci.set(Name);
}catch(Exception e) {}
tf1.setText("");
}
}
}

class receiveClient extends Thread


{
ChatIntf ci;
ChatClient cl;

public receiveClient(ChatClient cl,ChatIntf ci)


{
this.cl = cl;
this.ci = ci;
}

public void run()


{
String s = null;
while(true)
{
try
{
s = ci.get();
cl.ta.setText(s);
Thread.sleep(2000);
}catch(Exception e) {}
}
}
}

//<applet code=ChatClient width=400 height=400></applet>

DBIntf.java

import java.rmi.*;
import java.io.*;
import java.sql.*;

public interface DBIntf extends Remote


{
public void adding(String query) throws
RemoteException,SQLException,ClassNotFoundException;
public void update(String query) throws
RemoteException,SQLException,ClassNotFoundException;
public void delete(String query) throws
RemoteException,SQLException,ClassNotFoundException;
public String retrieve(String query) throws
RemoteException,SQLException,ClassNotFoundException;
}

DBImpl.java

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

public class DBImpl extends UnicastRemoteObject implements DBIntf


{
Connection connection;
Statement statement;
PreparedStatement ps;

public DBImpl() throws RemoteException


{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection =
DriverManager.getConnection("jdbc:odbc:mysql","sa","killer");
statement = connection.createStatement();
}catch(ClassNotFoundException e) {}
catch(SQLException ex){}
}

public void adding(String query) throws


RemoteException,SQLException,ClassNotFoundException
{
ps = connection.prepareStatement(query);
ps.execute();
}

public void update(String query) throws RemoteException, SQLException,


ClassNotFoundException
{
ps = connection.prepareStatement(query);
ps.execute();
}
public void delete(String query) throws RemoteException,SQLException,
ClassNotFoundException
{
ps = connection.prepareStatement(query);
ps.execute();
}

public String retrieve(String query) throws RemoteException, SQLException,


ClassNotFoundException
{
String res = new String();
ResultSet rs = statement.executeQuery(query);
ResultSetMetaData metadata = rs.getMetaData();
int columns = metadata.getColumnCount();
while(rs.next())
{
for(int i=1;i<=columns;i++)
{
res += rs.getString(i);
}
}
return res;
}

public static void main(String args[]) throws Exception


{
DBImpl impl = new DBImpl();
Naming.rebind("DBase",impl);
System.out.println("Service bound");
}
}

DBClient.java

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

public class DBClient


{
DBIntf intf;
public DBClient() throws RemoteException
{
try{
intf = (DBIntf) Naming.lookup("rmi://system6/DBase");
System.out.println("DBase Found");
}catch(Exception e)
{
System.out.println("Exception : " + e);
}
}

public void add1() throws


RemoteException,SQLException,ClassNotFoundException
{
intf.adding("Insert into students values(\'ABI\',\'B.Com\')");
System.out.println("Record Added");
}

public void edit() throws RemoteException, SQLException,


ClassNotFoundException
{
intf.update("Update students set course=\'M.Com\' where sname=\'ABI\'");
System.out.println("Record Updated");
}
public void retr() throws RemoteException, SQLException,
ClassNotFoundException
{
String str = "Select * from students where sname=\'ABI\'";
System.out.println("The Result is : " + intf.retrieve(str));
}

public void del() throws RemoteException, SQLException,


ClassNotFoundException
{
String str = "Delete from students where sname=\'aaaa\'";
intf.delete(str);
System.out.println("Record Deleted");
}

public static void main(String args[])


{
try
{
DBClient dbc = new DBClient();
dbc.add1();
dbc.edit();
dbc.retr();
// dbc.del();
}catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
}

You might also like