交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系...

80
交交交交交交交交交交 Programming in Java RMI 蔡蔡蔡 蔡蔡蔡蔡蔡蔡蔡蔡蔡蔡 [email protected] http://www.csie.nctu.edu.tw/~tsaiwn/java /
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    270
  • download

    15

Transcript of 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系...

Page 1: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系

Programming in Java

RMI

蔡文能交通大學資訊工程學系[email protected]

http://www.csie.nctu.edu.tw/~tsaiwn/java/

Page 2: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 2頁

Java RMI

Agenda

RMI IntroductionRPC vs. RMIRMI DeploymentRMI Example

Page 3: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 3頁

Java RMI

Page 4: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 4頁

Java RMI

Page 5: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 5頁

Java RMI

RMI Introduction RMI enables the programmer to create distributed Java

applications, in which the methods of remote Java objects can be invoked from other Java virtual machines, possibly on different hosts.

A Java program can make a call on a remote object once it obtains a reference to the remote object, either by looking up the remote object in the naming service provided by RMI or by receiving the reference as an argument or a return value. A client can call a remote object in a server, and that server can also be a client of other remote objects.

RMI uses object serialization to marshal and unmarshal parameters.

Page 6: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 6頁

Java RMI

Serialization

Action of encoding of an object into a stream of bytes

RMI performs marshalling/unmarshalling via Java Serialization Marshalling is the process of encoding arguments and results for

transmission

Thus, objects to be marshalled or unmarshalled must be serializable

Page 7: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 7頁

Java RMI

RMI Overview

Remote Method Invocation Java version of RPC

ha.nctu.edu.tw hehe.nctu.edu.tw

networknetworkObjectClient

RemoteObjectServer

method invocation

Page 8: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 8頁

Java RMI

blah, blah, blah

bar = foo(a,b);

blah, blah, blah

blah, blah, blah

bar = foo(a,b);

blah, blah, blah

int foo(int x, int y ) { if (x>100)

return(y-2); else if (x>10)

return(y-x); else

return(x+y);}

int foo(int x, int y ) { if (x>100)

return(y-2); else if (x>10)

return(y-x); else

return(x+y);}

ClientClient

ServerServer

protocol

Remote Procedure Call (RPC)

RPCRPC

Page 9: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 9頁

Java RMI

Middleware Layers

Applications

Middlewarelayers Request reply

protocoleXternal Data Representation(XDR)

Operating System

RPC, RMI, and events

分層負責 , 分工合作

Page 10: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 10頁

Java RMI

RPC – Remote Procedure CallThere are a number of popular RPC specifications.

Sun RPC is widely used.

NFS (Network File System) is RPC based.

RPC clients are processes that call remote procedures.

RPC servers are processes that include procedure(s) that can be called by clients.

Rich set of support tools.The RPC library is a collection of tools for automating the creation of RPC clients and servers.

Page 11: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 11頁

Java RMI

Sun RPC

RPCGEN There is a tool for automating the creation of RPC clients and

servers. The program rpcgen does most of the work for you. The input to rpcgen is a protocol definition in the form of a list of

remote procedures and parameter types.

Page 12: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 12頁

Java RMI

RPCGEN

Input File

rpcgen

Client Stubs XDR filters header file Server skeleton

C Source CodeC Source Code

ProtocolProtocolDescriptionDescription

foo_clnt.c foo_svc.c

foo.xx

% rpcgen –C foo.xx

foo_xdr.c foo.h

Page 13: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 13頁

Java RMI

RPC Programming

RPC library XDR routines RPC run time library

call rpc service

register with portmapper

dispatch incoming request to correct procedure

Program Generatorrpcgen

Page 14: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 14頁

Java RMI

RPC Run-time Library

High- and Low-level functions that can be used by clients and servers.

High-level functions provide simple access to RPC services. For client: int callrpc( . . . ) For server: int registerrpc( . . . ) svc_run() is a dispatcher on server

A dispatcher waits for incoming connections and invokes the appropriate function to handle each incoming request.

Page 15: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 15頁

Java RMI

Procedure Arguments

To reduce the complexity of the interface specification, Sun RPC includes support for a single argument to a remote procedure.*Typically the single argument is a structure that contains a number of values.

* Newer versions can handle multiple args.

Page 16: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 16頁

Java RMI

Procedure Identification

Each procedure is identified by: Hostname (IP Address) Program Identifier (32 bit integer) Procedure Identifier (32 bit integer)

Program Version identifierfor testing and migration.

Page 17: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 17頁

Java RMI

Program Identifiers

Each remote program has a unique ID.

Sun divided up the IDs:

0x00000000 - 0x1fffffff

0x20000000 - 0x3fffffff

0x40000000 - 0x5fffffff

0x60000000 - 0xffffffff

SunSun

SysAdmin SysAdmin

TransientTransient

ReservedReserved

Page 18: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 18頁

Java RMI

Procedure Identifiers &Program Version Numbers

Procedure Identifiers usually start at 1 and are numbered sequentially

Version Numbers typically start at 1 and are numbered sequentially.

Page 19: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 19頁

Java RMI

Iterative ServerSun RPC specifies that at most one remote procedure within a program can be invoked at any given time.

If a 2nd procedure is called, the call blocks until the 1st procedure has completed.

Having an iterative server is useful for applications that may share data among procedures.

Example: database - to avoid insert/delete/modify collisions.

We can provide concurrency when necessary...

Page 20: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 20頁

Java RMI

RPC example (1/7)This program shows you how to use a server program, a client program and an interface definition file to let the client program call the functions in the server program and get the results.Files: ① test_proc.c --- the server file ② test_client.c --- the client file ③ test.xx --- the Interface Definition file of RPC Files generated by rpcgen: rpcgen –C test.xx

test_clnt.c test_svc.c test.h

Page 21: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 21頁

Java RMI

RPC example (2/7)File test_proc.c (1/1) for server side

#include "test.h"int *addd_1_svc(int *argp, struct svc_req *rqstp){ static int result; result = *argp + 1; return (&result);}int *decc_1_svc(int *argp, struct svc_req *rqstp){ static int result; result = *argp - 1; return (&result);}

Page 22: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 22頁

Java RMI

RPC example (3/7)File test_client.c (1/3) for client side

#include "test.h" /* test_client.c , page1 */void test_prog_1(char *host){ CLIENT *clnt; int *result_1; int addd_1_arg; int *result_2; int decc_1_arg; #ifndef DEBUG clnt = clnt_create(host, TEST_PROG, TEST_VERS, "netpath"); if (clnt == (CLIENT *) NULL) /* NULL is 0 */ { clnt_pcreateerror(host); exit(1); } /* to be continued */

Page 23: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 23頁

Java RMI

RPC example (4/7)File test_client.c (2/3) for client side

#endif /* DEBUG */ /* test_client.c , page2 */ scanf ("%d",&addd_1_arg); scanf ("%d",&decc_1_arg); result_1 = addd_1(&addd_1_arg, clnt); if (result_1 == (int *) NULL) { clnt_perror(clnt, "call failed"); } result_2 = decc_1(&decc_1_arg, clnt); if (result_2 == (int *) NULL) { clnt_perror(clnt, "call failed"); } printf ("addd_1_result = %d\n",*result_1); printf ("decc_1_result = %d\n",*result_2); #ifndef DEBUG clnt_destroy(clnt); #endif/* DEBUG */} /* test_prog_1 *//* to be continued */

Page 24: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 24頁

Java RMI

RPC example (5/7)File test_client.c (3/3) for client side

/* test_client.c , page3 */main(int argc, char *argv[]){ char *host; if (argc < 2) /* no host name given */ { printf("usage: %s server_host\n", argv[0]); exit(1); } host = argv[1]; test_prog_1(host);}

Page 25: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 25頁

Java RMI

RPC example (6/7)File : test.xx

program TEST_PROG{ version TEST_VERS { int ADDD(int)=1; int DECC(int)=2; }=1;}=0x31234567;

rpcgen –C test.xx

Page 26: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 26頁

Java RMI

RPC example (7/7)Use rpcgen , C compiler, and Linking/loader By rpcgen test.xx , you can get test_clnt.c, test_svc.c and test.h. Compile/link on the client

gcc -o test test_client.c test_clnt.c -lnsl

Compile/link/run on the server (ccsun2.csie.nctu.edu.tw)

gcc -o test_svc test_svc.c test_proc.c -lrpcsvc -lnsl

./test_svc&

Run clien program on the client

./test ccsun2.csie.nctu.edu.tw

Demo on Sun machines

Page 27: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 27頁

Java RMI

rpcgen –C test.xx

Page 28: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 28頁

Java RMI

Test RPC -- Server Side

Page 29: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 29頁

Java RMI

Test RPC -- Client Side

把第一個數加 1,

把第二個數減一

輸入兩列 : 57 89

Page 30: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 30頁

Java RMI

Another example of rpcgen file

struct readargs {FileIdentifier f;FilePointer position;Length length;

};

program FILEREADWRITE { version VERSION {

void WRITE(writeargs)=1;Data READ(readargs)=2;2

}=2;} = 9999;

const MAX = 1000;typedef int FileIdentifier;typedef int FilePointer;typedef int Length;struct Data {

int length;char buffer[MAX];

};struct writeargs {

FileIdentifier f;FilePointer position;Data data;

};

Page 31: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 31頁

Java RMI

High-Level Library Limitation

The High-Level RPC library calls support UDP only (no TCP).

You must use lower-level RPC library functions to use TCP.

The High-Level library calls do not support any kind of authentication.

Page 32: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 32頁

Java RMI

Low-level RPC Library

Full control over all IPC options TCP & UDP Timeout values Asynchronous procedure calls

Multi-tasking Servers

Broadcasting

IPC : Inter-Process Communication

Page 33: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 33頁

Java RMI

The General RMI Architecture

The server must first bind its name to the registry

The client lookup the server name in the registry to establish remote references.

The Stub serializing the parameters to skeleton, the skeleton invoking the remote method and serializing the result back to the stub.

RMI Server

skeleton

stub

RMI Client

Registry

bind

lookupreturn call

Local Machine

Remote Machine

Page 34: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 34頁

Java RMI

The Stub and Skeleton

A client invokes a remote method, the call is first forwarded to stub.

The stub is responsible for sending the remote call over to the server-side skeleton

The stub opening a socket to the remote server, marshaling the object parameters and forwarding the data stream to the skeleton.

A skeleton contains a method that receives the remote calls, unmarshals the parameters, and invokes the actual remote object implementation.

Stu

b

RMI Client RMI Server

skeleton

return

call

Page 35: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 35頁

Java RMI

Stubs

Stub Client side Acts as an implementation of a remote interface Communicates with the real object over the network The stub class is generated from corresponding remote class

by the RMI compiler rmic

Note: As of the J2SE 5.0 release, stub classes for remote objects no longer need to be pregenerated using the rmic stub compiler, unless the remote object needs to support clients running in pre-5.0 VMs.

Page 36: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 36頁

Java RMI

skeletons

Skeleton Server side Carries on a conversation with the stub; Reads the parameters for the method call from the link Call the real remote service Writes the return value from the service back to the stub

Page 37: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 37頁

Java RMI

JVM Asociated

to ActivationGroup

RMI (in Java)

Client Code

network

NamingService

Server Object

ActivationDaemonClient

Stub

ServerSkeleton

Implement “Factories” using declarative descriptions of activatable objects

One JVM per ActivationGroup. Automatically launched by Activation daemon and contains (potentially) many small scale, semi-independent servers which can share resources (like connection pools) and which live under the same security restrictions

“Flat” directory structure which attempts to provide some notion of location transparency-- client code uses a single name to find a server

Page 38: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

Copyright ?1997 Alex Chaffee

交通大學資訊工程學系 蔡文能 10-第 38頁

Java RMI

RMI Layers

TCPRemote Reference Layer

Transport Layer

Java Virtual Machine

Client Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Page 39: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 39頁

Java RMI

Remote reference layer

Defines and supports the invocation semantics of the RMI connection

Provides a RemoteRef object that represents the link to the remote service implementation object

The stub objects use the invoke() method in RemoteRef to forward the method call

Page 40: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 40頁

Java RMI

Transport layer

Provides basic connectivity makes the connection between JVMs

Based on TCP/IP connections between machines in a network

Page 41: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 41頁

Java RMI

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

Page 42: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 42頁

Java RMI

Find Servers using Naming Service

Simple interface to registry

Implemented via 5 static methods on an object:

public static String[] list(String name) public static Remote lookup(String name) public static void bind(String name, Remote obj) public static void rebind(String name, Remote obj) public static void unbind(String name)

Page 43: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 43頁

Java RMI

java.rmi.Naming

Page 44: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 44頁

Java RMI

java.rmi.registry.LocateRegistry

Page 45: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 45頁

Java RMIjava.rmi.registry.Registry

Page 46: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 46頁

Java RMI

RMI Deployment

RMI Deployment( 佈署 ): Where does the server program finds it’s classes? Where does the client program find it’s classes? How are classes loaded from client to server and vice-versa?

Remember that Object Serialization does not send the classes, only the data

Page 47: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 47頁

Java RMI

Steps for Developing an RMI System

1. Define the Remote interface

2. Develop the remote object by implementing the remote interface.

3. Develop the client program.

4. Compile the Java source files.

5. Generate the client stubs and server skeletons.

6. Start the RMI registry.

7. Start the remote server objects.

8. Run the client

Page 48: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 48頁

Java RMI

RMI Class Loading It’s all in the codebase

Source from which to load classes into JVM Usual CLASSPATH variable “local codebase”

Set this while running both the server and the client but not rmiregistry RMI client needs stubs from server

Stubs downloaded from the server to the client using the java.rmi.server.codebase property

RMI server may need classes from the clientClasses downloaded from the client to the server using the java.rmi.server.codebase property setup by the client

RMI registry needs to find stub classesIf it finds them from CLASSPATH it will not convey the “true” code base associated with the stub class even if the server sets it.

Searches “local” code base initially, then searches the “java.rmi.server.codebase”

Page 49: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 49頁

Java RMI

RMIDownloading Stubs

Page 50: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 50頁

Java RMI

RMI

Downloading Classes

Page 51: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 51頁

Java RMI

Java Interfaces

Similar to Class

No implementation! All methods are abstract (virtual for C++ ).

Everything is public.

No constructor

an Interface is an API that can be implemented by a Class .

Page 52: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 52頁

Java RMI

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.

Page 53: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 53頁

Java RMI

Creating Remote object

1. Define an interface which extends Remote interface

2. Create the server program which implements that interface

3. Compile the Java program

4. Create the Stub

Note: As of the J2SE 5.0 release, stub classes for remote objects no longer need to be pregenerated using the rmic stub compiler, unless the remote object needs to support clients running in pre-5.0 VMs.

Page 54: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 54頁

Java RMI

Create the Stub

A. In JBuilder use “JNI/RMI” tab in class properties to generate it automatically

B. By hand: use rmic tool:

rmic ComputeEngine

Note: As of the J2SE 5.0 release, stub classes for remote objects no longer need to be pregenerated using the rmic stub compiler, unless the remote object needs to support clients running in pre-5.0 VMs.

Page 55: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 55頁

Java RMI

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;

. . .

}

Page 56: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 56頁

Java RMI

How the interface will be used

Class for your Remote Object

Your Interface UnicastRemoteObject

Remote Interface Class RemoteServer

extends

implementsextends

extendsprovides methods needed by

Page 57: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 57頁

Java RMI

Server Details – Implementation Class

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…

Note: As of the J2SE 5.0 release, stub classes for remote objects no longer need to be pregenerated using the rmic stub compiler, unless the remote object needs to support clients running in pre-5.0 VMs.

Page 58: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 58頁

Java RMI

Generating stubs and skeleton

Compile the remote interface and implementation:

> javac MyRemote.java MyRemoteImpl.java

Use rmic to generate MyRemoteImpl_stub.class, MyRemoteImpl_skel.class

> rmic MyRemoteImpl

Note: As of the J2SE 5.0 release, stub classes for remote objects no longer need to be pregenerated using the rmic stub compiler, unless the remote object needs to support clients running in pre-5.0 VMs.

Page 59: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 59頁

Java RMI

Server Detail – main()

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();

javaNaming.bind(“joe”,r);

} catch (RemoteException e) {

. . .

Page 60: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 60頁

Java RMI

Client Details

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.

rmi://hostname/objectname

Page 61: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 61頁

Java RMI

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

Page 62: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 62頁

Java RMI

Page 63: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 63頁

Java RMI

Page 64: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 64頁

Java RMI

Page 65: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 65頁

Java RMI

Page 66: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 66頁

Java RMI

Page 67: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 67頁

Java RMI

RMI registry (1/2)

A naming service Maps names to remote objects Provides clients with a mechanism to find remote services

running on RMI servers

Essential operations: bind/rebind, unbind, lookup Bind adds a service entry to the registry Unbind removes a service entry from the registry Lookup allows clients to find the service’s address using

service name

Page 68: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 68頁

Java RMI

RMI registry (2/2)

Names in the registry use unique names Recommend the name of the remote class that implements

the remote interface

For accessing a remote registry, use the following URL form rmi://host:port/serviceName host is the machine on which the registry is running The registry is listening on the port (default 1099)

Page 69: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 69頁

Java RMI

Page 70: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 70頁

Java RMI

Page 71: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 71頁

Java RMI

Page 72: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 72頁

Java RMI

Start the Java RMI registry

By default, the registry runs on TCP port 1099. To start a registry on a different port, specify the port number from the command line. For example, to start the registry on port 2001 on a Windows platform:

start rmiregistry 2001

If the registry will be running on a port other than 1099, you'll need to specify the port number in the calls to LocateRegistry.getRegistry in the Server and Client classes. For example, if the registry is running on port 2001 in this example, the call to getRegistry in the server would be: Registry registry =

LocateRegistry.getRegistry(2001);

Page 73: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 73頁

Java RMI

Page 74: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 74頁

Java RMI

Java RMI classes

Java.rmi.Remote Interface supporting remote objects

java.rmi.server.UnicastRemoteObject Continuously running server

java.rmi.activation.Activatable Server started by rmid daemon

java.rmi.Naming Lookup: Returns stub given a name

Java.rmi.registry.Registry , LocateRegistryjava.rmi.RMISecurityManager Validates rights to access downloaded object

Page 75: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 75頁

Java RMI

Other Technologies

CORBA XML based

SOAP / Web Service

J2EE/EJB MicroSoft’s .NET

Has Common Language Runtime

Tight Integration of Web Services and XML

Multiple language integration However only one platform Windows

Page 76: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 76頁

Java RMI

Reference RMI docs

java.sun.com/products/jdk/rmi/ Java Tutorial on RMI:

http://java.sun.com/j2se/1.5.0/docs/guide/rmi

JavaIDLjava.sun.com/docs/books/tutorial/idl

java.sun.com/products/jdk/idl/

www.omg.org/news/begin.htm

Jiniwww.artima.com/jini/resources

Page 77: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 77頁

Java RMI

Page 78: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 78頁

Java RMI

Programming Paradigms

Imperative Programming (FORTRAN, C, Pascal, …) The most common programming paradigm

Functional Programming (LISP, …)

Logic Programming (Prolog)

(Declarative programming language; 宣告式語言 )

Object-Oriented Programming

(Smalltalk, C++, Java, …)

Simply using C++/Java constructs does not automatically lead to well-organized Object-Oriented Programs.

Page 79: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 79頁

Java RMI

Why OO Programming?Better concepts and tools to model and represent the real world as closely as possible (including concurrency, e.g., in Windows GUI)=> model of reality=> behavior modelingBetter reusability & extensibility (inheritance)=> reduce the time/cost of development

Enhanced maintainability & improved reliability – “Encapsulation” and “Information Hiding” Object only accessible through the external interface Internal implementation details are not visible outside Localized changes to implementation of classes Completeness: all required operations are defined Independent testing and maintenance

Help writing good program more easily

Page 80: 交通大學資訊工程學系 Programming in Java RMI 蔡文能 交通大學資訊工程學系 tsaiwn@csie.nctu.edu.tw tsaiwn/java//java

交通大學資訊工程學系 蔡文能 10-第 80頁

Java RMI

RPC / RMI

謝謝捧場http://www.csie.nctu.edu.tw/~tsaiwn/java/

蔡文能