0% found this document useful (0 votes)
69 views9 pages

RMI Handout

Internet programming Hassan Khan, PUCIT. RMI provides a Naming Service through the RMI Registry that simplifies how programs specify the location of remote objects.

Uploaded by

Aaine Siddiqui
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views9 pages

RMI Handout

Internet programming Hassan Khan, PUCIT. RMI provides a Naming Service through the RMI Registry that simplifies how programs specify the location of remote objects.

Uploaded by

Aaine Siddiqui
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

3/30/2010

Internet Programming
Hassan Khan, PUCIT. 1

 Sockets programming: design a protocol first,


then implement clients and servers that
support the protocol.
 RMI: Develop an application, then move some
objects to remote machines.
◦ Not concerned with the details of the actual
communication between processes – everything is
just method calls.

Internet Programming
Hassan Khan, PUCIT. 2

1
3/30/2010

 Method Call Semantics – what does it mean to


make a call to a method?
◦ How many times is the method run?
◦ How do we know the method ran at all?
 RMI does a great job of providing natural call
semantics for remote objects/methods.
◦ Simply a few additional Exceptions that you need to
handle.

Internet Programming
Hassan Khan, PUCIT. 3

 It would be awkward if we needed to include


a hostname, port and protocol with every
remote method invocation.
 RMI provides a Naming Service through the
RMI Registry that simplifies how programs
specify the location of remote objects.
◦ This naming service is a JDK utility called
rmiregistry that runs at a well known address (by
default).

Internet Programming
Hassan Khan, PUCIT. 4

2
3/30/2010

Client App. Server App.

Stubs Skeleton

Remote Reference Remote Reference

Transport Transport

Internet Programming
Hassan Khan, PUCIT. 5

 The client acquired a reference to a remote


object.
◦ This part is different from creating a local
object.
 The client calls methods on the remote
object
◦ No (syntactic) difference!
◦ Just need to worry about a few new exceptions.

Internet Programming
Hassan Khan, PUCIT. 6

3
3/30/2010

 Define an interface that declares the


methods that will be available remotely.
 The server program must include a
class the implements this interface.
 The server program must create a remote
object and register it with the naming
service.

 The client program creates a remote


object by asking the naming service for
an object reference.

Internet Programming
Hassan Khan, PUCIT. 7

 Create an interface the extends the Remote


interface.
◦ This new interface includes all the public
methods that will be available as remote
methods.

public interface MyRemote extends Remote {


public int foo(int x) throws RemoteException;
public String blah(int y) throws RemoteException;
. . .
}

Internet Programming
Hassan Khan, PUCIT. 8

4
3/30/2010

 Create a class that implements the interface.


◦ The class should also extend UnicastRemoteObject*
 This class needs a constructor that throws
RemoteException !
 This class is now used by rmic to create the
stub and skeleton code.

*It doesn’t have to extend UnicastRemoteObject, there is another way…

Internet Programming
Hassan Khan, PUCIT. 9

public class MyRemoteImpl extends


UnicastRemoteObject implements MyRemote {

public MyRemoteImpl() throws RemoteException {


}

public int foo(int x) {


return(x+1);
}
public String blah(int y) {
return(“Your number is “ + y);
}
}

Internet Programming
Hassan Khan, PUCIT. 10

5
3/30/2010

 Compile the remote interface and


implementation:

> javac MyRemote.java MyRemoteImpl.java

 Use rmic to generate


MyRemoteImpl_stub.class, MyRemoteImpl_skel.class

> rmic MyRemoteImpl

Internet Programming
Hassan Khan, PUCIT. 11

 The server main() needs to:


◦ create a remote object.
◦ register the object with the Naming service.

public static void main(String args[]) {


try {
MyRemoteImpl r = new MyRemoteImpl();
Naming.bind(“joe”,r);
} catch (RemoteException e) {
. . .

Internet Programming
Hassan Khan, PUCIT. 12

6
3/30/2010

 The client needs to ask the naming service


for a reference to a remote object.
◦ The client needs to know the hostname or IP
address of the machine running the server.
◦ The client needs to know the name of the remote
object.
 The naming service uses URLs to identify
remote objects.

Internet Programming
Hassan Khan, PUCIT. 13

 Naming.lookup() method takes a string


parameter that holds a URL indicating the
remote object to lookup.
rmi://hostname/objectname

 Naming.lookup() returns an Object!


 Naming.lookup() can throw
◦ RemoteException
◦ MalformedURLException

Internet Programming
Hassan Khan, PUCIT. 14

7
3/30/2010

try {
Object o =
naming.lookup(“uri://monica.cs.rpi.edu/joe”);

MyRemote r = (MyRemote) o;

. . . Use r like any other Java object! . . .


} catch (RemoteException re) {
. . .
} catch (MalformedURLException up) {
throw up;
}

Internet Programming
Hassan Khan, PUCIT. 15

 First you need to run the Naming service


server:
start rmiregistry

 Now run the server:


java ServerMain

Internet Programming
Hassan Khan, PUCIT. 16

8
3/30/2010

Internet Programming
Hassan Khan, PUCIT. 17

You might also like