Escolar Documentos
Profissional Documentos
Cultura Documentos
Java RMI
Sistemas Distribuidos
Rodrigo Santamara
+ Java RMI
RMI
Java RMI
RMI
l
http://download.oracle.com/javase/tutorial/rmi/overview.html
Tutorial de sockets:
l
http://download.oracle.com/javase/tutorial/networking/sockets/
clientServer.html
RMI
Aplicacin
RMI vs Sockets
RMI
Sockets
Sencillo
Complicado
No hay un protocolo
Necesidad de un protocolo
Stub+registro+objetos
En el fondo, RMI = Sockets + Serializacin + Algunas utilidades
RMI
Implementacin
1.
2.
Implementar el servidor
1.
3.
4.
+ Java RMI
RMI
Java RMI
Java RMI
Implementacin
1.
2.
3.
4.
Java RMI
Esquema
pepe.usal.es
Interfaz
maria.usal.es
1) Publicacin
Interfaz
2) Implementacin
Cliente
4) Uso
Stub
3) Registro
Servicio
10
Java RMI
Interfaz
/**
* Interfaz para un servicio RMI de corredores de bolsa
* Debe heredar de java.rmi.Remote
* Debe manejar RemoteException en sus mtodos
*/
public interface Corredor extends Remote
{
String listarTitulos() throws RemoteException;
void comprar(String nombre, int cantidad) throws RemoteException;
void vender(String nombre, int cantidad) throws RemoteException;
}
11
Java RMI
Interfaz: publicacin
n
La mejor opcin:
cd /home/usuario/workspace/rmiInterface/src
javac rmiInterface/Corredor.java
jar cvf bolsaInterface.jar rmiInterface/*.class
12
Java RMI
Interfaz: codebase
n
System.setProperty("java.rmi.server.codebase", "url");
13
Java RMI
Interfaz: URLs
n
"file:///Users/rodri/Documents/workspace/assoo/bin/"
"file:///Users/rodri/Documents/workspace/interface.jar"
"http://vis.usal.es/rodrigo/documentos/aso/rmi/interface.jar"
14
Java RMI
Servidor y Stub
/**
* Servidor de bolsa que implementa Corredor
* Debe implementar una interfaz de java.rmi.Remote
*
*/
public class Bolsa implements Corredor
{
String listarTitulos() throws RemoteException
{...}
void comprar(String nombre, int cantidad) throws RemoteException;
{...}
void vender(String nombre, int cantidad) throws RemoteException;
{...}
//Cualquier otra funcin interna que sea necesaria
}
15
Java RMI
Stub: registro
n
Tres maneras:
l
Runtime.getRuntime().exec("rmiregistry");
rmiregistry [port]
LocateRegistry.createRegistry(int port);
16
Java RMI
Stub: registro
n
Mediante java.rmi.registry.Registry
n
Registry registro=LocateRegistry.getRegistry();
registro.rebind(nombre,stub);
Mediante java.rmi.Naming
n
Naming.rebind(nombre, stub)
17
Java RMI
Cliente
public class Cliente
{
public static void main(String args[])
{
try
{
String nombre="LaBolsa";
//Instanciar el registro RMI
Registry registro=LocateRegistry.getRegistry(args[0]);
//Instanciar un objeto de la clase del servidor
Corredor corredor=(Corredor) registro.lookup(nombre);
//Uso del servicio
...
}
catch (Exception e)
{
System.err.println("Excepcin en el cliente de la bolsa:");
e.printStackTrace();
}
}
}
18
Java RMI
Cliente: bsqueda
n
Mediante java.rmi.registry.Registry
n
Registry registro=LocateRegistry.getRegistry();
registro.lookup(nombre);
Mediante java.rmi.Naming
n
Naming.lookup(nombre)
19
Java RMI
Gestor de seguridad
Si nuestra interfaz contiene mtodos que requieren como
argumentos o devuelven clases distintas del API de Java, hay
que implementar un gestor de seguridad
20