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
objeto da
classe S
JVM cliente
método f
da classe C
em execução
chamada
remota
método g
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
reference is
published
1
get an object
reference
2
perceived
Server
Skeleton
Client
actual
3
communication
Stub
Componentes em execução
cliente
f de C
servidor
chamda remota de g
S_Stub
lookup
S_Skel
Socket
Cliente
Socket
Servidor
Socket
Cliente
Socket
Servidor
g de S
cria
main
bind
Naming
Naming
Registry
Hierarquia de classes e interfaces
Interface Remote
Classe UnicastRemoteObject
extends
Interface R
assinatura de g
implements
extends
Classe S
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
import
import
import
// ...
java.util.Date;
java.rmi.*;
java.rmi.registry.*;
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
server
source
code
Skeleton
rmic compiler
Stub
client
source
code
javac compiler
javac compiler
Server bytecode
Client bytecode
Download

Java RMI