java rmi alcides calsavara. objetivos permitir que um método de uma classe java em execução em...

Post on 17-Apr-2015

112 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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 cliente

método fda classe Cem execução

JVM servidor

método g

objeto daclasse S

chamadaremota

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

Server

Registry

Client

StubSkeleton

communication

actual

perceived

get an object referenceobject

reference is published

naming service

2

1

3

Componentes em execução

f de C g de S

S_SkelS_Stub

SocketCliente

SocketServidor

Naming

Naming

RegistrySocketCliente

SocketServidor

chamda remota de g

cliente servidor

main

cria

bind

lookup

Hierarquia de classes e interfaces

Interface Remote Classe UnicastRemoteObject

Interface Rassinatura de g

Classe Simplementação de g

extends

implements

extends

CompilaçãoS.java

javac

S.class

rmic

S_Skel.class S_Stub.class

C.java

javac

C.class

ComandosCompilaçã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 usedby 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

Server bytecode

server source code

client source code

Interface source code

Client bytecode

StubSkeleton

rmic compiler

javac compiler javac compiler

rmic compiler

top related