Download ppt - CS 584 Lecture 18

Transcript
Page 1: CS 584 Lecture 18

CS 584 Lecture 18

Assignment» Glenda assignment extended to the

Java RMI Deadline» No Java RMI Assignment

Test» Friday, Saturday, Monday

Page 2: CS 584 Lecture 18

Java RMI

Page 3: CS 584 Lecture 18

RMI Programming Steps

Coding the source files Compilation and Deployment Running

Page 4: CS 584 Lecture 18

Coding the Source Files

There are at least 3 source files » Interface definition» Server Implementation» Client Implementation

–HTML file if applet

Page 5: CS 584 Lecture 18

Interface Definition

Interface must be public Extends java.rmi.Remote Must declare java.rmi.RemoteException

in throws clause

Page 6: CS 584 Lecture 18

Interface Definition Example

package examples.hello

public interface Hello extends java.rmi.Remote{

String sayHello() throws java.rmi.RemoteException;}

Page 7: CS 584 Lecture 18

Server Implementation

Specifies the remote interface Defines the constructor Implements methods Creates and Installs a security manager Creates instances of the remote object Registers the remote object with the

RMI remote object registry

Page 8: CS 584 Lecture 18

Server Implementation

package examples.helloimport java.rmi.*;import java.rmi.server.UnicastRemoteObject

public class HelloImpl extends UnicastRemoteObject implements Hello

{private String name;

Page 9: CS 584 Lecture 18

Server Implementation

public HelloImpl(String s) throws RemoteException{

super();name = s;

}

public String sayHello() throws RemoteException{

return "Hello World";}

Page 10: CS 584 Lecture 18

Server Implementation

public static void main(String[] args){

System.setSecurityManager(new RMISecurityManager());

try {HelloImpl obj = new HelloImpl("HelloServer");Naming.rebind("//myhost/HelloServer", obj);System.out.println("Hello Server bound");

catch(Exception e) {System.out.println("Err: " + e.getMessage);e.printStackTrace();

}}

Page 11: CS 584 Lecture 18

Client Implementation

Obtain a reference to the "HelloServer" from the server's registry

Invoke the method on the remote object Use the returned results

Page 12: CS 584 Lecture 18

Client Implementation

package examples.hello

import java.awt.*;import java.rmi.*;

public class HelloApplet extends java.applet.Applet{

String message = " ";

Page 13: CS 584 Lecture 18

Client Implementation

public void init(){

try {Hello obj = (Hello)Naming.lookup("//" +

getCodeBase().getHost() + "/HelloServer");message = obj.sayHello();

}catch (Exception e) {

System.out.println("Err: " + e.getMessage);e.printStackTrace();

}}

Page 14: CS 584 Lecture 18

Client Implementation

public void paint(Graphics g){

g.drawString(message, 25,50);}

Page 15: CS 584 Lecture 18

Compilation

Compile the source files using javac Generate Stub and Skeleton files

» client side and server side proxies» generated using rmic on class files

Page 16: CS 584 Lecture 18

Deployment

RMI is based on packages RMI objects need to be in a visible place

Page 17: CS 584 Lecture 18

Execution

Start the registry on the server» start rmiregistry

Start the server using the java interpreter Run the applet

Page 18: CS 584 Lecture 18

For More Information

See Sun's website for a tutorial» web3.javasoft.com:80/products/jdk/

1.1/docs/guide/rmi/getstart.doc.html Remember the test!!!!!