SlideShare a Scribd company logo
RMI
By: Jalpesh Vasa
What is RMI(Remote Method Invocation) ?
 A true distributed computing application interface for Java, written to provide easy
access to objects existing on remote virtual machines
 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.
 Remote objects can be treated similarly to local objects
 Handles marshaling, transportation, and garbage collection of the remote objects
 Became part of the JDK with version 1.1
What is RMI not?
 Not language independent
 Limited only to Java
 Interfaces to remote objects are defined using ordinary Java interfaces (rather
than having to use a special purpose interface definition language)
 Can provide more advanced features like serialization and security
Support for the interface
Interface Support Layers
 Stub/skeleton layer
 Responsible for managing the remote object interface between the client and
server
 Remote reference layer
 Responsible for managing the "liveliness" of the remote objects
 Manages the communication between the client/server and virtual machines
 Transport layer
 Actual network/communication layer that is used to send the information
between the client and server over the wire
 Currently TCP/IP based
 Uses serialization and remote procedure call to send information back
and forth between remote objects
The General RMI Architecture
 The server must first bind its
name to the registry
 The client lookup the server
name in the registry to establish
remote references.
 The Stub serializing the
parameters to skeleton, the
skeleton invoking the remote
method and serializing the result
back to the stub.
R M I S e rv e r
s k e le to n
s tu b
R M I C lie n t
R e g is try
b in d
lo o k u pre tu rn c a ll
L o c a l M a c h in e
R e m o te M a c h in e
The Stub and Skeleton
 A client invokes a remote method, the call is first forwarded to stub.
 The stub is responsible for sending the remote call over to the server-
side skeleton
 The stub opening a socket to the remote server, marshalling the object
parameters and forwarding the data stream to the skeleton.
 A skeleton contains a method that receives the remote calls, unmarshals
the parameters, and invokes the actual remote object implementation.
Stub
RMI Client RMI Server
skeleton
return
call
The Stub and Skeleton (Cont.)
 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:
 It initiates a connection with remote Virtual Machine (JVM),
 It writes and transmits (marshals) the parameters to the remote Virtual Machine
(JVM),
 It waits for the result
 It reads (unmarshals) the return value or exception, and
 It finally, returns the value to the caller.
The Stub and Skeleton (Cont.)
 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:
 It reads the parameter for the remote method
 It invokes the method on the actual remote object, and
 It writes and transmits (marshals) the result to the caller.
Steps for Developing an RMI System
1. Define the remote interface
2. Develop the remote object by implementing the remote interface.
3. Develop the client program.
4. Compile the Java source files.
5. Generate the client stubs and server skeletons objects using the rmic tool.
6. Start the RMI registry service by rmiregistry tool.
7. Start the remote server objects.
8. Run the client
Step 1: Defining the Remote Interface
 To create an RMI application, the first step is the defining of a remote
interface between the client and server objects.
/* SampleServer.java */
import java.rmi.*;
public interface SampleServer extends Remote
{
public int sum(int a,int b) throws RemoteException;
}
Step 2: Develop the remote object and its
interface
 The server is a simple unicast remote server.
 Create server by extending java.rmi.server.UnicastRemoteObject.
 The server uses the RMISecurityManager to protect its resources
while engaging in remote communication.
/* SampleServerImpl.java */
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SampleServerImpl extends UnicastRemoteObject
implements SampleServer
{
SampleServerImpl() throws RemoteException
{
super();
}
Step 2: Develop the remote object and its
interface
Implement the remote methods
/* SampleServerImpl.java */
public int sum(int a,int b) throws RemoteException
{
return a + b;
}
}
The server must bind its name to the registry, the client will
look up the server name.
Use java.rmi.Naming class to bind the server name to
registry. In this example the name call “SAMPLE-SERVER”.
In the main method of your server object, the RMI security
manager is created and installed.
Step 2: Develop the remote object and its
interface
/* SampleServerImpl.java */
public static void main(String args[])
{
try
{
System.setSecurityManager(new RMISecurityManager());
//set the security manager
//create a local instance of the object
SampleServerImpl Server = new SampleServerImpl();
//put the local instance in the registry
Naming.rebind("SAMPLE-SERVER" , Server);
System.out.println("Server waiting.....");
}
catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString()); }
catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString()); }
}
Step 3: Develop the client program
 In order for the client object to invoke methods on the server, it
must first look up the name of server in the registry. You use the
java.rmi.Naming class to lookup the server name.
 The server name is specified as URL in the from (
rmi://host:port/name )
 Default RMI port is 1099.
 The name specified in the URL must exactly match the name that
the server has bound to the registry. In this example, the name is
“SAMPLE-SERVER”
 The remote method invocation is programmed using the remote
interface name (remoteObject) as prefix and the remote method
name (sum) as suffix.
Step 3: Develop the client program
import java.rmi.*;
import java.rmi.server.*;
public class SampleClient
{
public static void main(String[] args)
{
// set the security manager for the client
System.setSecurityManager(new RMISecurityManager());
//get the remote object from the registry
try
{
System.out.println("Security Manager loaded");
String url = "//localhost/SAMPLE-SERVER";
SampleServer remoteObject = (SampleServer)Naming.lookup(url);
System.out.println("Got remote object");
System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) );
}
catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString()); }
catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString()); }
catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
}
Step 4 & 5: Compile the Java source files
& Generate the client stubs and server
skeletons
Assume the program compile and executing at elpis on ~/rmi
Once the interface is completed, you need to generate stubs
and skeleton code. The RMI system provides an RMI compiler
(rmic) that takes your generated interface class and
procedures stub code on its self.
elpis:~/rmi> set CLASSPATH=”~/rmi”
elpis:~/rmi> javac SampleServer.java
elpis:~/rmi> javac SampleServerImpl.java
elpis:~/rmi> rmic SampleServerImpl
elpis:~/rmi> javac SampleClient.java
Step 6: Start the RMI registry
The RMI applications need install to Registry. And the
Registry must start manual by call rmiregisty.
The rmiregistry us uses port 1099 by default. You can
also bind rmiregistry to a different port by indicating the new
port number as : rmiregistry <new port>
elpis:~/rmi> rmiregistry
Remark: On Windows, you have to type in from the command
line:
> start rmiregistry
Steps 7 & 8: Start the remote server
objects & Run the client
Once the Registry is started, the server can be started and will
be able to store itself in the Registry.
Because of the grained security model in Java 2.0, you must
setup a security policy for RMI by set
java.security.policy to the file policy.all
elpis:~/rmi> java –Djava.security.policy=policy.all
SampleServerImpl
elpis:~/rmi> java –Djava.security.policy=policy.all
SampleClient
Example-2
 Create a service that can calculate the square of a number,
and the power of two numbers (238 for example).
Writing an interface
import java.math.BigInteger;
import java.rmi.*;
// PowerService Interface
//
// Interface for a RMI service that calculates powers
public interface PowerService extends java.rmi.Remote
{
// Calculate the square of a number
public BigInteger square ( int number )
throws RemoteException;
// Calculate the power of a number
public BigInteger power ( int num1, int num2)
throws RemoteException;
}
Implementing the interface
import java.math.*;
import java.rmi.*;
import java.rmi.server.*;
//
// PowerServiceServer
//
// Server for a RMI service that calculates powers
//
public class PowerServiceServer extends UnicastRemoteObject
implements PowerService
{
public PowerServiceServer () throws RemoteException
{
super();
}
Implementing the interface
// Calculate the square of a number
public BigInteger square ( int number )
throws RemoteException
{
String numrep = String.valueOf(number);
BigInteger bi = new BigInteger (numrep);
// Square the number
bi.multiply(bi);
return (bi);
}
// Calculate the power of a number
public BigInteger power ( int num1, int num2)
throws RemoteException
{
String numrep = String.valueOf(num1);
BigInteger bi = new BigInteger (numrep);
bi = bi.pow(num2);
return bi;
}
Implementing the interface
public static void main ( String args[] ) throws Exception
{
// Assign a security manager, in the event that dynamic
// classes are loaded
if (System.getSecurityManager() == null)
System.setSecurityManager ( new RMISecurityManager() );
// Create an instance of our power service server ...
PowerServiceServer svr = new PowerServiceServer();
// ... and bind it with the RMI Registry
Naming.bind ("PowerService", svr);
System.out.println ("Service bound....");
}
}
Writing a RMI client
import java.rmi.*;
import java.rmi.Naming;
import java.io.*;
// PowerServiceClient
public class PowerServiceClient
{
public static void main(String args[]) throws Exception
{
// Check for hostname argument
if (args.length != 1)
{
System.out.println("Syntax - PowerServiceClient host");
System.exit(1);
}
// Assign security manager
if (System.getSecurityManager() == null)
{
System.setSecurityManager
(new RMISecurityManager());
}
Writing a RMI client
// Call registry for PowerService
PowerService service = (PowerService)
Naming.lookup
("rmi://" + args[0] + "/PowerService");
DataInputStream din = new
DataInputStream (System.in);
Writing a RMI client
for (;;)
{
System.out.println("1 - Calculate square");
System.out.println("2 - Calculate power");
System.out.println("3 - Exit");
System.out.println();
System.out.print ("Choice : ");
String line = din.readLine();
Integer choice = new Integer(line);
int value = choice.intValue();
Writing a RMI client
switch (value)
{
case 1:
System.out.print ("Number : ");
line = din.readLine();System.out.println();
choice = new Integer (line);
value = choice.intValue();
// Call remote method
System.out.println ("Answer : " + service.square(value));
break;
case 2:
System.out.print ("Number : ");
line = din.readLine();
choice = new Integer (line);
value = choice.intValue();
System.out.print ("Power : ");
line = din.readLine();
choice = new Integer (line);
int power = choice.intValue();
// Call remote method
System.out.println ("Answer : " + service.power(value, power));
break;
Writing a RMI client
case 3:
System.exit(0);
default :
System.out.println ("Invalid option");
break;
}
}
}
}
Running the client and server
 Start the rmiregistry
 To start the registry, Windows users should do the following (assuming
that your javabin directory is in the current path):-
 start rmiregistry
 Compile the server
 Compile the server, and use the rmic tool to create stub files.
 Start the server
 From the directory in which the classes are located, type the following:-
 java PowerServiceServer
 Start the client
 You can run the client locally, or from a different machine. In either case,
you'll need to specify the hostname of the machine where you are
running the server. If you're running it locally, use localhost as the
hostname.
 java PowerServiceClient localhost

More Related Content

What's hot (20)

Remote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVARemote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVA
Prankit Mishra
 
Rmi ppt
Rmi pptRmi ppt
Rmi ppt
BVC Engineering College
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
Peter R. Egli
 
IIS
IISIIS
IIS
Giritharan V
 
JNDI
JNDIJNDI
JNDI
Shobana Pattabiraman
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java RMI
Java RMIJava RMI
Java RMI
Prajakta Nimje
 
Javascript event handler
Javascript event handlerJavascript event handler
Javascript event handler
Jesus Obenita Jr.
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
Java servlets and CGI
Java servlets and CGIJava servlets and CGI
Java servlets and CGI
lavanya marichamy
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
Jitender Singh Lodhi
 
Domain name system
Domain name systemDomain name system
Domain name system
Siddharth Chandel
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Android Data Persistence
Android Data PersistenceAndroid Data Persistence
Android Data Persistence
Jussi Pohjolainen
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
Rizwan Ali
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 

Similar to Remote Method Invocation in JAVA (20)

Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Sonali Parab
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
Sonali Parab
 
Introduction To Rmi
Introduction To RmiIntroduction To Rmi
Introduction To Rmi
SwarupKulkarni
 
ADB Lab Manual.docx
ADB Lab Manual.docxADB Lab Manual.docx
ADB Lab Manual.docx
SaiKumarPrajapathi
 
Remote method invocatiom
Remote method invocatiomRemote method invocatiom
Remote method invocatiom
sakthibalabalamuruga
 
Java rmi
Java rmiJava rmi
Java rmi
Fazlur Rahman
 
Rmi architecture
Rmi architectureRmi architecture
Rmi architecture
Maulik Desai
 
Rmi
RmiRmi
Rmi
Vijay Kiran
 
Rmi
RmiRmi
Rmi
Mallikarjuna G D
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Arun Nair
 
Remote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programmingRemote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programming
Gera Paulos
 
Rmi
RmiRmi
Rmi
Jafar Nesargi
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Veni7
 
Java rmi tutorial
Java rmi tutorialJava rmi tutorial
Java rmi tutorial
HarikaReddy115
 
Java RMI
Java RMIJava RMI
Java RMI
Ankit Desai
 
Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
elliando dias
 
Java RMI
Java RMIJava RMI
Java RMI
Sunil OS
 
Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)
shraddha mane
 
Rmi
RmiRmi
Rmi
vantinhkhuc
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Dew Shishir
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
Sonali Parab
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
Sonali Parab
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Arun Nair
 
Remote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programmingRemote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programming
Gera Paulos
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Veni7
 
Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
elliando dias
 
Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)Report on mini project(Student database handling using RMI)
Report on mini project(Student database handling using RMI)
shraddha mane
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
Dew Shishir
 
Ad

More from Jalpesh Vasa (16)

Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
Jalpesh Vasa
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
Jalpesh Vasa
 
5. HTML5
5. HTML55. HTML5
5. HTML5
Jalpesh Vasa
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
Jalpesh Vasa
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
Jalpesh Vasa
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
Jalpesh Vasa
 
4.1 PHP Arrays
4.1 PHP Arrays4.1 PHP Arrays
4.1 PHP Arrays
Jalpesh Vasa
 
4 Basic PHP
4 Basic PHP4 Basic PHP
4 Basic PHP
Jalpesh Vasa
 
3.2.1 javascript regex example
3.2.1 javascript regex example3.2.1 javascript regex example
3.2.1 javascript regex example
Jalpesh Vasa
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
Jalpesh Vasa
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
Jalpesh Vasa
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
Jalpesh Vasa
 
2 introduction css
2 introduction css2 introduction css
2 introduction css
Jalpesh Vasa
 
1 web technologies
1 web technologies1 web technologies
1 web technologies
Jalpesh Vasa
 
Kotlin for android development
Kotlin for android developmentKotlin for android development
Kotlin for android development
Jalpesh Vasa
 
Security in php
Security in phpSecurity in php
Security in php
Jalpesh Vasa
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
Jalpesh Vasa
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
Jalpesh Vasa
 
3.2.1 javascript regex example
3.2.1 javascript regex example3.2.1 javascript regex example
3.2.1 javascript regex example
Jalpesh Vasa
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
Jalpesh Vasa
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
Jalpesh Vasa
 
2 introduction css
2 introduction css2 introduction css
2 introduction css
Jalpesh Vasa
 
1 web technologies
1 web technologies1 web technologies
1 web technologies
Jalpesh Vasa
 
Kotlin for android development
Kotlin for android developmentKotlin for android development
Kotlin for android development
Jalpesh Vasa
 
Ad

Recently uploaded (20)

ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
European Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility TestingEuropean Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility Testing
Julia Undeutsch
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Nikki Chapple
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
European Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility TestingEuropean Accessibility Act & Integrated Accessibility Testing
European Accessibility Act & Integrated Accessibility Testing
Julia Undeutsch
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Nikki Chapple
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 

Remote Method Invocation in JAVA

  • 2. What is RMI(Remote Method Invocation) ?  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote virtual machines  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.  Remote objects can be treated similarly to local objects  Handles marshaling, transportation, and garbage collection of the remote objects  Became part of the JDK with version 1.1
  • 3. What is RMI not?  Not language independent  Limited only to Java  Interfaces to remote objects are defined using ordinary Java interfaces (rather than having to use a special purpose interface definition language)  Can provide more advanced features like serialization and security
  • 4. Support for the interface
  • 5. Interface Support Layers  Stub/skeleton layer  Responsible for managing the remote object interface between the client and server  Remote reference layer  Responsible for managing the "liveliness" of the remote objects  Manages the communication between the client/server and virtual machines  Transport layer  Actual network/communication layer that is used to send the information between the client and server over the wire  Currently TCP/IP based  Uses serialization and remote procedure call to send information back and forth between remote objects
  • 6. The General RMI Architecture  The server must first bind its name to the registry  The client lookup the server name in the registry to establish remote references.  The Stub serializing the parameters to skeleton, the skeleton invoking the remote method and serializing the result back to the stub. R M I S e rv e r s k e le to n s tu b R M I C lie n t R e g is try b in d lo o k u pre tu rn c a ll L o c a l M a c h in e R e m o te M a c h in e
  • 7. The Stub and Skeleton  A client invokes a remote method, the call is first forwarded to stub.  The stub is responsible for sending the remote call over to the server- side skeleton  The stub opening a socket to the remote server, marshalling the object parameters and forwarding the data stream to the skeleton.  A skeleton contains a method that receives the remote calls, unmarshals the parameters, and invokes the actual remote object implementation. Stub RMI Client RMI Server skeleton return call
  • 8. The Stub and Skeleton (Cont.)  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:  It initiates a connection with remote Virtual Machine (JVM),  It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),  It waits for the result  It reads (unmarshals) the return value or exception, and  It finally, returns the value to the caller.
  • 9. The Stub and Skeleton (Cont.)  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:  It reads the parameter for the remote method  It invokes the method on the actual remote object, and  It writes and transmits (marshals) the result to the caller.
  • 10. Steps for Developing an RMI System 1. Define the remote interface 2. Develop the remote object by implementing the remote interface. 3. Develop the client program. 4. Compile the Java source files. 5. Generate the client stubs and server skeletons objects using the rmic tool. 6. Start the RMI registry service by rmiregistry tool. 7. Start the remote server objects. 8. Run the client
  • 11. Step 1: Defining the Remote Interface  To create an RMI application, the first step is the defining of a remote interface between the client and server objects. /* SampleServer.java */ import java.rmi.*; public interface SampleServer extends Remote { public int sum(int a,int b) throws RemoteException; }
  • 12. Step 2: Develop the remote object and its interface  The server is a simple unicast remote server.  Create server by extending java.rmi.server.UnicastRemoteObject.  The server uses the RMISecurityManager to protect its resources while engaging in remote communication. /* SampleServerImpl.java */ import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; public class SampleServerImpl extends UnicastRemoteObject implements SampleServer { SampleServerImpl() throws RemoteException { super(); }
  • 13. Step 2: Develop the remote object and its interface Implement the remote methods /* SampleServerImpl.java */ public int sum(int a,int b) throws RemoteException { return a + b; } } The server must bind its name to the registry, the client will look up the server name. Use java.rmi.Naming class to bind the server name to registry. In this example the name call “SAMPLE-SERVER”. In the main method of your server object, the RMI security manager is created and installed.
  • 14. Step 2: Develop the remote object and its interface /* SampleServerImpl.java */ public static void main(String args[]) { try { System.setSecurityManager(new RMISecurityManager()); //set the security manager //create a local instance of the object SampleServerImpl Server = new SampleServerImpl(); //put the local instance in the registry Naming.rebind("SAMPLE-SERVER" , Server); System.out.println("Server waiting....."); } catch (java.net.MalformedURLException me) { System.out.println("Malformed URL: " + me.toString()); } catch (RemoteException re) { System.out.println("Remote exception: " + re.toString()); } }
  • 15. Step 3: Develop the client program  In order for the client object to invoke methods on the server, it must first look up the name of server in the registry. You use the java.rmi.Naming class to lookup the server name.  The server name is specified as URL in the from ( rmi://host:port/name )  Default RMI port is 1099.  The name specified in the URL must exactly match the name that the server has bound to the registry. In this example, the name is “SAMPLE-SERVER”  The remote method invocation is programmed using the remote interface name (remoteObject) as prefix and the remote method name (sum) as suffix.
  • 16. Step 3: Develop the client program import java.rmi.*; import java.rmi.server.*; public class SampleClient { public static void main(String[] args) { // set the security manager for the client System.setSecurityManager(new RMISecurityManager()); //get the remote object from the registry try { System.out.println("Security Manager loaded"); String url = "//localhost/SAMPLE-SERVER"; SampleServer remoteObject = (SampleServer)Naming.lookup(url); System.out.println("Got remote object"); System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) ); } catch (RemoteException exc) { System.out.println("Error in lookup: " + exc.toString()); } catch (java.net.MalformedURLException exc) { System.out.println("Malformed URL: " + exc.toString()); } catch (java.rmi.NotBoundException exc) { System.out.println("NotBound: " + exc.toString()); } } }
  • 17. Step 4 & 5: Compile the Java source files & Generate the client stubs and server skeletons Assume the program compile and executing at elpis on ~/rmi Once the interface is completed, you need to generate stubs and skeleton code. The RMI system provides an RMI compiler (rmic) that takes your generated interface class and procedures stub code on its self. elpis:~/rmi> set CLASSPATH=”~/rmi” elpis:~/rmi> javac SampleServer.java elpis:~/rmi> javac SampleServerImpl.java elpis:~/rmi> rmic SampleServerImpl elpis:~/rmi> javac SampleClient.java
  • 18. Step 6: Start the RMI registry The RMI applications need install to Registry. And the Registry must start manual by call rmiregisty. The rmiregistry us uses port 1099 by default. You can also bind rmiregistry to a different port by indicating the new port number as : rmiregistry <new port> elpis:~/rmi> rmiregistry Remark: On Windows, you have to type in from the command line: > start rmiregistry
  • 19. Steps 7 & 8: Start the remote server objects & Run the client Once the Registry is started, the server can be started and will be able to store itself in the Registry. Because of the grained security model in Java 2.0, you must setup a security policy for RMI by set java.security.policy to the file policy.all elpis:~/rmi> java –Djava.security.policy=policy.all SampleServerImpl elpis:~/rmi> java –Djava.security.policy=policy.all SampleClient
  • 20. Example-2  Create a service that can calculate the square of a number, and the power of two numbers (238 for example).
  • 21. Writing an interface import java.math.BigInteger; import java.rmi.*; // PowerService Interface // // Interface for a RMI service that calculates powers public interface PowerService extends java.rmi.Remote { // Calculate the square of a number public BigInteger square ( int number ) throws RemoteException; // Calculate the power of a number public BigInteger power ( int num1, int num2) throws RemoteException; }
  • 22. Implementing the interface import java.math.*; import java.rmi.*; import java.rmi.server.*; // // PowerServiceServer // // Server for a RMI service that calculates powers // public class PowerServiceServer extends UnicastRemoteObject implements PowerService { public PowerServiceServer () throws RemoteException { super(); }
  • 23. Implementing the interface // Calculate the square of a number public BigInteger square ( int number ) throws RemoteException { String numrep = String.valueOf(number); BigInteger bi = new BigInteger (numrep); // Square the number bi.multiply(bi); return (bi); } // Calculate the power of a number public BigInteger power ( int num1, int num2) throws RemoteException { String numrep = String.valueOf(num1); BigInteger bi = new BigInteger (numrep); bi = bi.pow(num2); return bi; }
  • 24. Implementing the interface public static void main ( String args[] ) throws Exception { // Assign a security manager, in the event that dynamic // classes are loaded if (System.getSecurityManager() == null) System.setSecurityManager ( new RMISecurityManager() ); // Create an instance of our power service server ... PowerServiceServer svr = new PowerServiceServer(); // ... and bind it with the RMI Registry Naming.bind ("PowerService", svr); System.out.println ("Service bound...."); } }
  • 25. Writing a RMI client import java.rmi.*; import java.rmi.Naming; import java.io.*; // PowerServiceClient public class PowerServiceClient { public static void main(String args[]) throws Exception { // Check for hostname argument if (args.length != 1) { System.out.println("Syntax - PowerServiceClient host"); System.exit(1); } // Assign security manager if (System.getSecurityManager() == null) { System.setSecurityManager (new RMISecurityManager()); }
  • 26. Writing a RMI client // Call registry for PowerService PowerService service = (PowerService) Naming.lookup ("rmi://" + args[0] + "/PowerService"); DataInputStream din = new DataInputStream (System.in);
  • 27. Writing a RMI client for (;;) { System.out.println("1 - Calculate square"); System.out.println("2 - Calculate power"); System.out.println("3 - Exit"); System.out.println(); System.out.print ("Choice : "); String line = din.readLine(); Integer choice = new Integer(line); int value = choice.intValue();
  • 28. Writing a RMI client switch (value) { case 1: System.out.print ("Number : "); line = din.readLine();System.out.println(); choice = new Integer (line); value = choice.intValue(); // Call remote method System.out.println ("Answer : " + service.square(value)); break; case 2: System.out.print ("Number : "); line = din.readLine(); choice = new Integer (line); value = choice.intValue(); System.out.print ("Power : "); line = din.readLine(); choice = new Integer (line); int power = choice.intValue(); // Call remote method System.out.println ("Answer : " + service.power(value, power)); break;
  • 29. Writing a RMI client case 3: System.exit(0); default : System.out.println ("Invalid option"); break; } } } }
  • 30. Running the client and server  Start the rmiregistry  To start the registry, Windows users should do the following (assuming that your javabin directory is in the current path):-  start rmiregistry  Compile the server  Compile the server, and use the rmic tool to create stub files.  Start the server  From the directory in which the classes are located, type the following:-  java PowerServiceServer  Start the client  You can run the client locally, or from a different machine. In either case, you'll need to specify the hostname of the machine where you are running the server. If you're running it locally, use localhost as the hostname.  java PowerServiceClient localhost