失效链接处理 |
Java RMI Tutorial PDF 下载
本站整理下载:
相关截图:
主要内容:
Using Java RMI at Colby
Fall 2008
Stephanie Taylor
“The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine
to invoke methods on an object running in another Java virtual machine.”
(http://java.sun.com/docs/books/tutorial/rmi/overview.html)
This document describes my experience running, modifying, and re-running a distributed version of
“Hello World” from http://java.sun.com/j2se/1.5.0/docs/guide/rmi/hello/hello-world.html. The basic
setup for the “Hello World” example (and for any simple client-server example) is this: There are two
machines - one we will call the server (lowercase), the other we will call the client (lowercase). 1 The
server machine will run a Java program called Server (title case) and the client machine will run a java
program called Client (title case). Server is a Remote object, i.e. an object with methods that can
be invoked by Client via an internet/intranet connection. To initiate the communication between the
server and client, a third program – the RMI registry must be running on the server. See Figure 1.
Figure 1: The rmiregistry and Server run on the server. The Client runs on the client. The registry is
used to initiate communication between Client and Server, but they then communicate “directly”
(this “direct” communication may be mediated by a web server).
Hello World Code
The code for the “Hello World” example is in a package example.hello. This means that the
.class files must be placed into a directory example/hello. I created a directory
/Users/srtaylor/Documents/CS336/java_rmi for all of the examples in this tutorial. I then
created a subdirectory example/hello for this example and placed Hello.java, Client.java, and
Server.java in it.
Let’s look at the code. Here is Hello.java:
1 In a client-server model there may be (and often is) more than one client. Each client is identical to
every other, so I simply refer to “the” client here.
package example.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
It declares that it is in the example.hello package and imports the Remote and
RemoteException classes. The Hello interface extends the Remote class. This means that any
class implementing Hello will be a Remote class. Any method in a Remote object must throw a
RemoteException. Thus the method sayHello is declared as one that has the potential to throw
a RemoteException.
Let’s move on to Server.java. Here is its code:
package example.hello;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
System.out.println("Running sayHello()");
return "Hello, world!";
}
public static void main(String args[]) {
int rport = (args.length < 1) ? 8080 : Integer.parseInt(args[0]);
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj,0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry(rport);
registry.rebind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
First, we noti
|