Download Client Virtual Machine

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
www.hndit.com
Introduction to
Java Remote
Method
Invocation
www.hndit.com
Remote Objects
Client
Object
Java Virtual Machine
TCP
Remote
Object
Java Virtual Machine
www.hndit.com
What is RMI?
• RMI is an RPC system for OOP languages
• RMI allows programs to
– Hold a reference to an object on a remote system
– Invoke methods of the remote object
• Client Server Architecture
– Server holds the object
– Client holds a small stub that accessed the object on the sever
www.hndit.com
RMI Layers
Client
Object
Stub
Remote
Object
TCP
Skeleton
Remote Reference Layer
Remote Reference Layer
Transport Layer
Transport Layer
Java Virtual Machine
Java Virtual Machine
www.hndit.com
Remote Objects
• Remote Objects
– Live on Server
– Accessed as if they were local
www.hndit.com
Remote References and Interfaces
• Remote References
– Refer to remote objects
– Invoked on the client exactly like local object references
• Remote Interfaces
– Declare exposed methods
– Implemented on client
– Like a proxy for the remote object
www.hndit.com
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
www.hndit.com
Remote Interfaces and Stubs
Remote Interface
Client
Stub
Skeleton
Remote Object
www.hndit.com
Registries
• Name and look up remote objects
• Servers can register their objects
• Clients can find server objects and obtain a remote
reference
• Registry is a running process on a host machine
www.hndit.com
RMI System Architecture
Client
Object
Remote
Object
Stub
Skeleton
Server
Client Virtual Machine
Server Virtual Machine
Registry Virtual Machine
www.hndit.com
RMI Flow
Client Virtual Machine
Server Virtual Machine
Remote
Object
Client
Object
1
Skeleton
Stub
Server
1. Server creates remote object
2. Server registers remote
objects
2
myMessage
Registry Virtual Machine
www.hndit.com
RMI Flow
Client Virtual Machine
Server Virtual Machine
Remote
Object
Client
Object
Skeleton
Stub
Server
3
4
myMessage
Registry Virtual Machine
3. Client requests object from registry
4. Registry returns remote reference
(and stub gets created)
www.hndit.com
RMI Flow
Client Virtual Machine
Client
Object
Server Virtual Machine
7
5
Remote
Object
6
Skeleton
Stub
Server
myMessage
Registry Virtual Machine
5. Client invokes stub method
6. Stub talks to skeleton
7. Skeleton invokes remote object
method
www.hndit.com
RMI Usage
• Start registry
• Start server
• Run client
www.hndit.com
Creating Remote Objects
• Define a remote Interface
– Extends java.rmi.Remote
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Message extends Remote {
void sayHello(String name) throws RemoteException;
}
www.hndit.com
Creating Remote Objects
• Define a class that implements the remote interface
– Extends java.rmi.RemoteObject or java.rmi.UnicastRemoteObject
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MessageImpl extends UnicastRemoteObject implements Message
{
public MessageImpl() throws RemoteException {
}
@Override
public void sayHello(String name) throws RemoteException {
System.out.println("hello " + name);
}
}
www.hndit.com
Create the server
• Create a new instance of the remote object
• Registers it in the registry with unique name
private void startServer() {
try {
// create on port 1099
Registry registry = LocateRegistry.createRegistry(1099);
// create a new service named myMessage
registry.rebind("myMessage", new MessageImpl());
System.out.println("system is ready");
} catch (Exception e) {
e.printStackTrace();
}
}
www.hndit.com
Create the Client
• Find the registry
• Lookup for name to obtain a reference
• Cast the reference to the appropriate Remote Interface
private void doTest() {
try {
// fire to localhost port 1099
Registry myRegistry = LocateRegistry.getRegistry(“localhost",
1099);
// search for myMessage service
Message impl = (Message) myRegistry.lookup("myMessage");
// call server's method
impl.sayHello("HND");
System.out.println("Message Sent");
} catch (Exception e) {
e.printStackTrace();
}
}
www.hndit.com
Limitations of RMI
• Java Only
– Can use JNI on the server
• Uses TCP, not UDP
• At least two sockets per connection