Remote Method Invocation rmi Ir Risanuri Hidayat M

  • Slides: 41
Download presentation
Remote Method Invocation (rmi) Ir. Risanuri Hidayat, M. Sc.

Remote Method Invocation (rmi) Ir. Risanuri Hidayat, M. Sc.

Apa itu RMI • Remote Procedure Calls (RPCs) – Memanggil fungsi yang berada di

Apa itu RMI • Remote Procedure Calls (RPCs) – Memanggil fungsi yang berada di komputer lain – Komunikasi di atas jaringan – Marshal data – Tipe-tipe data terbatas – Cenderung memakai interface definition language (IDL) • Remote Method Invocation (RMI) – Implementasi RPC dengan Java – Meng-handle marshaling data across the network – Transfer Java objects – No IDL is required

Apa itu RMI • Java Remote Method Invocation (RMI) system memungkinkan object yang running

Apa itu RMI • Java Remote Method Invocation (RMI) system memungkinkan object yang running di satu JVM untuk memanggil suatu metode dar satu object yang running di JVM yang lain. • RMI memungkinkan komunikasi remote antar program JAVA • Note: Jika program tersambung ke suatu program IDL, sebaiknya kita pakai Java IDL dari pada memakai RMI. (baca sendiri IDL)

Apa itu RMI • Ilustrasi RMI

Apa itu RMI • Ilustrasi RMI

Pendahuluan • Aplikasi RMI sering terbagi menjadi dua bagian: server dan client. • Server

Pendahuluan • Aplikasi RMI sering terbagi menjadi dua bagian: server dan client. • Server mempunyai beberapa remote objects, dan referencenya, serta menunggu jika client ingin memanggil remote object tersebut • Client mendapatkan remote refernce untuk satu atau lebih remote object di dalam server, dan kemudian memanggil metode di dalamnya. • RMI menyediakan mekanisme sehingga server dan client dapat berkomunikasi dan tukar menukar informasi timbal balik. Aplikasi seperti ini disebut dengan distributed object application.

Keuntungan RMI • Salahsatu keuntungan RMI adalah kemampuan untuk download bytecodes (code) dari suatu

Keuntungan RMI • Salahsatu keuntungan RMI adalah kemampuan untuk download bytecodes (code) dari suatu object's class, jika class tsb tidak terdefinisikan di VM-nya penerima. • Type-type dan metode-metode object (class), yang terletak dalam satu VM, dapat dikirim ke VM yang lain, yang mungkin saja remote. • Sifat-sifat object yang terkirim ini tidak berubah sama sekali

Remote Interfaces, Remote Objects, Remote Methods • Aplikasi terdistribusi dengan Java RMI terdiri atas

Remote Interfaces, Remote Objects, Remote Methods • Aplikasi terdistribusi dengan Java RMI terdiri atas interfaces and classes. • Interfaces mendifinisikan methods. Class meng-implement metode yang didefinisikan di dalam interfaces • Objects (perwujudan dari class) yang mempunyai metode tersebut (yang dapat dipanggil dari jauh) disebut dengan remote objects. • Suatu object akan menjadi remote jika meng-implement suatu remote interface, yang mempunyai karakteristik sbb – remote interface meng-extends interface java. rmi. Remote. – Setiap metode interface men-declare java. rmi. Remote. Exception di dalam throws clause.

Remote Interfaces, Remote Objects, Remote Methods • RMI memperlakukan remote object berbeda dengan yang

Remote Interfaces, Remote Objects, Remote Methods • RMI memperlakukan remote object berbeda dengan yang non-remote object, ketika object tersebut dikirim ke VM yang lain. • Selain membuat copy object ke VM penerima, RMI mengirim satu remote stub untuk satu remote object. – stub ini beraksi sebagai perwakilan lokal, proxy, untuk remote object tsb, dan untuk pemanggilnya, remote reference. – Remote reference memanggil metode pada stub lokal, yang bertanggung jawab untuk memanggil ke remote object. • Stub (untuk remote object) meng-implements remote interfaces yang remote object juga meng-implements.

Arsitektur RMI • The server must first bind its name to the registry •

Arsitektur RMI • 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. lookup Registry bind

Stub dan Skeleton • A client invokes a remote method, the call is first

Stub dan 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 serverside 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.

Membuat Aplikasi Terdistribusi dengan RMI Ada beberapa langkah untuk membuat aplikasi terdistribusi dengan RMI

Membuat Aplikasi Terdistribusi dengan RMI Ada beberapa langkah untuk membuat aplikasi terdistribusi dengan RMI 1. Buat satu (atau lebih) Remote Interface 2. Buat Class di server (remote object) yang meng-implement Remote Interface tersebut 3. Buat program di client yang memanggil remote object tersebut 4. Kompilasi source dan generate stub dan skeleton 5. Start RMI Registry 6. Start (Run) Server 7. Start (Run) Client

Design Remote Interface • To create an RMI application, the first step is the

Design Remote Interface • To create an RMI application, the first step is the defining of a remote interface between the client and server objects. • remote interfaces: menspesifikasikan metode yang dapat dipanggil oleh client. /* Sample. Server. java */ import java. rmi. *; public interface Sample. Server extends Remote { public int sum(int a, int b) throws Remote. Exception; }

Remote Object • • • Remote objects harus meng-implement satu atau lebih remote interfaces.

Remote Object • • • Remote objects harus meng-implement satu atau lebih remote interfaces. Remote object class bisa saja meng-implement interface yang lain (baik lokal ataupun remote) dan metode yang lain (yang hanya lokal) The server is a simple unicast remote server. Create server by extending java. rmi. server. Unicast. Remote. Object. • The server uses the RMISecurity. Manager to protect its resources while engaging in remote communication. • The server must bind its name to the registry, the client will look up the server name. Use java. rmi. Naming class to bind the server name to registry. In this example the name call “SAMPLE-SERVER”. • • In the main method of your server object, the RMI security manager is created and installed.

Remote Object /* Sample. Server. Impl. java */ import java. rmi. *; import java.

Remote Object /* Sample. Server. Impl. java */ import java. rmi. *; import java. rmi. server. *; import java. rmi. registry. *; public class Sample. Server. Impl extends Unicast. Remote. Object implements Sample. Server { Sample. Server. Impl() throws Remote. Exception { super(); } public int sum(int a, int b) throws Remote. Exception { return a + b; }

Remote Object public static void main(String args[]) { try { System. set. Security. Manager(new

Remote Object public static void main(String args[]) { try { System. set. Security. Manager(new RMISecurity. Manager()); //set the security manager //create a local instance of the object Sample. Server. Impl Server = new Sample. Server. Impl(); //put the local instance in the registry Naming. rebind("SAMPLE-SERVER" , Server); System. out. println("Server waiting. . . "); } catch (java. net. Malformed. URLException me) { System. out. println("Malformed URL: " + me. to. String()); } catch (Remote. Exception re) { System. out. println("Remote exception: " + re. to. String()); } } }

Program Client • In order for the client object to invoke methods on the

Program Client • In order for the client object to invoke methods on the server, it must first look up the name of server in the registry. You use the java. rmi. Naming class to lookup the server name. • The server name is specified as URL in the from ( rmi: //host: port/name ) • Default RMI port is 1099. • The name specified in the URL must exactly match the name that the server has bound to the registry. In this example, the name is “SAMPLE -SERVER” • The remote method invocation is programmed using the remote interface name (remote. Object) as prefix and the remote method name (sum) as suffix.

Program Client import java. rmi. *; import java. rmi. server. *; public class Sample.

Program Client import java. rmi. *; import java. rmi. server. *; public class Sample. Client { public static void main(String[] args) { // set the security manager for the client System. set. Security. Manager(new RMISecurity. Manager()); //get the remote object from the registry { System. out. println("Security Manager loaded"); String url = "//localhost/SAMPLE-SERVER"; Sample. Server remote. Object = (Sample. Server)Naming. lookup(url) ; System. out. println("Got remote object"); System. out. println(" 1 + 2 = " + remote. Object. sum(1, 2) ); } catch (Remote. Exception exc) { System. out. println("Error in lookup: " + exc. to. String()); } catch (java. net. Malformed. URLException exc) { System. out. println("Malformed URL: " + exc. to. String()); } catch (java. rmi. Not. Bound. Exception exc) { System. out. println("Not. Bound: " + exc. to. String()); } } }

Compile sources dan buat (generate) stubs • Pertama, gunakan javac compiler untuk mengkompile source

Compile sources dan buat (generate) stubs • Pertama, gunakan javac compiler untuk mengkompile source files, yang di sana terdapat implementasi remote interfaces, server class, dan client classes. • Kedua, gunakan rmic compiler untuk membuat stubs untuk remote objects. RMI menggunakan stub remote object sebagai proxy pada clients, sehingga client dapat berkomunikasi dengan remote object tertentu.

Compile sources dan buat (generate) stubs • Assume the program compile and executing at

Compile sources dan buat (generate) stubs • Assume the program compile and executing at elpis on ~/rmi • Once the interface is completed, you need to generate stubs and skeleton code. The RMI system provides an RMI compiler (rmic) that takes your generated interface class and procedures stub code on its self. elpis: ~/rmi> set CLASSPATH=”~/rmi” javac Sample. Server. Impl. java rmic Sample. Server. Impl elpis: ~/rmi> javac Sample. Client. java

Start RMI Registry • The RMI applications need install to Registry. And the Registry

Start RMI Registry • The RMI applications need install to Registry. And the Registry must start manual by call rmiregisty. • The rmiregistry us uses port 1099 by default. You can also bind rmiregistry to a different port by indicating the new port number as : rmiregistry <new port> elpis: ~/rmi> rmiregistry • Remark: On Windows, you have to type in from the command line: > start rmiregistry

Start Server dan Client • Once the Registry is started, the server can be

Start Server dan Client • Once the Registry is started, the server can be started and will be able to store itself in the Registry. • Because of the grained security model in Java 2. 0, you must setup a security policy for RMI by set java. security. policy to the file policy. all elpis: ~/rmi> java –Djava. security. policy=policy. all Sample. Server. Impl elpis: ~/rmi> java –Djava. security. policy=policy. all Sample. Client

File Policy • In Java 2, the java application must first obtain information regarding

File Policy • In Java 2, the java application must first obtain information regarding its privileges. It can obtain the security policy through a policy file. In above example, we allow Java code to have all permissions, the contains of the policy file policy. all is: grant { permission java. security. All. Permission; }; • Now, we given an example for assigning resource permissions: grant { permission java. io. file. Permission “/tmp/*”, “read”, “write”; permission java. net. Socket. Permission “somehost. somedomain. com: 999”, ”connect”; permission java. net. Socket. Permission “*: 102465535”, ”connect, request”; permission java. net. Socket. Permission “*: 80”, ”connect”; };

File Policy 1. allow the Java code to read/write any files only under the

File Policy 1. allow the Java code to read/write any files only under the /tmp directory, includes any subdirectories 2. allow all java classes to establish a network connection with the host “somehost. somedomain. com” on port 999 3. allows classes to connection to or accept connections on unprivileged ports greater than 1024 , on any host 4. allows all classes to connect to the HTTP port 80 on any host. • You can obtain complete details by following links: http: //java. sun. com/products//jdk/1. 2/docs/guide/security/spec /security-spec. doc 3. html

RMI Tutorial Dengan JBUILDER Risanuri Hidayat, Ir. , M. Sc.

RMI Tutorial Dengan JBUILDER Risanuri Hidayat, Ir. , M. Sc.

Pendahuluan ● ● Tutorial ini memberikan petunjuk membuat aplikasi RMI sederhana dengan JBUILDER 5.

Pendahuluan ● ● Tutorial ini memberikan petunjuk membuat aplikasi RMI sederhana dengan JBUILDER 5. 0 Client mempunyai metode yang dikerjakan Server menerima panggilan, dan mengerjakan metode tersebut, hasilnya dikirim kembali ke client Aplikasi sederhana ini, client meminta server menampilkan tulisan “Hallo Sayang” ke client

File ● Ada tiga file dalam aplikasi ini – – – ● RMI 02_Iface.

File ● Ada tiga file dalam aplikasi ini – – – ● RMI 02_Iface. java - remote interface Server_rmi. java - remote object yang berada di server yang meng-implement Interface RMI 02_Iface Client_rmi. java – client yang memanggil metode remote, say. Hello Ada tambahan 1 file untuk alasan set up security – rmi. policy

Remote Interface package rmi 02; import java. rmi. Remote. Exception; public interface RMI 02_iface

Remote Interface package rmi 02; import java. rmi. Remote. Exception; public interface RMI 02_iface extends Remote { String say. Hello() throws Remote. Exception; }

Server_rmi package rmi 02; public static void main(String args[]) { import java. rmi. Naming;

Server_rmi package rmi 02; public static void main(String args[]) { import java. rmi. Naming; import java. rmi. Remote. Exception; import java. rmi. RMISecurity. Manager; import java. rmi. server. Unicast. Remote. Object; // Create and install a security manager if (System. get. Security. Manager() == null) { System. set. Security. Manager(new RMISecurity. Manager()); } try { Server_rmi obj = new Server_rmi(); // Bind this object instance to the name // "Hello. Server" Naming. rebind("//localhost/Hello. Server", obj); System. out. println("Hello. Server bound in registry"); } catch (Exception e) { System. out. println("Hello. Impl err: " + e. get. Message()); e. print. Stack. Trace(); } } } public class Server_rmi extends Unicast. Remote. Object implements RMI 02_iface { public Server_rmi() throws Remote. Exception { super(); } public String say. Hello() { return "Hallo Sayang. . "; }

Client_rmi package rmi 02; import java. rmi. Naming; import java. rmi. Remote. Exception; public

Client_rmi package rmi 02; import java. rmi. Naming; import java. rmi. Remote. Exception; public class Client_rmi { public Client_rmi() { } public static void main(String[] args) { String message = "ihik. . "; //"obj" is the identifier that we'll use to refer //to the remote object that implements the // "Hello" //interface RMI 02_iface rmi_obj = null; try { rmi_obj = (RMI 02_iface)Naming. lookup("//localhost/ Hello. Server"); message = rmi_obj. say. Hello(); } catch(Exception e) { e. print. Stack. Trace(); } System. out. println(message); } }

Compile source ● ● ● To compile the source files, run the javac command

Compile source ● ● ● To compile the source files, run the javac command as follows: javac -d $HOME/public_html/myclasses Hello. java Hello. Impl. java Hello. Applet. java Use rmic to generate skeletons and/or stubs rmic -d $HOME/public_html/myclasses examples. hello. Hello. Impl The "-d" option indicates the root directory in which to place the compiled stub and skeleton class files. So the preceding command creates the following files in the directory $HOME/public_html/myclasses/examples/hello: – – Hello. Impl_Stub. class Hello. Impl_Skel. class

Compile di JBUILDER ● ● ● Right-click Hello. Impl. java in the project pane.

Compile di JBUILDER ● ● ● Right-click Hello. Impl. java in the project pane. Select Properties from the context menu. Choose the Build page. The Properties dialog box is displayed. Check the Generate RMI Stub/Skeleton field on the RMI/JNI tab of the Build page. Enter -v 1. 2 in the Options field and Click OK. – ● ● (If you do not enter -v 1. 2 in the Options field, you will see deprecation warnings during compilation. ) Click OK to close the dialog box. Right-click Hello. Impl. java and choose Make. The file is compiled with the RMI compiler, rmic.

Compile di JBUILDER

Compile di JBUILDER

● ● ● Memilih Run. Time Configuration di JBUILDER Choose Run|Configurations. The Runtime Configurations

● ● ● Memilih Run. Time Configuration di JBUILDER Choose Run|Configurations. The Runtime Configurations dialog box is displayed. Click New. On the Application page of the Runtime Properties dialog box, enter Hello World Server in the Configuration Name field. Click the ellipsis button next to the Main class field. The Select Main Class For Project dialog box is displayed. Expand the hello package and choose Hello. Impl. Click OK. In the VM parameters field of the Runtime Properties dialog box, enter parameters similar to the following using your own project directory: -Djava. rmi. server. codebase=file: d: myjavarmi 02 Djava. security. policy= file: d: myjavarmi 02. policy

Run. Time Configuration

Run. Time Configuration

Start RMI registry ● ● Click the down arrow next to the Run button

Start RMI registry ● ● Click the down arrow next to the Run button on the JBuilder toolbar. Choose Hello World Server. By default, the registry runs on port 1099. To start the registry on a different port, specify the port number from the command line. For example, to start the registry on port 2001 on a Microsoft Windows NT system: start rmiregistry 2001

Run Server ● java Djava. rmi. server. codebase=http: //myhos t/~myusrname/myclasses/ Djava. security. policy=$HOME/mysrc/poli cy

Run Server ● java Djava. rmi. server. codebase=http: //myhos t/~myusrname/myclasses/ Djava. security. policy=$HOME/mysrc/poli cy examples. hello. Hello. Impl

Run Server ● The output should look like this: Hello. Server bound in registry

Run Server ● The output should look like this: Hello. Server bound in registry

rmi 02. policy grant { // Allow everything for now permission java. net. Socket.

rmi 02. policy grant { // Allow everything for now permission java. net. Socket. Permission "*: 1024 -65535", "accept, connect, listen"; };

Menulis file policy di JBUILDER ● ● ● Right-click the project file Hello. jpx.

Menulis file policy di JBUILDER ● ● ● Right-click the project file Hello. jpx. Choose Add Files/Packages. In the Add Files Or Packages To Project dialog box, make sure the root of the project (jbproject/Hello) is selected. – ● (Click the Project button to quickly move to this directory. ) Enter rmi. policy in the File Name field.

Run Client Hallo Sayang. .

Run Client Hallo Sayang. .

Catatan ● ● Aplikasi RMI di JBUILDER tidak stabil Sekali melakukan kesalahan, harus membuat

Catatan ● ● Aplikasi RMI di JBUILDER tidak stabil Sekali melakukan kesalahan, harus membuat project baru