Docsity

Java Remote Method Invocation (RMI) in Computer Network Programming - Prof. W. Xu, Study notes of Computer Science

Toggle sidebar
bg1
CSCE 515:
Computer Network
Programming
------ Remote Method Invocation
reference: Dave Hollinger
Wenyuan Xu
Department of Computer Science and
Engineering
University of South Carolina
Netprog: Java RMI 2
Network Programming Paradigms
๎š„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.
Netprog: Java RMI 3
Call Semantics
๎š„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.
Netprog: Java RMI 4
Finding Remote Objects
๎š„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).
Netprog: Java RMI 5
RMI Adds a few layers
Server App.
Skeleton
Remote Reference
Transport
Client App.
Stubs
Remote Reference
Transport
Netprog: Java RMI 6
Remote Object References
๎š„The client acquires 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.
pf3
pf4

Partial preview of the text

Download Java Remote Method Invocation (RMI) in Computer Network Programming - Prof. W. Xu and more Study notes Computer Science in PDF only on Docsity!

CSCE 515:

Computer Network

Programming

------ Remote Method Invocation

reference: Dave Hollinger Wenyuan Xu

Department of Computer Science and Engineering University of South Carolina

Netprog: Java RMI^2

Network Programming Paradigms

ย„ 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.

Netprog: Java RMI^3

Call Semantics

ย„ 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.

Netprog: Java RMI^4

Finding Remote Objects

ย„ 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).

RMI Adds a few layers

Server App. Skeleton Remote Reference Transport

Client App. Stubs Remote Reference Transport

Remote Object References

ย„ The client acquires 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.

Netprog: Java RMI^7

Overview of RMI Programming

ย„ Define an interface that declares the methods that

will be available remotely.

ย„ The server program must include a class that

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.

Netprog: Java RMI^8

Java Interfaces

ย„ Similar to Class

ย„ No implementation! All methods are

abstract (virtual for C++ folks).

ย„ Everything is public.

ย„ No fields defined, just Methods.

ย„ No constructor

ย„ an Interface is an API that can be

implemented by a Class.

Netprog: Java RMI^9

Interfaces and Inheritence

ย„ In Java a class can only extend a single

superclass (single inheritence).

ย„ A class can implement any number of

interfaces.

end result is very similar to multiple inheritence.

Netprog: Java RMI^10

Sample Interface

public interface Shape { public getArea(); public draw(); public fill(Color c); }

Implementing an Interface

public class Circle implements Shape {

private double radius;

private Point center;

// define a constructor and other

// methods

// MUST define the methods:

// getArea();

// draw();

// public fill(Color c);

Server Details โ€“ extending Remote

ย„ Create an interface the extends the java.rmi.Remote interface.

This new interface includes all the public methods

that will be available as remote methods.

import java.rmi.*; public interface MyRemote extends Remote { public int foo(int x) throws RemoteException; public String blah(int y) throws RemoteException;

... }

Netprog: Java RMI^19

Using The Naming service

ย„ 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

Netprog: Java RMI^20

Getting a Remote Object

try { Object o = Naming.lookup(โ€œrmi://localhost/ReMathโ€);

MyRemote r = (MyRemote) o; //... Use r like any other Java object! } catch (RemoteException re) {

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

Starting the Server

ย„ First you need to run the Naming service

server:

rmiregistry &

ย„ Now run the server:

java ServerMain

Sample Code

ย„ There is sample RMI code on the course

homepage:

RemoteMathImpl: remote integer arithmetic