Anda di halaman 1dari 3

Tahapan membuat program RMI:

1. Buat Interface, misal HelloInterface.java:


/*

(1) Tahap Pertama adalah membuat Interface

*/
import java.rmi.*;
/**
* Remote Interface for the "Hello, world!" example.
*/
public interface HelloInterface extends Remote {
/**
* Remotely invocable method.
* @return the message of the remote object, such as "Hello,
world!".
* @exception RemoteException if the remote invocation fails.
*/
public String say() throws RemoteException;
public String teriak() throws RemoteException;
}

2. Buat Implementasi, misal Hello.java:


/*

(2) Tahap Kedua adalah membuat Remote Object dan Interfacenya

*/
import java.rmi.*;
import java.rmi.server.*;
/**
* Remote Class for the "Hello, world!" example.
*/
public class Hello extends UnicastRemoteObject implements
HelloInterface {
private String message;
/**
* Construct a remote object
* @param msg the message of the remote object, such as "Hello,
world!".
* @exception RemoteException if the object handle cannot be
constructed.
*/
public Hello (String msg) throws RemoteException {
message = msg;
}
/**
* Implementation of the remotely invocable method.
* @return the message of the remote object, such as "Hello,
world!".
* @exception RemoteException if the remote invocation fails.
*/
public String say() throws RemoteException {

//System.out.println("method ini ada yang memanggil dari client");


return message;
}
public String teriak() throws RemoteException {
System.out.println("yang dipanggil client adalah method:
teriak()");
return message;
}
}

3. Membuat aplikasi Server, misal Server.java:


/*

(3) Tahap Ketiga adalah membuat aplikasi Server

*/
/**
* Server program for the "Hello, world!" example.
* @param argv The command line arguments which are ignored.
*/
import java.rmi.Naming;
public class Server {
public static void main (String[] argv) {
try {
Naming.rebind ("Hello", new Hello ("Hello, world!"));
System.out.println ("Hello Server is ready.");
} catch (Exception e) {
System.out.println ("Hello Server failed: " + e);
}
}
}

4. Membuat aplikasi Client, misal Client.java:


/*

(4) Tahap Keempat membuat aplikasi Client

*/
/**
* Client program for the "Hello, world!" example.
* @param argv The command line arguments which are ignored.
*/
import java.rmi.Naming;
public class Client {
public static void main (String[] argv) {
try {
HelloInterface hello =
(HelloInterface) Naming.lookup ("//localhost/Hello");

System.out.println (hello.say());
System.out.println (hello.teriak());
} catch (Exception e) {
System.out.println ("HelloClient exception: " + e);
}
}

5. Kompilasi Program RMI:


a. Kompilasi program Aplikasi Server
Kompilasi Server.java akan menghasilkan file-file .class yaitu Hello.class,
HelloInterface.class, dan Server.class
b. Kompilasi RMI Registry dari command promt (DOS prompt)
Kompilasi RMI dilakukan untuk file Implementasi yaitu file Hello.class.
Melalui command prompt ketikan perintah:
rmic Hello kemudian tekan Enter
c. Kompilasi program Aplikasi Client
6. Menjalankan Program RMI:
a. Menyiapkan jendela command prompt
Menjalankan Program RMI diperlukan 3 (tiga) jendela command prompt (jika
Server dan Client dijalankan pada komputer yang sama).
b. Menjalankan RMI Registry
Jendela command prompt pertama untuk menjalankan rmi registry, dengan
perintah:
rmiregistry kemudian tekan Enter
c. Menjalankan Server
Jendela command prompt kedua untuk menjalankan Server, dengan perintah:
java Server kemudian tekan Enter
d. Menjalankan Client
Jendela command prompt ketiga untuk menjalankan Client, dengan perintah:
java Client kemudian tekan Enter

Anda mungkin juga menyukai