0% found this document useful (0 votes)
212 views

Remote Method Invocation (RMI) - Javatpoint

Uploaded by

Haddad Houssaine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views

Remote Method Invocation (RMI) - Javatpoint

Uploaded by

Haddad Houssaine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Home Java Programs OOPs String Exception Multithreading Collections JavaFX JSP Spring Spring Boot Projects

Basics of Java

OOPs Concepts
RMI (Remote Method Invocation) ← Prev Next →
Java String

Java Regex The RMI (Remote Method Invocation) is an API that


Remote Method Invocation (RMI)
Exception Handling provides a mechanism to create distributed application in
Understanding stub and skeleton
Java Inner classes java. The RMI allows an object to invoke methods on an
Java Multithreading object running in another JVM. stub
Java I/O skeleton
Java Networking The RMI provides remote communication between the
Requirements for the distributed
Java AWT applications using two objects stub and skeleton.
applications
Java Swing
Steps to write the RMI program
Understanding stub and skeleton
JavaFX
RMI Example
Java Applet
RMI uses stub and skeleton object for communication with
Java Reflection
the remote object.
Java Date

Java Conversion A remote object is an object whose method can be invoked from another JVM. Let's understand the
Java Collection stub and skeleton objects:
Java JDBC

SCROLL
⇧Java TO TOP
New Features stub
RMI
The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed
Java RMI
through it. It resides at the client side and represents the remote object. When the caller invokes
Internationalization
method on the stub object, it does the following tasks:
Interview Questions

1. It initiates a connection with remote Virtual Machine (JVM),


2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
5. It finally, returns the value to the caller.

skeleton

The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are
routed through it. When the skeleton receives the incoming request, it does the following tasks:

1. It reads the parameter for the remote method


2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.

In the Java 2 SDK, an stub protocol was introduced that eliminates the need for skeletons.

Understanding requirements for the distributed applications


If any application performs these tasks, it can be distributed application.

1. The application need to locate the remote method


2. It need to provide the communication with the remote objects, and
3. The application need to load the class definitions for the objects.

The RMI application have all these features, so it is called the distributed application.

Java RMI Example


The is given the 6 steps to write the RMI program.

1. Create the remote interface


2. Provide the implementation of the remote interface
3. Compile the implementation class and create the stub and skeleton objects using the rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application

RMI Example
In this example, we have followed all the 6 steps to create and run the rmi application. The client
application need only two files, remote interface and client application. In the rmi application, both
client and server interacts with the remote interface. The client application invokes methods on the
proxy object, RMI sends the request to the remote JVM. The return value is sent back to the proxy
object and then to the client application.

1) 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;  
}  

2) Provide the implementation of the remote interface

Now 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{  
super();  
}  
public int add(int x,int y){return x+y;}  
}  

3) create the stub and skeleton objects using the rmic tool.

Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI
compiler and creates stub and skeleton objects.

rmic AdderRemote  

4) Start the registry service by the rmiregistry tool

Now start the registry service by using the rmiregistry tool. If you don't specify the port number, it
uses a default port number. In this example, we are using the port number 5000.

rmiregistry 5000  

5) Create and run the server application

Now rmi services need to be hosted in a server process. The Naming class provides methods to get
and store the remote object. The Naming class provides 5 methods.

public static java.rmi.Remote lookup(java.lang.String) throws It returns the reference of


java.rmi.NotBoundException, java.net.MalformedURLException, the remote object.
java.rmi.RemoteException;

public static void bind(java.lang.String, java.rmi.Remote) throws It binds the remote object
java.rmi.AlreadyBoundException, java.net.MalformedURLException, with the given name.
java.rmi.RemoteException;

public static void unbind(java.lang.String) throws It destroys the remote


java.rmi.RemoteException, java.rmi.NotBoundException, object which is bound with
java.net.MalformedURLException; the given name.

public static void rebind(java.lang.String, java.rmi.Remote) throws It binds the remote object
java.rmi.RemoteException, java.net.MalformedURLException; to the new name.

public static java.lang.String[] list(java.lang.String) throws It returns an array of the


java.rmi.RemoteException, java.net.MalformedURLException; names of the remote
objects bound in the
registry.

In this example, we are binding the remote object by the name sonoo.

import java.rmi.*;  
import java.rmi.registry.*;  
public class MyServer{  
public static void main(String args[]){  
try{  
Adder stub=new AdderRemote();  
Naming.rebind("rmi://localhost:5000/sonoo",stub);  
}catch(Exception e){System.out.println(e);}  
}  
}  

6) Create and run the client application

At the client we are getting the stub object by the lookup() method of the Naming class and invoking
the method on this object. In this example, we are running the server and client applications, in the
same machine so we are using localhost. If you want to access the remote object from another
machine, change the localhost to the host name (or IP address) where the remote object is located.

import java.rmi.*;  
public class MyClient{  
public static void main(String args[]){  
try{  
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");  
System.out.println(stub.add(34,4));  
}catch(Exception e){}  
}  
}  

download this example of rmi

For running this rmi example,  
  
1) compile all the java files  
  
javac *.java  
  
2)create stub and skeleton object by rmic tool  
  
rmic AdderRemote  
  
3)start rmi registry in one command prompt  
  
rmiregistry 5000  
  
4)start the server in another command prompt  
  
java MyServer  
  
5)start the client application in another command prompt  
  
java MyClient  

Output of this RMI example

Meaningful example of RMI application with database

Consider a scenario, there are two applications running in different machines. Let's say MachineA and
MachineB, machineA is located in United States and MachineB in India. MachineB want to get list of
all the customers of MachineA application.

Let's develop the RMI application by following the steps.

1) Create the table

First of all, we need to create the table in the database. Here, we are using Oracle10 database.

2) Create Customer class and Remote interface

File: Customer.java

package com.javatpoint;  
public class Customer implements java.io.Serializable{  
    private int acc_no;  
    private String firstname,lastname,email;  
    private float amount;  
//getters and setters  
}  

Note: Customer class must be Serializable.

File: Bank.java

package com.javatpoint;  
import java.rmi.*;  
import java.util.*;  
interface Bank extends Remote{  
public List<Customer> getCustomers()throws RemoteException;  
}  

3) Create the class that provides the implementation of Remote interface

File: BankImpl.java

package com.javatpoint;  
import java.rmi.*;  
import java.rmi.server.*;  
import java.sql.*;  
import java.util.*;  
class BankImpl extends UnicastRemoteObject implements Bank{  
BankImpl()throws RemoteException{}  
  
public List<Customer> getCustomers(){  
List<Customer> list=new ArrayList<Customer>();  
try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"
PreparedStatement ps=con.prepareStatement("select * from customer400");  
ResultSet rs=ps.executeQuery();  
  
while(rs.next()){  
Customer c=new Customer();  
c.setAcc_no(rs.getInt(1));  
c.setFirstname(rs.getString(2));  
c.setLastname(rs.getString(3));  
c.setEmail(rs.getString(4));  
c.setAmount(rs.getFloat(5));  
list.add(c);  
}  
  
con.close();  
}catch(Exception e){System.out.println(e);}  
return list;  
}//end of getCustomers()  
}  

4) Compile the class rmic tool and start the registry service by rmiregistry tool

5) Create and run the Server

File: MyServer.java

package com.javatpoint;  
import java.rmi.*;  
public class MyServer{  
public static void main(String args[])throws Exception{  
Remote r=new BankImpl();  
Naming.rebind("rmi://localhost:6666/javatpoint",r);  
}}  

6) Create and run the Client

File: MyClient.java

package com.javatpoint;  
import java.util.*;  
import java.rmi.*;  
public class MyClient{  
public static void main(String args[])throws Exception{  
Bank b=(Bank)Naming.lookup("rmi://localhost:6666/javatpoint");  
  
List<Customer> list=b.getCustomers();  
for(Customer c:list){  
System.out.println(c.getAcc_no()+" "+c.getFirstname()+" "+c.getLastname()  
+" "+c.getEmail()+" "+c.getAmount());  
}  
  
}}  

download this example of rmi with database

← Prev Next →

Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

Help Others, Please Share

Learn Latest Tutorials

Splunk tutorial SPSS tutorial Swagger T-SQL tutorial


tutorial
Splunk SPSS Transact-SQL
Swagger

Tumblr tutorial React tutorial Regex tutorial Reinforcement


learning tutorial
Tumblr ReactJS Regex
Reinforcement
Learning

R Programming RxJS tutorial React Native Python Design


tutorial tutorial Patterns
RxJS
R Programming React Native Python Design
Patterns

Python Pillow Python Turtle Keras tutorial


tutorial tutorial
Keras
Python Pillow Python Turtle

Preparation

Aptitude Logical Verbal Ability Interview


Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions

Company
Interview
Questions
Company Questions

Trending Technologies

Artificial AWS Tutorial Selenium Cloud


Intelligence tutorial Computing
AWS
Tutorial tutorial
Selenium
Artificial Cloud Computing
Intelligence

Hadoop tutorial ReactJS Data Science Angular 7


Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7

Blockchain Git Tutorial Machine DevOps


Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps

B.Tech / MCA

DBMS tutorial Data Structures DAA tutorial Operating


tutorial System tutorial
DBMS DAA
Data Structures Operating System

Computer Compiler Computer Discrete


Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics

Ethical Hacking Computer Software html tutorial


Tutorial Graphics Tutorial Engineering
Web Technology
Tutorial
Ethical Hacking Computer Graphics
Software
Engineering

Cyber Security Automata C Language C++ tutorial


tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming

Java tutorial .Net Python tutorial List of


Framework Programs
Java Python
tutorial
Programs
.Net

Control Data Mining Data


Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse

Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.

Website Designing
Website Development
Java Development
PHP Development
WordPress
Graphic Designing
Logo
Digital Marketing
On Page and Off Page SEO
PPC
Content Development
Corporate Training
Classroom and Online Training
Data Entry

Training For College Campus


JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter

LEARN TUTORIALS OUR WEBSITES OUR SERVICES CONTACT

Learn Java Javatpoint.com Website Development Address: G-13, 2nd Floor, Sec-3
Learn Data Structures Hindi100.com Android Development Noida, UP, 201301, India
Learn C Programming Lyricsia.com
Website Designing Contact No: 0120-4256464, 9990449935
Learn C++ Tutorial Quoteperson.com
Learn C# Tutorial Jobandplacement.com Digital Marketing Contact Us
Learn PHP Tutorial Summer Training Subscribe Us
Learn HTML Tutorial Privacy Policy
Industrial Training
Learn JavaScript Tutorial Sitemap
Learn jQuery Tutorial College Campus Training
Learn Spring Tutorial About Me

© Copyright 2011-2021 www.javatpoint.com. All rights reserved. Developed by JavaTpoint.

You might also like