You are on page 1of 21

Java RMI

Alcides Calsavara
Objetivos
• Permitir que um método de uma classe Java
em execução em uma máquina virtual JVM
chame um método de um objeto (instância
de uma classe Java) situado em outra
máquina virtual JVM, usando a mesma
sintaxe e com a mesma facilidade de se
chamar um método local.
• Transparência de acesso e de localização.
Esquema geral

JVM servidor

JVM cliente objeto da


classe S
método f
da classe C método g
chamada
em execução
remota
Remote Method Invocation
• Facility to call methods remotely
• Similar to RPC - Remote Procedure Call
• High-level communication abstraction
• The RMI mechanism provides:
• control transfer between caller and called
• parameters cans be exchanged
• class definitions can be exchanged
• RMI support in Java provides:
• A specific API
• compilation support tools
• runtime support
General scenario
naming service
Registry

object 1 get an object


reference
reference is
published
2
perceived
Server Client
actual
Skeleton Stub

3
communication
Componentes em execução
cliente servidor
cria
f de C g de S
chamda remota de g
main
S_Stub S_Skel
bind
lookup
Socket Socket
Cliente Servidor Naming

Naming
Socket Socket Registry
Cliente Servidor
Hierarquia de classes e interfaces

Interface Remote Classe UnicastRemoteObject

extends

extends
Interface R
assinatura de g

Classe S
implements implementação de g
Compilação
S.java
C.java

javac
javac
S.class

C.class
rmic

S_Skel.class S_Stub.class
Comandos
Compilação:

javac S.java
rmic S
javac C.java

Execução (3 processos):

rmiregistry
java S
java C
A Java RMI example
// file iCalendar.java
// specifies a date server interface

import java.rmi.* ;
public interface iCalendar
extends Remote
{
java.util.Date getDate ()
throws RemoteException ;
}
The RMI date server
// file CalendarImpl.java
// the date server implementation

import java.util.Date;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;

// ...
The RMI date server
public class CalendarImpl
extends UnicastRemoteObject
implements iCalendar {

public CalendarImpl()
throws RemoteException {}

public Date getDate ()


throws RemoteException {
return new Date ();
}
The RMI date server
public static void main(String args[]) {
CalendarImpl cal;
try {
cal = new CalendarImpl();
LocateRegistry.createRegistry(1099);
Naming.bind("rmi:///CalendarImpl",
cal);
System.out.println("Ready !");
}
catch (Exception e) {
e.printStackTrace();
}
}
The RMI date client
// file CalendarUser.java

import java.util.Date;
import java.rmi.*;

public class CalendarUser


{
// constructor
public CalendarUser() {}
The RMI date client
public static void main(String args[])
{
long t1=0,t2=0;
Date date;
iCalendar remoteCal;
try {
remoteCal = (iCalendar) Naming.lookup
("rmi://some.host.com/CalendarImpl");

t1 = remoteCal.getDate().getTime();
t2 = remoteCal.getDate().getTime();
}
The RMI date client
catch (Exception e) {
e.printStackTrace();
}

System.out.println
("This RMI call took ”
+ (t2-t1) + " milliseconds");
} // main

} // class CalendarUser
RMI naming service
• Very simple
• Manages pairs [name, object reference]
• names are keys to get references
• there is no “domain” notion
• Implementation
• Only one registry per process
• Defaults to TCP/IP port 1099
• Applications can register names only
in a registry running on its host
• Registry server can be launched manually
or inside the program
Naming service method calls
• To create a registry at port 1099:
LocateRegistry.createRegistry (1099);

• To bind a name to a reference:


Naming.bind ("rmi:///someobj", objRef);

• To resolve a name:
obj = (objClass) Naming.lookup
("rmi://some.host/someobj");
Dynamic class loaders
• To dynamic load classes used
by a running program or applet
• from the local disks or the network
• Three kinds of class loaders:
• Default class loader: to load the classes
needed by the JVM (CLASSPATH variable).
• Applet class loader: to download applets
and additional classes from the applet server.
• RMI class loader: to download from the remote server host all
the classes (stubs, skeletons, parameters) to allow the RMI
calls.
Setting the RMI class loader
• On the client side:
// launch the RMI security manager
System.setSecurityManager (
new RMISecurityManager () );

// set a system property


System.getProperties ().put (
"java.rmi.server.codebase",
"http://some.where/java/classes/” );

• On the server side:


• Put stubs and classes at the indicated place
Building apps with RMI
Interface source code

rmic compiler rmic compiler


server client
source source
code Skeleton Stub code

javac compiler javac compiler

Server bytecode Client bytecode