SlideShare une entreprise Scribd logo
1  sur  38
Télécharger pour lire hors ligne
Java
                 RMI
              CDP Tutorial


Based on


           Based on the slides of Alexander Day Chaffee
Remote Objects (Diagram)
Java Virtual Machine         Java Virtual Machine



       Client                      Remote
       Object          TCP         Object
What is RMI?

    RMI is an RPC system for an object based
    language.

    Objects provide a natural granularity for the
    binding of functions.
    
        RMI allows a program to hold a reference to an object
        on a remote system and to call that object’s methods.

    Client-Server architecture.
    
        Server holds the object.
    
        Client holds a small stub that accesses the object on
        the Server.
RMI Layers
Java Virtual Machine            Java Virtual Machine
        Client                         Remote
        Object                         Object



         Stub                          Skeleton


 Remote Reference Layer          Remote Reference Layer
                          TCP
  Transport Layer                 Transport Layer
Remote Objects

    Remote Objects
    
        Live on server
    
        Accessed as if they were local
Remote References and
        Interfaces

    Remote References
    
        Refer to remote objects
    
        Invoked on client exactly like local object
        references

    Remote Interfaces
    
        Declare exposed methods
    
        Implemented on client
    
        Like a proxy for the remote object
Stubs and Skeletons

    Stub
    
        lives on client
    
        pretends to be remote object

    Skeleton
    
        lives on server
    
        receives requests from stub
    
        talks to true remote object
    
        delivers response to stub
Remote Interfaces and Stubs
                  Remote Interface



                implements           implements


                                             Remote Object
Client         Stub          Skeleton
                                               (Server)
Registries

    Name and look up remote objects

    Servers can register their objects

    Clients can find server objects and obtain a
    remote reference

    A registry is a running process on a host
    machine
RMI System Architecture
Client Virtual Machine                 Server Virtual Machine
    Client                                        Remote
                                                  Object


                                      Skeleton
             Stub                                   Server




                     “Fred”

                    Registry Virtual Machine
RMI Flow
1. Server Creates Remote Object
   Client Virtual Machine                Server Virtual Machine
2. Server Registers Remote Object
        Client                                      Remote
                                                    Object
                                                             1

                                        Skeleton
               Stub                                   Server


                                             2


                       “Fred”

                      Registry Virtual Machine
RMI Flow
Client Virtual Machine                  Server Virtual Machine
    Client                                          Remote
                            3. Client requests object from Registry
                                                    Object
                            4. Registry returns remote reference
                            (and stub gets created)
                                        Skeleton
             Stub                                    Server
     3              4




                        “Fred”

                    Registry Virtual Machine
RMI Flow
Client Virtual Machine                  Server Virtual Machine
    Client                                          Remote
                                                    Object
               5                                7

                             6
                                       Skeleton
              Stub                                   Server

             5. Client invokes stub method
             6. Stub talks to skeleton
             7. Skeleton invokes remote object
                method “Fred”

                     Registry Virtual Machine
RMI Usage

    Start registry

    Start server

    Run client
Creating Remote Objects

    Define a Remote Interface
    
        extends java.rmi.Remote

    Define a class that implements the Remote
    Interface
    
        extends java.rmi.RemoteObject
    
        or java.rmi.UnicastRemoteObject
Remote Interface Example
import java.rmi.*;
public interface Adder
  extends Remote
{
    public int add(int x, int y)
         throws RemoteException;
}
Remote Class Example
import java.rmi.*;
import java.rmi.server.*;
public class AdderImpl extends UnicastRemoteObject
  implements Adder
{
    public AdderImpl() throws RemoteException
    {
      // NOTE: !!!EMPTY CONSTRUCTOR!!!
    }
    public int add(int x, int y) throws RemoteException
    {
        return x + y;
    }
}
Registering Remote Classes

    start the registry
    
        running process

    Unix:
    rmiregistry &

    Windows:
    start /m rmiregistry
Registry CLASSPATH

    Registry VM needs to be able to find stub
    file(s)

    You must set the CLASSPATH to include
    the directory containing the stub file

    An easy way to check CLASSPATH is to use the javap command,
    supplying a fully package qualified class name. It uses the current
    CLASSPATH to find and print the interface to a class.
Create the server

    Creates a new instance of the remote
    object

    Registers it in the registry with a unique
    name

    That’s it
RMI Server Example
try {
    AdderImpl adder = new AdderImpl();
    Naming.rebind("adder", adder);
   System.err.println(“Bind successful”);
}
catch (RemoteException re) {
    re.printStackTrace();
}
catch (MalformedURLException me) {
    me.printStackTrace();
}
Launch the Server
% java AdderServer
Bind successful
Server Logging

  invoke from command line
java
  -Djava.rmi.server.logCalls=true
  YourServerImpl

  or enable inside program
RemoteServer.setLog(System.err);
Creating an RMI Client

    Find a registry
    
        use java.rmi.Naming

    Lookup the name, returns a reference

    Cast the reference to the appropriate
    Remote Interface

    Just use it!
RMI URLs
rmi://host[:port]/name

  default port is 1099

  Specifies hostname of registry

  can also use relative URLs
  
      name only
  
      assumes registry is on local host
RMI Client Example
Adder a = (Adder)//not AdderImpl
  Naming.lookup("rmi://server/adder");

int sum = a.add(2,2);
System.out.println("2+2=" + sum);
Remote Interfaces vs. Remote
      Classes

    Remember that the reference is to an
    interface

    You must make references, arrays, etc. out
    of the interface type, not the
    implementation type

    You can’t cast the remote reference to a
    normal reference

    So name your Remote Objects with “Impl”
    (so you don’t get confused)
Parameter Passing

    Primitive types
    
        passed by value

    Remote objects
    
        passed by reference

    Non-remote objects
    
        passed by value
    
        uses Java Object Serialization
Callbacks

    They just work

    Pass in a remote reference to a client
    object

    Server object can call its methods
    transparently

    Registry is out of the loop
Object Serialization

    aka Persistence

    saves the state (data) of a particular
    instance of an object

    serialize - to save

    unserialize - to load
Java Serialization

    writes object as a sequence of bytes

    writes it to a Stream

    recreates it on the other end

    creates a brand new object with the old
    data
java.io.Serializable

    Objects that implement the
    java.io.Serializable interface are marked as
    serializable

    Also subclasses

    Magically, all non-static and non-transient
    data members will be serialized

    Actually, it’s not magic, it’s Reflection (it’s
    done with mirrors)

    empty interface - just a marker

    It’s a promise
Not All Objects Are
        Serializable

    Any object that doesn’t implement
    Serializable

    Any object that would pose a security risk
    
        e.g. FileInputStream

    Any object whose value depends on VM-
    specific information
    
        e.g. Thread

    Any object that contains a (non-static, non-
    transient) unserializable object (recursively)
NotSerializableException

    thrown if you try to serialize or unserialize
    an unserializable object

    maybe you subclassed a Serializable object
    and added some unserializable members
Incompatible Changes

    If class has members added or removed, it
    becomes incompatible

    java.io.InvalidClassException thrown if you
    try to deserialize an incompatible object
    stream
Limitations of RMI

    Java-only
    
        but you can use JNI on the server

    Uses TCP, not UDP

    At least two sockets per connection

    Untested for huge loads
Summary

    RMI is a very clean API

    Easy way to write distributed programs

    Wire protocol may need improvement for
    large-scale problems
Where to get more
      information

    Harold, Java Network Programming
    (O’Reilly)

    rmi-users mailing list (rmi-users@javasoft.com)

    http:/ / www.developer.com/ (Gamelan)

    http:/ / www.javaworld.com/ (magazine)

    http:/ / www.stinky.com/ java/ (Alexander
    Day Chaffee's site)

    Material from the previous semester

Contenu connexe

Tendances

Tendances (20)

A Short Java RMI Tutorial
A Short Java RMI TutorialA Short Java RMI Tutorial
A Short Java RMI Tutorial
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
 
Rmi
RmiRmi
Rmi
 
Rmi
RmiRmi
Rmi
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMI
 
Remote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVARemote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVA
 
Rmi ppt
Rmi pptRmi ppt
Rmi ppt
 
Java rmi
Java rmiJava rmi
Java rmi
 
Rmi presentation
Rmi presentationRmi presentation
Rmi presentation
 
Rmi
RmiRmi
Rmi
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
 
Java RMI
Java RMIJava RMI
Java RMI
 
Java rmi tutorial
Java rmi tutorialJava rmi tutorial
Java rmi tutorial
 
Java rmi
Java rmiJava rmi
Java rmi
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
 
Rmi architecture
Rmi architectureRmi architecture
Rmi architecture
 
Java remote method invocation
Java remote method invocationJava remote method invocation
Java remote method invocation
 
Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)Introduction to Remote Method Invocation (RMI)
Introduction to Remote Method Invocation (RMI)
 

En vedette

En vedette (13)

Introduction To Rmi
Introduction To RmiIntroduction To Rmi
Introduction To Rmi
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Ravi Tuppad
Ravi TuppadRavi Tuppad
Ravi Tuppad
 
Basic java
Basic java Basic java
Basic java
 
Java RMI
Java RMIJava RMI
Java RMI
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
 
Networking
NetworkingNetworking
Networking
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
 
Jdbc 1
Jdbc 1Jdbc 1
Jdbc 1
 
Java Servlets & JSP
Java Servlets & JSPJava Servlets & JSP
Java Servlets & JSP
 
Java servlets
Java servletsJava servlets
Java servlets
 
Advanced java programming-contents
Advanced java programming-contentsAdvanced java programming-contents
Advanced java programming-contents
 
Advance Java
Advance JavaAdvance Java
Advance Java
 

Similaire à Rmi ppt-2003 (20)

Distributed objects
Distributed objectsDistributed objects
Distributed objects
 
Rmi
RmiRmi
Rmi
 
Module 3 remote method invocation-2
Module 3   remote method  invocation-2Module 3   remote method  invocation-2
Module 3 remote method invocation-2
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
javarmi
javarmijavarmi
javarmi
 
RMI (Remote Method Invocation)
RMI (Remote Method Invocation)RMI (Remote Method Invocation)
RMI (Remote Method Invocation)
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
 
Remote method invocatiom
Remote method invocatiomRemote method invocatiom
Remote method invocatiom
 
Net remoting
Net remotingNet remoting
Net remoting
 
DS
DSDS
DS
 
#4 (Remote Method Invocation)
#4 (Remote Method Invocation)#4 (Remote Method Invocation)
#4 (Remote Method Invocation)
 
Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01
 
Corba
CorbaCorba
Corba
 
Activation
ActivationActivation
Activation
 
Java rmi
Java rmiJava rmi
Java rmi
 
Rmi
RmiRmi
Rmi
 
Rmi
RmiRmi
Rmi
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Rmi3
Rmi3Rmi3
Rmi3
 
ADB Lab Manual.docx
ADB Lab Manual.docxADB Lab Manual.docx
ADB Lab Manual.docx
 

Dernier

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Dernier (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Rmi ppt-2003

  • 1. Java RMI CDP Tutorial Based on Based on the slides of Alexander Day Chaffee
  • 2. Remote Objects (Diagram) Java Virtual Machine Java Virtual Machine Client Remote Object TCP Object
  • 3. What is RMI?  RMI is an RPC system for an object based language.  Objects provide a natural granularity for the binding of functions.  RMI allows a program to hold a reference to an object on a remote system and to call that object’s methods.  Client-Server architecture.  Server holds the object.  Client holds a small stub that accesses the object on the Server.
  • 4. RMI Layers Java Virtual Machine Java Virtual Machine Client Remote Object Object Stub Skeleton Remote Reference Layer Remote Reference Layer TCP Transport Layer Transport Layer
  • 5. Remote Objects  Remote Objects  Live on server  Accessed as if they were local
  • 6. Remote References and Interfaces  Remote References  Refer to remote objects  Invoked on client exactly like local object references  Remote Interfaces  Declare exposed methods  Implemented on client  Like a proxy for the remote object
  • 7. Stubs and Skeletons  Stub  lives on client  pretends to be remote object  Skeleton  lives on server  receives requests from stub  talks to true remote object  delivers response to stub
  • 8. Remote Interfaces and Stubs Remote Interface implements implements Remote Object Client Stub Skeleton (Server)
  • 9. Registries  Name and look up remote objects  Servers can register their objects  Clients can find server objects and obtain a remote reference  A registry is a running process on a host machine
  • 10. RMI System Architecture Client Virtual Machine Server Virtual Machine Client Remote Object Skeleton Stub Server “Fred” Registry Virtual Machine
  • 11. RMI Flow 1. Server Creates Remote Object Client Virtual Machine Server Virtual Machine 2. Server Registers Remote Object Client Remote Object 1 Skeleton Stub Server 2 “Fred” Registry Virtual Machine
  • 12. RMI Flow Client Virtual Machine Server Virtual Machine Client Remote 3. Client requests object from Registry Object 4. Registry returns remote reference (and stub gets created) Skeleton Stub Server 3 4 “Fred” Registry Virtual Machine
  • 13. RMI Flow Client Virtual Machine Server Virtual Machine Client Remote Object 5 7 6 Skeleton Stub Server 5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object method “Fred” Registry Virtual Machine
  • 14. RMI Usage  Start registry  Start server  Run client
  • 15. Creating Remote Objects  Define a Remote Interface  extends java.rmi.Remote  Define a class that implements the Remote Interface  extends java.rmi.RemoteObject  or java.rmi.UnicastRemoteObject
  • 16. Remote Interface Example import java.rmi.*; public interface Adder extends Remote { public int add(int x, int y) throws RemoteException; }
  • 17. Remote Class Example import java.rmi.*; import java.rmi.server.*; public class AdderImpl extends UnicastRemoteObject implements Adder { public AdderImpl() throws RemoteException { // NOTE: !!!EMPTY CONSTRUCTOR!!! } public int add(int x, int y) throws RemoteException { return x + y; } }
  • 18. Registering Remote Classes  start the registry  running process  Unix: rmiregistry &  Windows: start /m rmiregistry
  • 19. Registry CLASSPATH  Registry VM needs to be able to find stub file(s)  You must set the CLASSPATH to include the directory containing the stub file  An easy way to check CLASSPATH is to use the javap command, supplying a fully package qualified class name. It uses the current CLASSPATH to find and print the interface to a class.
  • 20. Create the server  Creates a new instance of the remote object  Registers it in the registry with a unique name  That’s it
  • 21. RMI Server Example try { AdderImpl adder = new AdderImpl(); Naming.rebind("adder", adder); System.err.println(“Bind successful”); } catch (RemoteException re) { re.printStackTrace(); } catch (MalformedURLException me) { me.printStackTrace(); }
  • 22. Launch the Server % java AdderServer Bind successful
  • 23. Server Logging  invoke from command line java -Djava.rmi.server.logCalls=true YourServerImpl  or enable inside program RemoteServer.setLog(System.err);
  • 24. Creating an RMI Client  Find a registry  use java.rmi.Naming  Lookup the name, returns a reference  Cast the reference to the appropriate Remote Interface  Just use it!
  • 25. RMI URLs rmi://host[:port]/name  default port is 1099  Specifies hostname of registry  can also use relative URLs  name only  assumes registry is on local host
  • 26. RMI Client Example Adder a = (Adder)//not AdderImpl Naming.lookup("rmi://server/adder"); int sum = a.add(2,2); System.out.println("2+2=" + sum);
  • 27. Remote Interfaces vs. Remote Classes  Remember that the reference is to an interface  You must make references, arrays, etc. out of the interface type, not the implementation type  You can’t cast the remote reference to a normal reference  So name your Remote Objects with “Impl” (so you don’t get confused)
  • 28. Parameter Passing  Primitive types  passed by value  Remote objects  passed by reference  Non-remote objects  passed by value  uses Java Object Serialization
  • 29. Callbacks  They just work  Pass in a remote reference to a client object  Server object can call its methods transparently  Registry is out of the loop
  • 30. Object Serialization  aka Persistence  saves the state (data) of a particular instance of an object  serialize - to save  unserialize - to load
  • 31. Java Serialization  writes object as a sequence of bytes  writes it to a Stream  recreates it on the other end  creates a brand new object with the old data
  • 32. java.io.Serializable  Objects that implement the java.io.Serializable interface are marked as serializable  Also subclasses  Magically, all non-static and non-transient data members will be serialized  Actually, it’s not magic, it’s Reflection (it’s done with mirrors)  empty interface - just a marker  It’s a promise
  • 33. Not All Objects Are Serializable  Any object that doesn’t implement Serializable  Any object that would pose a security risk  e.g. FileInputStream  Any object whose value depends on VM- specific information  e.g. Thread  Any object that contains a (non-static, non- transient) unserializable object (recursively)
  • 34. NotSerializableException  thrown if you try to serialize or unserialize an unserializable object  maybe you subclassed a Serializable object and added some unserializable members
  • 35. Incompatible Changes  If class has members added or removed, it becomes incompatible  java.io.InvalidClassException thrown if you try to deserialize an incompatible object stream
  • 36. Limitations of RMI  Java-only  but you can use JNI on the server  Uses TCP, not UDP  At least two sockets per connection  Untested for huge loads
  • 37. Summary  RMI is a very clean API  Easy way to write distributed programs  Wire protocol may need improvement for large-scale problems
  • 38. Where to get more information  Harold, Java Network Programming (O’Reilly)  rmi-users mailing list (rmi-users@javasoft.com)  http:/ / www.developer.com/ (Gamelan)  http:/ / www.javaworld.com/ (magazine)  http:/ / www.stinky.com/ java/ (Alexander Day Chaffee's site)  Material from the previous semester