Download Java RMI overview I

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
INF5040
Work group meeting no. 1
Åshild Grønstad Solheim
([email protected])
Outline
•
•
Some smaller exercises (voluntary)
Mandatory programming project:
– EJB / JBoss
– Changed from CORBA to EJB, new exercises
– No regular work group meetings during project period
•
Student presentations followed by discussions
•
Work in groups of 3: indicate preferences on who to work with
and what subject to present
•
No work group meeting next week
•
Today: Java RMI - overview, code example, exercise
4/5/2001
2
Java RMI overview I
•
Java RMI (Remote Method Invocation) in lecture next week
•
Middleware:
– A software layer
– Provides a convenient programming model for application
programmers
– Masks heterogenity of underlying networks, hardware, operating
systems and programming languages
•
Java RMI is middleware
– Provides components (e.g proxies, dispatchers) that hide the details
of marshalling, message passing and locating remote objects from
client and server programmers
– Only for the Java language
•
Java only => Interface Definition Language (IDL) not necessary. Ref.
CORBA (Common Object Request Broker Architecture)
4/5/2001
3
Java RMI overview II
• Request – reply protocol:
– Message structure : MessageType, CallId,
Remote Object Reference, Method,
Arguments
• Java RMI extends Java object model to support
distributed objects
• Invoke remote methods using familiar syntax
4/5/2001
4
Java RMI overview III
• Remote interface
– Extends Remote
– Public methods which throw RemoteExceptions
• Servant class
– Implements the remote interface
– Extends UnicastRemoteObject (or Activatable)
• Parameter and result passing
– Non-remote objects passed by value
– Remote objects passed by reference
4/5/2001
5
Java RMI overview IV
• Marshalling
– Translate data structures and primitive values to a form
suitable for transmission (external data representation)
– Java object serialization
– Unmarshalling: the reverse
• RMIregistry
– Non-persistent name server
– Rebind(String name, Remote obj), bind (String name,
Remote obj), unbind(String name, Remote obj),
lookup(String name), list()
4/5/2001
6
Java RMI overview V
• Client invokes remote method
• Server (contains main () method and servant
class (implements remote interface))
• Callback to client possible (ref. exercise)
– Server notifies client about event
4/5/2001
7
Java RMI architecture I
• NB! Skeleton no longer required.
• Generic dispatcher used instead
4/5/2001
8
Java RMI architecture II
• Stub/Proxy
– Client side stand-in for the remote object, same interface
– Marshals method and arguments into request message
– Unmarshals reply message
•
Generic dispatcher
– Reflection allows same dispatcher for all classes of remote objects
– Skeleton not required
– Unmarshals the request message
– Invokes the method
– Marshals result or exeption into reply message
• Remote Reference Module
– ROR => local object reference
4/5/2001
9
Java RMI development process
•NB! Skeleton no longer required.
4/5/2001
10
A distributed Hello World application
- The remote interface:
import java.rmi.*;
public interface HelloInterface extends Remote {
public String say() throws RemoteException;
}
4/5/2001
11
The servant class
(implements the remote interface):
import java.rmi.*;
import java.rmi.server.*;
public class Hello extends UnicastRemoteObject implements HelloInterface {
private String message;
public Hello (String msg) throws RemoteException {
message = msg;
}
public String say() throws RemoteException {
return message;
}
}
4/5/2001
12
The server:
import java.rmi.*;
public class HelloServer {
public HelloServer () { }
public static void main (String[] argv) {
try {
Naming.rebind ("Hello", new Hello ("Hello, world! Welcome to INF5040!"));
System.out.println ("Hello Server is ready.");
} catch (Exception e) {
System.out.println ("Hello Server failed: " + e);
}
}
}
4/5/2001
13
The client:
import java.rmi.*;
public class HelloClient {
public HelloClient () { }
public static void main (String[] argv) {
try {
HelloInterface hello = (HelloInterface) Naming.lookup ("//"+argv[0]+"/Hello");
System.out.println (hello.say());
} catch (Exception e) {
System.out.println ("HelloClient exception: " + e);
}
}
}
4/5/2001
14
Compilation
1. javac *.java
2. rmic Hello
•
rmic generates client proxy from compiled
server class, not from remote interface
definition.
•
If rmic does not find the class files, set
CLASSPATH to current directory:
setenv CLASSPATH .
4/5/2001
15
Running the distributed application
•
Server:
1. rmiregistry &
2. java HelloServer &
•
Client:
java HelloClient <server name – ex. kolme.ifi.uio.no>
4/5/2001
16
Discussion in groups
• How to implement callback in the Hello world
application?
• Server side: Suggest an additional method for printing
a greeting and performing a callback to the client.
– NB! Unrealistic test-application!
– Normally register to receive callback when event occurs
• Client side: Suggest an implementation for receiving
the callback from the server.
• Present and discuss proposals
• Hint: p. 214 in Coulouris..
4/5/2001
17
Until next meeting
• Find source code and Java RMI tutorial on
web
• Run the distributed Hello World application,
extend with callback to client
• Hopefully the subjects to present will be ready
• Email preferences on group and subject to
present
• Remember: no work group meeting next week
4/5/2001
18