1 / 21

Java RMI

Java RMI. Alcides Calsavara. Objetivos.

mary
Download Presentation

Java RMI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java RMI Alcides Calsavara

  2. 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.

  3. Esquema geral JVM servidor objeto da classe S JVM cliente método f da classe C em execução método g chamada remota

  4. 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

  5. 2 1 3 Client Stub Skeleton Server Registry General scenario naming service get an object reference object reference is published perceived actual communication

  6. 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 Servidor Socket Cliente Naming Naming Socket Cliente Socket Servidor Registry

  7. Hierarquia de classes e interfaces Classe UnicastRemoteObject Interface Remote extends extends Interface R assinatura de g Classe S implementação de g implements

  8. Compilação S.java C.java javac javac S.class C.class rmic S_Skel.class S_Stub.class

  9. Comandos Compilação: javac S.java rmic S javac C.java Execução (3 processos): rmiregistry java S java C

  10. 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 ; }

  11. 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.*; // ...

  12. The RMI date server public class CalendarImpl extends UnicastRemoteObject implements iCalendar { public CalendarImpl() throws RemoteException {} public Date getDate () throws RemoteException { return new Date (); }

  13. 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(); } }

  14. The RMI date client // file CalendarUser.java import java.util.Date; import java.rmi.*; public class CalendarUser { // constructor public CalendarUser() {}

  15. 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(); }

  16. The RMI date client catch (Exception e) { e.printStackTrace(); } System.out.println ("This RMI call took ” + (t2-t1) + " milliseconds"); } // main } // class CalendarUser

  17. 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 onlyin a registry running on its host • Registry server can be launched manuallyor inside the program

  18. 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");

  19. 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 classesneeded by the JVM (CLASSPATH variable). • Applet class loader: to download appletsand 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.

  20. 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

  21. Client bytecode rmic compiler javac compiler javac compiler rmic compiler Stub Skeleton Server bytecode Building apps with RMI Interface source code server source code client source code

More Related