SlideShare a Scribd company logo
Distributed Systems
Lecture -5-
Created by :
Eng. Ghadeer Al Hasan
RMI
Remote Method Invocation
Intro 1
Today we havetwo simple examples
- 1st : a programallows the userto log in to the server
- 2nd : a programallows the userto viewbooks fromthe server and query them
1st Example : LogInRMI
Server 3
public interface loginFacade extends Remote{
public String login(String username, String password) throws RemoteException;
}
public class LoginServer extends UnicastRemoteObject implements loginFacade{
private TreeMap clients = new TreeMap<String,String>();
public LoginServer() throws RemoteException{
super();
}
@Override
public String login(String username, String password) throws RemoteException {
Init();
String response = search(username, password);
return response;
}
Server… 4
private void Init() {
clients.put("user1", "1234");
clients.put("user2","1234");
}
private String search(String username,String password){
String response ="";
Set set = clients.entrySet();
Iterator it = set.iterator();
boolean flag = false;
while(it.hasNext()){
response ="";
Map.Entry me = (Map.Entry) it.next();
String user = me.getKey().toString();
String pass = me.getValue().toString();
if(username.equalsIgnoreCase(user)){
flag = true;
if(password.equalsIgnoreCase(pass)){
response ="LOGIN_SUCCESFUL";
}else{
response = "PASSWORD_INCORRECT";
}
break;
}
}
if(!flag){
response = "USER_NOT_EXISTS";
}
return response;
}
Server… 5
public static void main(String[] args){
try{
Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
LoginServer obj = new LoginServer();
reg.rebind("rmi://localhost/service", obj);
System.out.println("Server Running...");
}catch(RemoteException ex){
System.out.println(ex);
}
}
Client 6
public class Client {
public static void main(String[] args) {
try{
Registry reg = LocateRegistry.getRegistry("localhost", 1099);
loginFacade server = (loginFacade) reg.lookup("rmi://localhost/service");
Scanner scan = new Scanner(System.in);
System.out.println("Enter username : ");
String username = scan.nextLine();
System.out.println("Enter password : ");
String password = scan.nextLine();
String response = server.login(username, password);
System.out.println("Message From Server : " + response);
}catch(RemoteException | NotBoundException ex){
System.out.println(ex);
}
}
}
Run 7
2nd Example : BookRMI
9
public class Book implements Serializable{
private static final long serialVersionUID = 1190476516911661470L;
private String title;
private String code;
private double cost;
public Book(String code) {
this.code = code;
}
public Book(String title, String isbn, double cost) {…}
public String getTitle() {…}
public String getCode() {…}
public double getCost() {…}
public String toString() {
return "> " + this.title + " ($" + this.cost + ")";
}
}
Server
10
public interface BookInterface extends Remote {
Book findBook(Book b) throws RemoteException;
List<Book> allBooks() throws RemoteException;
}
public class BookStore extends UnicastRemoteObject implements BookInterface {
private static final long serialVersionUID = 1L;
private List<Book> bookList;
protected BookStore(List<Book> list) throws RemoteException {
super();
this.bookList = list;
}
Server…
11
@Override
public Book findBook(Book book) throws RemoteException {
Predicate<Book> predicate = x -> x.getCode().equals(book.getCode());
return bookList.stream().filter(predicate).findFirst().get();
}
@Override
public List<Book> allBooks() throws RemoteException {
return bookList;
}
private static List<Book> initializeList() {
List<Book> list = new ArrayList<>();
list.add(new Book("Head First Java, 2nd Edition", "A1", 31.41));
list.add(new Book("Java In A Nutshell", "A2", 10.90));
list.add(new Book("Java: The Complete Reference", "B1", 40.18));
list.add(new Book("Head First Servlets and JSP", "B2", 35.41));
list.add(new Book("Java Puzzlers: Traps, Pitfalls", "C1", 39.99));
return list;
}
Server…
12
public static void main(String[] args) {
try {
Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
BookStore store = new BookStore(initializeList());
reg.rebind("rmi://localhost/service", store);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.getMessage());
}
}
Server…
13
public class Client {
private static BookInterface server;
public static void main(String[] args) {
try {
Registry reg = LocateRegistry.getRegistry("localhost", 1099);
server = (BookInterface) reg.lookup("rmi://localhost/service");
boolean findmore;
do {
String[] options = {"Show All", "Find a book", "Exit"};
int choice = JOptionPane.
showOptionDialog(null, "Choose an action", "Option dialog",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
Client
14
switch (choice) {
case 0:
List<Book> list = server.allBooks();
StringBuilder message = new StringBuilder();
list.forEach(x -> {
message.append(x.toString() + "n");
});
JOptionPane.showMessageDialog(null, new String(message));
break;
Client…
case 1:
String code= JOptionPane
.showInputDialog("Type the code of the book you want to find.");
try {
Book response = server.findBook(new Book(code));
JOptionPane.showMessageDialog(null, "Title: "
+ response.getTitle() + "n" + "Cost: $"
+ response.getCost(),
response.getCode(), JOptionPane.INFORMATION_MESSAGE);
} catch (NoSuchElementException ex) {
JOptionPane.showMessageDialog(null, "Not found");
}
break;
default:
System.exit(0);
break;
}
findmore = (JOptionPane.showConfirmDialog(null, "Do you want to exit?",
"Exit",JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION);
} while (findmore);
} catch (RemoteException | NotBoundException ex) {
System.out.println(ex);
}
}
}
15Run
References 16
- YouTube link
https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ
- GitHub
https://github.com/Ghadeerof
End Lecture

More Related Content

PPTX
Java осень 2012 лекция 2
PPTX
Multithreaded programming
PPT
Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
PPT
Networking Core Concept
PDF
Chat application in java using swing and socket programming.
PPT
JDBC Core Concept
PDF
ikh331-06-distributed-programming
PDF
Chat Room System using Java Swing
Java осень 2012 лекция 2
Multithreaded programming
Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
Networking Core Concept
Chat application in java using swing and socket programming.
JDBC Core Concept
ikh331-06-distributed-programming
Chat Room System using Java Swing

What's hot (20)

PDF
DCN Practical
DOCX
JavaExamples
PDF
The Ring programming language version 1.7 book - Part 30 of 196
PDF
Python postgre sql a wonderful wedding
PDF
The Ring programming language version 1.5.2 book - Part 26 of 181
PDF
The Ring programming language version 1.5.1 book - Part 24 of 180
PDF
The Ring programming language version 1.7 book - Part 29 of 196
DOCX
Codes
PDF
Core java pract_sem iii
PDF
The Ring programming language version 1.10 book - Part 35 of 212
PPTX
FRP: What does "declarative" mean
PDF
The Ring programming language version 1.6 book - Part 28 of 189
PDF
"PostgreSQL and Python" Lightning Talk @EuroPython2014
PDF
5. Ввод-вывод, доступ к файловой системе
PDF
The Ring programming language version 1.7 book - Part 7 of 196
PDF
3. Объекты, классы и пакеты в Java
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
PPTX
Swing database(mysql)
PDF
The Ring programming language version 1.8 book - Part 31 of 202
PDF
The Ring programming language version 1.3 book - Part 17 of 88
DCN Practical
JavaExamples
The Ring programming language version 1.7 book - Part 30 of 196
Python postgre sql a wonderful wedding
The Ring programming language version 1.5.2 book - Part 26 of 181
The Ring programming language version 1.5.1 book - Part 24 of 180
The Ring programming language version 1.7 book - Part 29 of 196
Codes
Core java pract_sem iii
The Ring programming language version 1.10 book - Part 35 of 212
FRP: What does "declarative" mean
The Ring programming language version 1.6 book - Part 28 of 189
"PostgreSQL and Python" Lightning Talk @EuroPython2014
5. Ввод-вывод, доступ к файловой системе
The Ring programming language version 1.7 book - Part 7 of 196
3. Объекты, классы и пакеты в Java
Psycopg2 - Connect to PostgreSQL using Python Script
Swing database(mysql)
The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.3 book - Part 17 of 88
Ad

Similar to #5 (Remote Method Invocation) (20)

PDF
IKH331-07-java-rmi
PDF
Testing in android
PDF
Java rmi
PDF
Run rmi
ODP
Codemotion appengine
PPT
Network
PDF
Java 8 Lambda Built-in Functional Interfaces
PDF
Java VS Python
PDF
RMI Java Programming Lab Manual 2019
PDF
Akka Cluster in Java - JCConf 2015
PDF
Painless Persistence with Realm
ODP
Jersey Guice AOP
PPT
比XML更好用的Java Annotation
PPTX
Nice to meet Kotlin
ODP
Lambda Chops - Recipes for Simpler, More Expressive Code
PPTX
PPT
Java RMI
PPTX
In kor we Trust
PPTX
Laravel for Web Artisans
PPT
E:\Plp 2009 2\Plp 9
IKH331-07-java-rmi
Testing in android
Java rmi
Run rmi
Codemotion appengine
Network
Java 8 Lambda Built-in Functional Interfaces
Java VS Python
RMI Java Programming Lab Manual 2019
Akka Cluster in Java - JCConf 2015
Painless Persistence with Realm
Jersey Guice AOP
比XML更好用的Java Annotation
Nice to meet Kotlin
Lambda Chops - Recipes for Simpler, More Expressive Code
Java RMI
In kor we Trust
Laravel for Web Artisans
E:\Plp 2009 2\Plp 9
Ad

More from Ghadeer AlHasan (20)

PPTX
[C++ Tutorial ] #9 Classes
PPTX
[C++ Tutorial] #8 Files
PPTX
[C++ Tutorial] #7- Linked List
PPTX
[Java] #8 String and Inner Class
PPTX
[C++ Tutorial] #6- Pointers
PPTX
[Java] #7 - Input & Output Stream
PPTX
[C++] #5 - Structures
PPTX
#6- Arrays and Collections Framework
PPTX
5- Overriding and Abstraction In Java
PPTX
4- Inheritance, Aggregation, Encapsulation and Overloading
PPTX
3- Operators in Java
PPTX
2- Introduction to java II
PPTX
1- Introduction to java
PPTX
0- Overview
PPTX
4- Arrays
PPTX
3- Functions
PPTX
2- Control Structures
PPTX
1- Languages Basics
PPTX
#8 (Java Message Service)
PPTX
#7 (Java Message Service)
[C++ Tutorial ] #9 Classes
[C++ Tutorial] #8 Files
[C++ Tutorial] #7- Linked List
[Java] #8 String and Inner Class
[C++ Tutorial] #6- Pointers
[Java] #7 - Input & Output Stream
[C++] #5 - Structures
#6- Arrays and Collections Framework
5- Overriding and Abstraction In Java
4- Inheritance, Aggregation, Encapsulation and Overloading
3- Operators in Java
2- Introduction to java II
1- Introduction to java
0- Overview
4- Arrays
3- Functions
2- Control Structures
1- Languages Basics
#8 (Java Message Service)
#7 (Java Message Service)

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
Project quality management in manufacturing
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
web development for engineering and engineering
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Project quality management in manufacturing
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CYBER-CRIMES AND SECURITY A guide to understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
UNIT 4 Total Quality Management .pptx
web development for engineering and engineering
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CH1 Production IntroductoryConcepts.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Foundation to blockchain - A guide to Blockchain Tech
Structs to JSON How Go Powers REST APIs.pdf
Lesson 3_Tessellation.pptx finite Mathematics
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Embodied AI: Ushering in the Next Era of Intelligent Systems
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

#5 (Remote Method Invocation)

  • 1. Distributed Systems Lecture -5- Created by : Eng. Ghadeer Al Hasan RMI Remote Method Invocation
  • 2. Intro 1 Today we havetwo simple examples - 1st : a programallows the userto log in to the server - 2nd : a programallows the userto viewbooks fromthe server and query them
  • 3. 1st Example : LogInRMI
  • 4. Server 3 public interface loginFacade extends Remote{ public String login(String username, String password) throws RemoteException; } public class LoginServer extends UnicastRemoteObject implements loginFacade{ private TreeMap clients = new TreeMap<String,String>(); public LoginServer() throws RemoteException{ super(); } @Override public String login(String username, String password) throws RemoteException { Init(); String response = search(username, password); return response; }
  • 5. Server… 4 private void Init() { clients.put("user1", "1234"); clients.put("user2","1234"); } private String search(String username,String password){ String response =""; Set set = clients.entrySet(); Iterator it = set.iterator(); boolean flag = false; while(it.hasNext()){ response =""; Map.Entry me = (Map.Entry) it.next(); String user = me.getKey().toString(); String pass = me.getValue().toString(); if(username.equalsIgnoreCase(user)){ flag = true; if(password.equalsIgnoreCase(pass)){ response ="LOGIN_SUCCESFUL"; }else{ response = "PASSWORD_INCORRECT"; } break; } } if(!flag){ response = "USER_NOT_EXISTS"; } return response; }
  • 6. Server… 5 public static void main(String[] args){ try{ Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); LoginServer obj = new LoginServer(); reg.rebind("rmi://localhost/service", obj); System.out.println("Server Running..."); }catch(RemoteException ex){ System.out.println(ex); } }
  • 7. Client 6 public class Client { public static void main(String[] args) { try{ Registry reg = LocateRegistry.getRegistry("localhost", 1099); loginFacade server = (loginFacade) reg.lookup("rmi://localhost/service"); Scanner scan = new Scanner(System.in); System.out.println("Enter username : "); String username = scan.nextLine(); System.out.println("Enter password : "); String password = scan.nextLine(); String response = server.login(username, password); System.out.println("Message From Server : " + response); }catch(RemoteException | NotBoundException ex){ System.out.println(ex); } } }
  • 9. 2nd Example : BookRMI
  • 10. 9 public class Book implements Serializable{ private static final long serialVersionUID = 1190476516911661470L; private String title; private String code; private double cost; public Book(String code) { this.code = code; } public Book(String title, String isbn, double cost) {…} public String getTitle() {…} public String getCode() {…} public double getCost() {…} public String toString() { return "> " + this.title + " ($" + this.cost + ")"; } } Server
  • 11. 10 public interface BookInterface extends Remote { Book findBook(Book b) throws RemoteException; List<Book> allBooks() throws RemoteException; } public class BookStore extends UnicastRemoteObject implements BookInterface { private static final long serialVersionUID = 1L; private List<Book> bookList; protected BookStore(List<Book> list) throws RemoteException { super(); this.bookList = list; } Server…
  • 12. 11 @Override public Book findBook(Book book) throws RemoteException { Predicate<Book> predicate = x -> x.getCode().equals(book.getCode()); return bookList.stream().filter(predicate).findFirst().get(); } @Override public List<Book> allBooks() throws RemoteException { return bookList; } private static List<Book> initializeList() { List<Book> list = new ArrayList<>(); list.add(new Book("Head First Java, 2nd Edition", "A1", 31.41)); list.add(new Book("Java In A Nutshell", "A2", 10.90)); list.add(new Book("Java: The Complete Reference", "B1", 40.18)); list.add(new Book("Head First Servlets and JSP", "B2", 35.41)); list.add(new Book("Java Puzzlers: Traps, Pitfalls", "C1", 39.99)); return list; } Server…
  • 13. 12 public static void main(String[] args) { try { Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); BookStore store = new BookStore(initializeList()); reg.rebind("rmi://localhost/service", store); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.getMessage()); } } Server…
  • 14. 13 public class Client { private static BookInterface server; public static void main(String[] args) { try { Registry reg = LocateRegistry.getRegistry("localhost", 1099); server = (BookInterface) reg.lookup("rmi://localhost/service"); boolean findmore; do { String[] options = {"Show All", "Find a book", "Exit"}; int choice = JOptionPane. showOptionDialog(null, "Choose an action", "Option dialog", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); Client
  • 15. 14 switch (choice) { case 0: List<Book> list = server.allBooks(); StringBuilder message = new StringBuilder(); list.forEach(x -> { message.append(x.toString() + "n"); }); JOptionPane.showMessageDialog(null, new String(message)); break; Client… case 1: String code= JOptionPane .showInputDialog("Type the code of the book you want to find."); try { Book response = server.findBook(new Book(code)); JOptionPane.showMessageDialog(null, "Title: " + response.getTitle() + "n" + "Cost: $" + response.getCost(), response.getCode(), JOptionPane.INFORMATION_MESSAGE); } catch (NoSuchElementException ex) { JOptionPane.showMessageDialog(null, "Not found"); } break; default: System.exit(0); break; } findmore = (JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Exit",JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION); } while (findmore); } catch (RemoteException | NotBoundException ex) { System.out.println(ex); } } }
  • 16. 15Run
  • 17. References 16 - YouTube link https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ - GitHub https://github.com/Ghadeerof