Programming in java
This presentation is the property of its rightful owner.
Sponsored Links
1 / 100

Programming in Java PowerPoint PPT Presentation


  • 51 Views
  • Uploaded on
  • Presentation posted in: General

Programming in Java. RMI 蔡文能 交通大學資訊工程學系 [email protected] http://www.csie.nctu.edu.tw/~tsaiwn /java/. Agenda. RMI Introduction RPC vs. RMI Remote Procedure Call Remote Method invocation RPC example RMI Architecture Client stub and Server skeleton

Download Presentation

Programming in Java

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Programming in java

Programming in Java

RMI

蔡文能

交通大學資訊工程學系

[email protected]

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


Agenda

Agenda

  • RMI Introduction

  • RPC vs. RMI

    • Remote Procedure Call

    • Remote Method invocation

  • RPC example

  • RMI Architecture

    • Client stub and Server skeleton

    • RMI Deployment and RMI registry

    • RMI Example

  • From function to Distributed computing

    • DLL, RPC/RMI, CORBA, Web Services


Rmi introduction

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.


Serialization

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 ( should implements java.io.serializable)


Rmi overview

RMI Overview

  • Remote Method Invocation

    • Java version of RPC

network

Object

Client

Remote

Object

Server

method invocation

ha.nctu.edu.tw

hehe.nctu.edu.tw


Remote procedure call rpc

Remote Procedure Call (RPC)

Client

Server

blah, blah, blah

bar = foo(a,b);

blah, blah, blah

RPC

int foo(int x, int y ) {

if (x>100)

return(y-2);

else if (x>10)

return(y-x);

else

return(x+y);

}

protocol


Why rpc rmi

Why RPC/RMI

Calling a function/method at a remote server:

  • Advanced form of communication

    • Sockets and MPI: data transfer

    • RPC/RMI: control transfer

  • Transparency in function calls

    • No distinguish between local and remote calls (in theoretical)

    • Also applied to IPC on the same machine

  • Ease of use

    • Compiled-time argument type checking

    • Automatic stub generation

IPC : Inter-Process Communication


Middleware layers

Applications

RPC, RMI, and events

Middleware

Request reply protocol

layers

eXternal Data Representation(XDR)

Operating System

Middleware Layers

分層負責, 分工合作


Rpc remote procedure call

RPC – Remote Procedure Call

  • There 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.


Sun rpc

Sun RPC

  • RPCGEN command

    • There is a tool for automating the creation of RPC clients and servers.

    • The program rpcgendoes most of the work for you. This rpcgen is usually knows as RPC compiler.

    • The input to rpcgen is a protocol definition in the form of a list of remote procedures and parameter types.


Rpcgen

RPCGEN

Protocol

Description

Input File

foo.xx

rpcgen

% rpcgen –C foo.xx

Client Stubs

Server skeleton

header file

XDR filters

foo_clnt.c

foo_xdr.c

foo.h

foo_svc.c

C Source Code


Rpc programming

RPC Programming

  • RPC Library

    • XDR routines

    • RPC run time library

      • call rpc service

      • register with portmapper

      • dispatch incoming request to correct procedure

    • Program Generator

      • rpcgen


Rpc run time library

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.


Procedure arguments

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.


Procedure identification

Procedure Identification

  • Each procedure is identified by:

    • Hostname (IP Address)

    • Program Identifier (32 bit integer)

    • Procedure Identifier (32 bit integer)

    • Program Version identifier

      • for testing and migration.


Program identifiers

Program Identifiers

  • Each remote program has a unique ID.

  • Sun divided up the IDs:

    0x00000000 - 0x1fffffff

    0x20000000 - 0x3fffffff

    0x40000000 - 0x5fffffff

    0x60000000 - 0xffffffff

Sun

SysAdmin

Transient

Reserved


Procedure identifiers program version numbers

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.


Iterative server

Iterative Server

  • Sun 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...


Rpc example 1 7

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


Rpc example 2 7

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

}


Rpc example 3 7

RPC example (3/7)

  • File test_client.c (1/3) for client side

#include "test.h" /* test_client.c , page1 */

voidtest_prog_1(char *host)

{

CLIENT *clnt;

int *result_1;

int addd_1_arg;

int *result_2;

int decc_1_arg;

#ifndefDEBUG

clnt = clnt_create(host, TEST_PROG, TEST_VERS, "netpath");

if (clnt == (CLIENT *) NULL) /* NULL is 0 */

{

clnt_pcreateerror(host);

exit(1);

} /* to be continued */


Rpc example 4 7

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

#ifndefDEBUG

clnt_destroy(clnt);

#endif/* DEBUG */

} /* test_prog_1 *//* to be continued */


Rpc example 5 7

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

}


Rpc example 6 7

RPC example (6/7)

  • File : test.xx

rpcgen –C test.xx

program TEST_PROG

{

version TEST_VERS

{

int ADDD(int)=1;

int DECC(int)=2;

}=1;

}=0x31234567;


Rpc example 7 7

RPC example (7/7)

  • Use rpcgen , C compiler, and Linking/loader

    • By rpcgentest.xx, you can get test_clnt.c, test_svc.c and test.h.

    • Compile/link on the client

      gcc -o test test_client.ctest_clnt.c-lnsl

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

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

      ./test_svc&

    • Run clien program on the client

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

Demo on Sun machines


Rpcgen c test xx

rpcgen –C test.xx


Test rpc server side

Test RPC -- Server Side


Test rpc client side

Test RPC -- Client Side

輸入兩列:

57

89

把第一個數加 1,

把第二個數減一


Another example of rpcgen file

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;

};

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;


High level library limitation

High-Level Library Limitation

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

  • You must use lower-level RPC library functions if you want to use TCP.

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


Low level rpc library

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


The general rmi architecture

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 client stub (存根).


The stub and skeleton

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.

  • Java RMI is NOT an all-purpose ORB architecture like CORBA


Programming in java

Stub

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


Skeleton

skeleton

  • 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

Note: Before J2SE 5.0 (i.e., JDK 1.4 and older version,

rmicis required to generate the skeleton ???_Skel.class

And ???_Stub.class for client (Where ??? Is your server Class name)


Rmi in java

RMI (in Java)

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

Client

Code

Implement “Factories” using declarative descriptions of activatable objects

Naming

Service

Activation

Daemon

Client

Stub

network

Server

Skeleton

JVM

Asociated

to Activation

Group

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

Server

Object


Java interfaces

Java Interfaces

  • Similar to Class

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

  • Everything is public.

  • No constructor

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

  • In Java a class can only extend a single superclass (single inheritence). But a Java class can implement any number of interfaces.


Serializable interface

Serializable interface

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

  • A class is Serializable if it implements the interface Serializable. A Serializable class is one whose instances can be marshaled (turned into a linear sequence of bits). Serializable objects can be transmitted from one computer to another, as well as be stored into a file.


Java rmi classes

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 , LocateRegistry

  • java.rmi.RMISecurityManager

    • Validates rights to access downloaded object


Rmi adds a few layers to tcp ip

Client App.

Server App.

Stubs

Skeleton

Remote Reference

Remote Reference

Transport

Transport

RMI Adds a few layers to TCP/IP


Remote reference layer

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


Transport layer

Transport layer

  • Provides basic connectivity

    • makes the connection between JVMs

  • Based on TCP/IP connections between machines in a network

  • The network works are done by the client stub and the server Skelton.


Finding remote objects

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


Find servers using naming service

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 Remotelookup(String name)

public static void bind(String name, Remote obj)

public static void rebind(String name, Remote obj)

public static void unbind(String name)


Java rmi naming

java.rmi.Naming


Programming in java

java.rmi.registry.LocateRegistry


Programming in java

java.rmi.registry.Registry


Steps for developing an rmi system

Steps for Developing an RMI System

1. Define the remote interface extends java.rmi.Remote that declares the methods that will be available remotely.

2. Develop the remote server class by implementing the remote interface. The server program must create a remote object and register it with the namingservice.

3. Develop the client program.

4. Compile the Java source files.

5. Use rmic to Generate the client stubs and server skeletons.

6. Start the RMI registry.

7. Start the remote server objects.

8. Run the client


Rmi deployment

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

  • Deployment on client

    • two ways to get the class files to the client

      • Copy class files for all stub classes to the client manually

      • Client dynamically download stub class from Web server


Dynamic loading on client 1 2

Dynamic Loading on Client (1/2)

  • In principle, modifying your RMI application to allow dynamic loading of stub classes is now straightforward:

    • Install the stub classes in a Web Server document directory.

    • Set the java.rmi.server.codebase property for the server application, to reference that Web Server directory. (see below)

    • Create a security policy file on the client.

    • Set the java.security.policy property for the client application.

    • Set a security manager in the client.

  • This also works for any classes (not just stubs) whose serialized form may be communicated via remote method calls.

    • You just need to reinterpret “server” and “client” application according to the direction the serialized object moves—as “source” and “destination” application.

javaw –Djava.rmi.server.codebase=http://www.mit.edu/ggyy/ HelloServer


Dynamic loading on client 2 2

Dynamic Loading on Client (2/2)

  • 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 client

    • Classes downloaded from the client to the server using the java.rmi.server.codebase property setup by the client

  • RMI registry needs to find stub classes

    • If 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”


Setting the security manager

Setting the Security Manager

  • In an RMI application, if no security manager is set, stubs and classes can only be loaded from the local CLASSPATH.

  • To enable dynamic loading, issue the command:

    System.setSecurityManager(new RMISecurityManager()) ;

    at the start of the program.

  • You should do this in any application that may have to download code—in the simple examples considered so far this means RMI clients that need to download stubs.

  • This isn’t the end of the story. You also have to define a new property: the java.security.policy property.

    • In simple cases this java.security.policy property is needed forclients,whereasjava.rmi.server.codebaseis needed for servers.


Defining a security policy

Defining a Security Policy

  • The simplest security policy you can define is a plain text file with contents:

    grant {

    permission java.security.AllPermission "", "" ;

    } ;

  • This policy allows downloaded code to do essentially anything the current user has privileges to do:

    • Read, write and delete arbitrary files; open, read and write to arbitrary Internet sockets; execute arbitrary UNIX/Windows commands on the local machine, etc.

    • It is a dangerous policy if there is any chance you may download code from untrustworthy sources (e.g. the Web).

    • For now you can use this policy, but please avoid dynamically loading code you cannot trust!


The java security policy property

The java.security.policy Property

  • If the text file containing our security policy is called (for example) policy.all, the original HelloClient example might now be run as follows:

    java –Djava.security.policy=policy.all HelloClient

  • Alternatively this property can be set inside the program using System.setProperty().


Rmi scenario 1 2

RMI scenario (1/2)

Downloading Stubs


Rmi scenario 2 2

RMI scenario (2/2)

Downloading Classes


Creating remote object

Creating Remote object

  • Define an interface which extends Remote interface

  • Create the server program which implements that interface

  • Server class should extend UnicastRemoteObject or you can use use UnicastRemoteObject.exportObject(svrObj, 0);

  • Compile the Java program

  • Create the Stub for client using rmic command or other tools

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.


Create the stub

Create the Stub

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

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


Server details extending remote

Server Details – extending Remote

  • Create an interface the extends the java.rmi.Remoteinterface.

    • 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;

      . . .

      }


How the interface will be used

Remote Interface

Class RemoteServer

provides methods

needed by

extends

extends

Your Interface

UnicastRemoteObject

implements

extends

Class for your Remote Object

How the interface will be used


Server details implementation class

Server Details – Implementation Class

  • Create a class that implements the interface.

    • The class should also extend UnicastRemoteObject;or you can create the server object svrObj and then use the static function UnicastRemoteObject.exportObject(svrObj, 0);

  • This class needs a constructor that throws RemoteException !

  • This class is now used by rmic to create the stub and skeleton code.

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.


Generating stubs and skeleton

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.


Server detail main

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

      . . .


Client details

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


Using the naming service

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


Rmi registry 1 2

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

  • Names in the registry use unique names

    • Recommend the name of the remote class that implements the remote interface


Rmi registry 2 2

RMI registry (2/2)

  • To send a message to a remote “server object,”

    • The “client object” has to find the object

      • Do this by looking it up in a registry

    • The client object then has to marshal the parameters (prepare them for transmission)

      • Java requires Serializable parameters

      • The server object has to unmarshal its parameters, do its computation, and marshal its response

    • The client object has to unmarshal the response

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


Start the java rmi registry

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


  • Stateful stateless servers

    Stateful/Stateless Servers

    • Stateful servers:

      • Servers keep track of client information.

      • RPC/RMI reply depends on that client information.

      • Pros: Simplify client design

      • Cons: A server crash loses client information. A client crash leaves old client information at server.

      • At-most-once invocation semantics (Java RMI takes this design.)

    • Stateless servers:

      • Clients must maintain Inter-call information.

      • RPC/RMI reply is always the same.

      • At-least-once invocation semantics (Some RPC implementations take this design.)


    Other technologies

    Other Technologies

    • CORBA

    • XML based

      • SOAP / Web Service

    • J2EE/EJB

    • MicroSoft’s COM, DCOM

    • MicroSoft’s .NET

      • Has Common Language Runtime

      • Tight Integration of Web Services and XML

      • Multiple language integration

        • However only one platform Windows


    Reference

    Reference

    • RMI docs

      • java.sun.com/products/jdk/rmi/

    • Java Tutorial on RMI:

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

    • JavaIDL

      • java.sun.com/docs/books/tutorial/idl

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

      • www.omg.org/news/begin.htm

    • Jini -- http://www.jini.org/

      • www.artima.com/jini/resources


    Programming paradigms

    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.


    Why oo programming

    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 modeling

    • Better 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


    From function to distributed computing

    From function to Distributed computing

    • Function, Subroutine, Procedure

    • DLL (share Library)

    • RPC, RMI

    • CORBA (Common Object Request Broker Architecture)

    • XML, SOAP, Web service

    • ODBC, JDBC

    • Java EE (J2EE), EJB

    • DCOM, .NET (dot NET), C# (請寫出 C# 要如何唸?)

    • parallel computing

    • pervasive computing(普及計算)、ubiquitous computing

    • Grid computing(網格運算)

    • Cloud computing(雲端運算)

    JINI: Jini Is Not Initials

    GNU: GNU Is Not Unix


    Rmi example from sun java 1 4

    RMI example from Sun Java(1/4)

    C:\jtest\RMI>java lineno < Hello.java

    1 //Hello.java

    2 //package example.hello;

    3 import java.rmi.Remote;

    4 import java.rmi.RemoteException;

    5 public interface Hello extends Remote {

    6 String sayHello() throws RemoteException;

    7 }// interface Hello

    C:\jtest\RMI>java lineno < Server.java

    1 //Server.java

    2 //package example.hello;

    3 import java.rmi.registry.Registry;

    4 import java.rmi.registry.LocateRegistry;

    5 import java.rmi.RemoteException;

    6 import java.rmi.server.UnicastRemoteObject;

    7 public class Server implements Hello {

    8 public Server( ) {}

    9 public String sayHello() {

    10 return "Hello, world!";

    11 }


    Rmi example from sun java 2 4

    RMI example from Sun Java(2/4)

    12 public static void main(String args[]) {

    13 try {

    14 Server obj = new Server();

    15 Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

    16

    17 // Bind the remote object's stub in the registry

    18 Registry registry = LocateRegistry.getRegistry();

    19 registry.bind("Hello", stub);

    20

    21 System.err.println("Server ready");

    22 } catch (Exception e) {

    23 System.err.println("Server exception: " + e.toString());

    24 e.printStackTrace();

    25 }

    26 } // main(

    27 }//class Server


    Rmi example from sun java 3 4

    RMI example from Sun Java(3/4)

    C:\jtest\RMI>java lineno < Client.java

    1 //Client.java

    2 //package example.hello;

    3 import java.rmi.registry.LocateRegistry;

    4 import java.rmi.registry.Registry;

    5 public class Client {

    6 private Client() {}

    7 public static void main(String[] args) {

    8 String host = (args.length < 1) ? null : args[0];

    9 try {

    10 Registry registry = LocateRegistry.getRegistry(host);

    11 Hello stub = (Hello) registry.lookup("Hello");

    12 String response = stub.sayHello( );

    13 System.out.println("response: " + response);

    14 } catch (Exception e) {

    15 System.err.println("Client exception: " + e.toString());

    16 e.printStackTrace();

    17 }

    18 }//main(

    19 }// class Client


    Rmi example from sun java 4 4

    RMI example from Sun Java(4/4)

    C:\jtest\RMI>javac *java

    C:\jtest\RMI>start rmiregistry

    C:\jtest\RMI>javaw Server

    C:\jtest\RMI>java Client

    response: Hello, world!

    C:\jtest\RMI>java Client 192.168.1.2

    response: Hello, world!

    C:\jtest\RMI>java Client 192.168.1.1

    Client exception: java.rmi.ConnectException: Connection refused to host: 192.168.1.1; nested exception is:

    java.net.ConnectException: Connection timed out: connect


    Hint to deploy rmi

    Hint to deploy RMI

    • Server requires: Hello.java, Server.java

    • Client requires: Hello.java, Client.java

    • JDK 1.4 and Older version

      • Compile all *.java

      • rmic Server

      • Copy *stub*class to Client side

      • Copy *skel* to Server

      • Server side: start rmiregistry

      • Server side: javae Server

      • Client side: java Client

    • JDK 1.5 and later version ( rmic is no longer needed )

      • Compile all *.java

      • Server side: start rmiregistry

      • Server side: javae Server

      • Client side: java Client


    Web services

    Web services

    • A collection of XML-based technologies developed by the e-business community to address issues of:

      • Service discovery : UDDI + WSDL

      • Interoperable data exchange and/or application invocation : HTTP + XML + SOAP (Simple Object Access Protocol)

      • Service compositions (workflow, business processes)

    • The programming effort and maintainability is roughly the same both for Web Services and CORBA

    • Major developers include:

      • Apache, IBM, HP, SUN & Microsoft (.NET)

    • http://www.webservices.org/


    W3c working group web service definition

    W3C (working group) Web service definition

    • "A Web service is a software application identified by a URI, whose interfaces and bindings are capable of being defined, described and discovered as XML artefacts. A Web service supports direct interactions with other software agents using XML based messages exchanged via internet-based protocols."

    • http://www.w3c.org/TR/2002/WD-wsa-reqs-20020819


    Web services protocol stack

    Web Services Protocol Stack


    Programming in java

    SOAP

    • Simple Object Access Protocol

      • http://www.w3c.org/TR/SOAP/

    • A lightweight protocol for exchange of information in a decentralised, distributed environment

    • Two different styles to use:

      • to encapsulate RPC calls using the extensibility and flexibility of XML

      • …or to deliver a whole document without any method calls encapsulated


    Programming in java

    WSDL

    • Web Services Definition Language

      • http://www.w3.org/TR/wsdl/

    • An XML-based language for describing Web Services

      • what the service does (description)

      • how to use it (method signatures)

      • where to find the service

    • WSDL does not depend on the underlying protocol


    Uddi and alternatives

    UDDI (and alternatives)

    • Universal Description, Discovery and Integration

      • http://www.uddi.org

    • UDDI creates a platform-independent, open framework & registry for:

      • Describing services

      • Discovering businesses

      • Integrating business services

    • The UDDI may be less used than predicted, especially on the Internet level

    • BioMoby - an alternative for Life Sciences domain?


    Programming in java

    Traditional CGI-based approach

    Waiting for requests

    (known location,

    known port)

    Sending requests, getting results

    Data as name/value pairs

    • cgi-bin scripts:

      • Data transmitted as name-value pairs (HTML forms)

      • Transport over (state-less) HTTP protocol

      • no standards for keeping user sessions (state-fullness)

      • server side: a script is called


    Programming in java

    CORBA-based approach

    Waiting for requests

    (known location,

    known port)

    Sending requests, getting results

    Data in binary format

    • CORBA: (Common Object-Request-Broker Architecture)

      • Data transmitted as objects (at least it looks like that)

      • Transport (usually) over well standardised IIOP protocol

      • user sessions (state-fullness) very inter-operable

      • server side: an RPC call is made


    Programming in java

    SOAP-based communication(Web Service)

    Waiting for requests

    (known location,

    known port)

    Sending requests, getting results

    Data in XML format

    • SOAP: (Simple Object Access Protocol)

      • Data in a well-defined XML format

      • Transport over various protocols

        • HTTP, SMTP are the most used, perhaps because they are firewall-friendly

      • server side: either an RPC call or a message delivered


    Rpc rmi

    RPC / RMI

    謝謝捧場

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

    蔡文能


  • Login