SlideShare a Scribd company logo
1
1. Develop RMI Application with database.
The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java. The RMI allows an object to invoke methods on an object
running in another JVM.
The RMI provides remote communication between the applications using two
objects stub and skeleton.
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's
understand the stub and skeleton objects:
stub
The stub is an object, acts as a gateway for the client side. All the outgoing requests are
routed through it. It resides at the client side and represents the remote object. When the
caller invokes method on the stub object, it does the following tasks:
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.
2
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 has 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 needs only two files, remote interface and client application. In the rmi
application, both client and server interact 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.
3
a) 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.
1. import java.rmi.*;
2. public interface Adder extends Remote
3. {
4. public int add(int x,int y)throws RemoteException;
5. }
b) 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
o Either extend the UnicastRemoteObject class,
o or use the exportObject() method of the UnicastRemoteObject class
In case, you extend the UnicastRemoteObject class, you must define a constructor that
declares RemoteException.
1. import java.rmi.*;
2. import java.rmi.server.*;
3. public class AdderRemote extends UnicastRemoteObject implements Adder
4. {
5. AdderRemote()throws RemoteException
6. {
7. super();
8. }
9. public int add(int x,int y)
10. {
11. return x+y;
12. }
13. }
4
c) 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.
1. rmic AdderRemote
d) 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.
1. rmiregistry 5000
e) 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
java.rmi.NotBoundException,
java.net.MalformedURLException,
java.rmi.RemoteException;
It returns the reference of the remote
object.
public static void bind(java.lang.String,
java.rmi.Remote) throws
java.rmi.AlreadyBoundException,
java.net.MalformedURLException,
java.rmi.RemoteException;
It binds the remote object with the given
name.
public static void unbind(java.lang.String) throws
java.rmi.RemoteException,
java.rmi.NotBoundException,
java.net.MalformedURLException;
It destroys the remote object which is
bound with the given name.
5
public static void rebind(java.lang.String,
java.rmi.Remote) throws
java.rmi.RemoteException,
java.net.MalformedURLException;
It binds the remote object to the new
name.
public static java.lang.String[]
list(java.lang.String) throws
java.rmi.RemoteException,
java.net.MalformedURLException;
It returns an array of the names of the
remote objects bound in the registry.
In this example, we are binding the remote object by the name sonoo.
1. import java.rmi.*;
2. import java.rmi.registry.*;
3. public class MyServer
4. {
5. public static void main(String args[])
6. {
7. Try
8. {
9. Adder stub=new AdderRemote();
10. Naming.rebind("rmi://localhost:5000/sonoo",stub);
11. }
12. catch(Exception e)
13. {
14. System.out.println(e);
15. }
16. }
17. }
e) 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
6
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.
1. import java.rmi.*;
2. public class MyClient
3. {
4. public static void main(String args[])
5. {
6. Try
7. {
8. Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
9. System.out.println(stub.add(34,4));
10. }
11. catch(Exception e)
12. {
13. }
14. }
15. }
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
7
java MyClient
Example of RMI application with database
8
Consider a scenario, there are two applications running in different machines. Let's say
Machine A and Machine B, machine A is located in United States and Machine B in India.
Machine B want to get list of all the customers of Machine A 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
1. package com.javatpoint;
2. public class Customer implements java.io.Serializable
3. {
4. private int acc_no;
5. private String firstname,lastname,email;
6. private float amount;
7. //getters and setters
8. }
9
File: Bank.java
1. package com.javatpoint;
2. import java.rmi.*;
3. import java.util.*;
4. interface Bank extends Remote
5. {
6. public List<Customer> getCustomers()throws RemoteException;
7. }
3) Create the class that provides the implementation of Remote interface
File: BankImpl.java
1. package com.javatpoint;
2. import java.rmi.*;
3. import java.rmi.server.*;
4. import java.sql.*;
5. import java.util.*;
6. class BankImpl extends UnicastRemoteObject implements Bank
7. {
8. BankImpl()throws RemoteException
9. {
10. }
11. public List<Customer> getCustomers()
12. {
13. List<Customer> list=new ArrayList<Customer>();
14. Try
15. {
16. Class.forName("oracle.jdbc.driver.OracleDriver");
17. Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","syste
m","oracle");
18. PreparedStatement ps=con.prepareStatement("select * from customer400");
19. ResultSet rs=ps.executeQuery();
20.
21. while(rs.next()){
22. Customer c=new Customer();
23. c.setAcc_no(rs.getInt(1));
24. c.setFirstname(rs.getString(2));
25. c.setLastname(rs.getString(3));
26. c.setEmail(rs.getString(4));
27. c.setAmount(rs.getFloat(5));
28. list.add(c);
29. }
30. con.close();
31. }
32. catch(Exception e)
10
33. {
34. Syste
m.ou
t.prin
tln(e)
;
35. }
36. retur
n list
;
37. }//end of getCustomers()
38. }
4) Compile the class rmic tool and start the registry service by rmiregistry tool
5) Create and run the Server
File: MyServer.java
1. package com.javatpoint;
2. import java.rmi.*;
3. public class MyServer
4. {
5. public static void main(String args[])throws Exception
6. {
7. Remote r=new BankImpl();
8. Naming.rebind("rmi://localhost:6666/javatpoint",r);
9. }
10. }
11
6) Create and run the Client
File: MyClient.java
1. package com.javatpoint;
2. import java.util.*;
3. import java.rmi.*;
4. public class MyClient
5. {
6. public static void main(String args[])throws Exception
7. {
8. Bank b=(Bank)Naming.lookup("rmi://localhost:6666/javatpoint");
9.
10. List<Customer> list=b.getCustomers();
11. for(Customer c:list)
12. {
13. System.out.println(c.getAcc_no()+" "+c.getFirstname()+" "+c.getLastname()
+" "+c.getEmail()+" "+c.getAmount());
14. }
15. }
16. }

More Related Content

PPTX
Remote method invocation
Veni7
 
PDF
Remote Method Invocation in JAVA
Jalpesh Vasa
 
PPTX
Introduction To Rmi
SwarupKulkarni
 
DOCX
Remote Method Invocation
Sonali Parab
 
PPTX
Remote Method Invocation (Java RMI)
Sonali Parab
 
PDF
Remote Method Invocation, Advanced programming
Gera Paulos
 
PPTX
Rmi architecture
Maulik Desai
 
PDF
JavaRMI, JAVA RPC , INTRO , DESCRIPTION , EXPLAINED.pdf
ishankumra13579
 
Remote method invocation
Veni7
 
Remote Method Invocation in JAVA
Jalpesh Vasa
 
Introduction To Rmi
SwarupKulkarni
 
Remote Method Invocation
Sonali Parab
 
Remote Method Invocation (Java RMI)
Sonali Parab
 
Remote Method Invocation, Advanced programming
Gera Paulos
 
Rmi architecture
Maulik Desai
 
JavaRMI, JAVA RPC , INTRO , DESCRIPTION , EXPLAINED.pdf
ishankumra13579
 

Similar to ADB Lab Manual.docx (20)

PDF
Java rmi tutorial
HarikaReddy115
 
PDF
Java rmi
Fazlur Rahman
 
PPTX
Rmi
Jafar Nesargi
 
PPTX
Remote method invocatiom
sakthibalabalamuruga
 
PPTX
Rmi
Vijay Kiran
 
DOCX
remote method invocation
Arun Nair
 
PPTX
Remote method invocation
Dew Shishir
 
DOCX
Report on mini project(Student database handling using RMI)
shraddha mane
 
PPTX
Java RMI
Ankit Desai
 
PPT
Distributed Objects and JAVA
elliando dias
 
PDF
Distributed Programming (RMI)
Ravi Kant Sahu
 
PPT
Rmi
phanleson
 
PPT
Rmi
leminhvuong
 
PDF
Rmi
vantinhkhuc
 
PPTX
DS
Verma Mukesh
 
PDF
Remote Method Invocation
ashishspace
 
PDF
Module 3 remote method invocation-2
Ankit Dubey
 
PDF
RMI (Remote Method Invocation)
Thesis Scientist Private Limited
 
Java rmi tutorial
HarikaReddy115
 
Java rmi
Fazlur Rahman
 
Remote method invocatiom
sakthibalabalamuruga
 
remote method invocation
Arun Nair
 
Remote method invocation
Dew Shishir
 
Report on mini project(Student database handling using RMI)
shraddha mane
 
Java RMI
Ankit Desai
 
Distributed Objects and JAVA
elliando dias
 
Distributed Programming (RMI)
Ravi Kant Sahu
 
Rmi
phanleson
 
Remote Method Invocation
ashishspace
 
Module 3 remote method invocation-2
Ankit Dubey
 
RMI (Remote Method Invocation)
Thesis Scientist Private Limited
 
Ad

Recently uploaded (20)

PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PDF
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
Zero Carbon Building Performance standard
BassemOsman1
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Ad

ADB Lab Manual.docx

  • 1. 1 1. Develop RMI Application with database. The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM. The RMI provides remote communication between the applications using two objects stub and skeleton. RMI uses stub and skeleton object for communication with the remote object. A remote object is an object whose method can be invoked from another JVM. Let's understand the stub and skeleton objects: stub The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks: 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.
  • 2. 2 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 has 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 needs only two files, remote interface and client application. In the rmi application, both client and server interact 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.
  • 3. 3 a) 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. 1. import java.rmi.*; 2. public interface Adder extends Remote 3. { 4. public int add(int x,int y)throws RemoteException; 5. } b) 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 o Either extend the UnicastRemoteObject class, o or use the exportObject() method of the UnicastRemoteObject class In case, you extend the UnicastRemoteObject class, you must define a constructor that declares RemoteException. 1. import java.rmi.*; 2. import java.rmi.server.*; 3. public class AdderRemote extends UnicastRemoteObject implements Adder 4. { 5. AdderRemote()throws RemoteException 6. { 7. super(); 8. } 9. public int add(int x,int y) 10. { 11. return x+y; 12. } 13. }
  • 4. 4 c) 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. 1. rmic AdderRemote d) 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. 1. rmiregistry 5000 e) 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 java.rmi.NotBoundException, java.net.MalformedURLException, java.rmi.RemoteException; It returns the reference of the remote object. public static void bind(java.lang.String, java.rmi.Remote) throws java.rmi.AlreadyBoundException, java.net.MalformedURLException, java.rmi.RemoteException; It binds the remote object with the given name. public static void unbind(java.lang.String) throws java.rmi.RemoteException, java.rmi.NotBoundException, java.net.MalformedURLException; It destroys the remote object which is bound with the given name.
  • 5. 5 public static void rebind(java.lang.String, java.rmi.Remote) throws java.rmi.RemoteException, java.net.MalformedURLException; It binds the remote object to the new name. public static java.lang.String[] list(java.lang.String) throws java.rmi.RemoteException, java.net.MalformedURLException; It returns an array of the names of the remote objects bound in the registry. In this example, we are binding the remote object by the name sonoo. 1. import java.rmi.*; 2. import java.rmi.registry.*; 3. public class MyServer 4. { 5. public static void main(String args[]) 6. { 7. Try 8. { 9. Adder stub=new AdderRemote(); 10. Naming.rebind("rmi://localhost:5000/sonoo",stub); 11. } 12. catch(Exception e) 13. { 14. System.out.println(e); 15. } 16. } 17. } e) 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
  • 6. 6 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. 1. import java.rmi.*; 2. public class MyClient 3. { 4. public static void main(String args[]) 5. { 6. Try 7. { 8. Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo"); 9. System.out.println(stub.add(34,4)); 10. } 11. catch(Exception e) 12. { 13. } 14. } 15. } 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
  • 7. 7 java MyClient Example of RMI application with database
  • 8. 8 Consider a scenario, there are two applications running in different machines. Let's say Machine A and Machine B, machine A is located in United States and Machine B in India. Machine B want to get list of all the customers of Machine A 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 1. package com.javatpoint; 2. public class Customer implements java.io.Serializable 3. { 4. private int acc_no; 5. private String firstname,lastname,email; 6. private float amount; 7. //getters and setters 8. }
  • 9. 9 File: Bank.java 1. package com.javatpoint; 2. import java.rmi.*; 3. import java.util.*; 4. interface Bank extends Remote 5. { 6. public List<Customer> getCustomers()throws RemoteException; 7. } 3) Create the class that provides the implementation of Remote interface File: BankImpl.java 1. package com.javatpoint; 2. import java.rmi.*; 3. import java.rmi.server.*; 4. import java.sql.*; 5. import java.util.*; 6. class BankImpl extends UnicastRemoteObject implements Bank 7. { 8. BankImpl()throws RemoteException 9. { 10. } 11. public List<Customer> getCustomers() 12. { 13. List<Customer> list=new ArrayList<Customer>(); 14. Try 15. { 16. Class.forName("oracle.jdbc.driver.OracleDriver"); 17. Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","syste m","oracle"); 18. PreparedStatement ps=con.prepareStatement("select * from customer400"); 19. ResultSet rs=ps.executeQuery(); 20. 21. while(rs.next()){ 22. Customer c=new Customer(); 23. c.setAcc_no(rs.getInt(1)); 24. c.setFirstname(rs.getString(2)); 25. c.setLastname(rs.getString(3)); 26. c.setEmail(rs.getString(4)); 27. c.setAmount(rs.getFloat(5)); 28. list.add(c); 29. } 30. con.close(); 31. } 32. catch(Exception e)
  • 10. 10 33. { 34. Syste m.ou t.prin tln(e) ; 35. } 36. retur n list ; 37. }//end of getCustomers() 38. } 4) Compile the class rmic tool and start the registry service by rmiregistry tool 5) Create and run the Server File: MyServer.java 1. package com.javatpoint; 2. import java.rmi.*; 3. public class MyServer 4. { 5. public static void main(String args[])throws Exception 6. { 7. Remote r=new BankImpl(); 8. Naming.rebind("rmi://localhost:6666/javatpoint",r); 9. } 10. }
  • 11. 11 6) Create and run the Client File: MyClient.java 1. package com.javatpoint; 2. import java.util.*; 3. import java.rmi.*; 4. public class MyClient 5. { 6. public static void main(String args[])throws Exception 7. { 8. Bank b=(Bank)Naming.lookup("rmi://localhost:6666/javatpoint"); 9. 10. List<Customer> list=b.getCustomers(); 11. for(Customer c:list) 12. { 13. System.out.println(c.getAcc_no()+" "+c.getFirstname()+" "+c.getLastname() +" "+c.getEmail()+" "+c.getAmount()); 14. } 15. } 16. }