0% found this document useful (0 votes)
17 views22 pages

CH 5

Uploaded by

Tade Mf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views22 pages

CH 5

Uploaded by

Tade Mf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

MEKDELA AMBA

UNIVERSITY
DEPARTMENT
OF
SOFTWARE
ENGINEERING
ADVANCED PROGRAMMING(SENG6132)
Chapter Five
Remote Method Invocation
OUTLINE
• Overview of RMI
• The RMI Registry
• The Remote Interface
• Implementing RMI
INTRODUCTION
• Remote Method Invocation (RMI) is a Java API that allows an object running in
one Java Virtual Machine (JVM) to invoke methods on an object running in another
JVM, potentially located on a remote server. It is a powerful and flexible mechanism
for creating distributed applications in Java, enabling communication between
different systems over a network.

• RMI enables developers to build distributed systems where objects can interact with
each other as if they were local to the application, even though they are hosted on
different machines.

• Is a powerful and simple way to build distributed applications in Java. It allows Java
objects to interact remotely as though they were local, providing seamless
communication across networks.
RMI
• It is an API that provide a mechanism to create distributed application in
java.
• RMI allow an object to invoke method on an object running in another
JVM.
• RMI simplifies the development of distributed applications by enabling the
interaction between objects running on different machines, within a
network.
• It provide remote communication between the applications using two object
stub and skeleton.
CONT…
• Stub: The client-side representation of the remote object. It acts as a gateway
to the actual object on the server side.
• Skeleton: The server-side representation that receives the remote method
calls and dispatches them to the actual object. Note that in modern versions
of Java, the skeleton is no longer required as RMI now uses dynamic
proxies.
THE RMI REGISTRY
• The RMI registry is a simple server that allows clients to find remote
objects on a server. It stores the references to the remote objects and makes
them available for clients to look up.

• The registry runs on a specific port (by default, port 1099) and is used to
bind objects so that clients can locate and invoke them.

• RMI uses a registry (a simple name-server for Java objects) to store and
retrieve remote objects. The server binds its remote objects to the RMI
registry, and clients use the registry to look up and access those objects.
THE REMOTE INTERFACE
• In RMI the remote interface plays a crucial role in defining the contract
between a client and a server in a distributed Java application. It enables the
client to invoke methods on an object that exists on a remote machine (the
server) as if it were a local object.
• The remote interface in RMI is an interface that declares the methods a
remote object can invoke. These methods can be called by a client, and the
remote object implements the interface to provide the actual functionality.
IMPLEMENTING RMI
• Implementing Remote Method Invocation (RMI) in Java
involves several steps, which can be summarized as defining the
remote interface, creating the remote object, setting up the RMI
registry, and writing the client to access the remote object. Below
is a step-by-step guide to implement an RMI-based application.
STEPS TO DEVELOP RMI SYSTEM
Define the Remote Interface
Implement the Remote Interface
Create the RMI Server
Create the RMI Client
Compile the Java Files
Start the RMI Registry
Run the Server and Client
STEP 1
import java.rmi.Remote;
import java.rmi.RemoteException;

// Remote interface
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
STEP 2
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
// Implementing the remote interface
public class HelloImpl extends UnicastRemoteObject implements Hello {
// Constructor
protected HelloImpl() throws RemoteException {
super();
}// Implement the remote method
public String sayHello() throws RemoteException {
return "Hello, World!";
}}
STEP 3
import java.rmi.*;
import java.rmi.registry.Registry;
public class HelloServer {
public static void main(String[] args) {
try {// Create and export the remote object
HelloImpl obj = new HelloImpl(); // Start the RMI registry
LocateRegistry.createRegistry(1099); // Default port is
1099 // Bind the remote object to the registry
Naming.rebind("rmi://localhost/Hello", obj);
System.out.println("Server ready");
} catch (Exception e) {e.printStackTrace();
}}}
STEP 4
import java.rmi.Naming;
public class HelloClient {
public static void main(String[] args) {
try {// Look up the remote object by its name in the registry
Hello hello = (Hello) Naming.lookup("rmi://localhost/Hello");
// Call the remote method
String response = hello.sayHello();
System.out.println("Response from server: " + response);
} catch (Exception e) {
e.printStackTrace();
}}}
STEP 5 RUNNING THE RMI APPLICATION
Implement interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RmiServerIntf extends Remote{
// an interace that extends java.rmi.Remote;remote interface: the client's view of the
remote object;
public String getMessage ()throws RemoteException;
public int summing(int x, int y)throws RemoteException ;
}
REMOTE OBJECT: RMI SERVER
import java.awt.GridLayout;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.*;
public class RmiServer extends UnicastRemoteObject implements
RmiServerIntf {
//RmiServerIntf is a remote interface;
//RmiServer is a remote object and getMessage is a remote method
public String MESSAGE="YY";
public RmiServer(String msg) throws RemoteException {
super(0);
}
CONT…

public String getMessage() throws RemoteException {


return this.MESSAGE;
}
public int summing(int x, int y) {
return x + y;
}
public int getAge() {
return 25;
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("The Server:");
JLabel msg_label_server_satarted = new JLabel("");
JLabel msg_label_registry_created = new JLabel("");
JLabel msg_label_peer_server_bound = new JLabel("");
msg_label_server_satarted.setText("RMI server started");
CONT…
try {
LocateRegistry.createRegistry(1099);
msg_label_registry_created.setText("java RMI registry created");
} catch (RemoteException e) {
msg_label_registry_created.setText("java RMI registry already exists");
}
RmiServer obj = new RmiServer("I am an RMI Server");
Naming.bind("tadele", obj);
msg_label_peer_server_bound.setText("PeerServer bound in registry");
frame.setLayout(new GridLayout(3, 1));
frame.add(msg_label_server_satarted);
frame.add(msg_label_registry_created);
frame.add(msg_label_peer_server_bound);
frame.setBounds(200, 100, 400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
RMI CLIENT
import java.awt.GridLayout;
import java.rmi.Naming;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class RmiClient {
public static void main (String args[])throws Exception {
RmiServerIntf obj = (RmiServerIntf)Naming.lookup("tadele”) ;//stub
JLabel label_display_output = new JLabel();
JLabel label_sum_display = new JLabel();
label_display_output.setText(obj.getMessage());
CONT…

label_sum_display.setText(" "+obj.summing(30, 40));


JDialog frame = new JDialog();
frame.setLayout(new GridLayout(2,1));
frame.add(label_display_output);
frame.add(label_sum_display);
frame.setTitle("Client");
frame.setBounds(100, 200, 300, 300);
frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy