Subject : JAVA RMI Test 첫번째 예제

Solution Description:
=====================

. 구현코드 생성

www% mkdir $HOME/temp/rmi/src/examples/hello

 - remote interface 정의

www% vi Hello.java

package examples.hello;

public interface Hello extends java.rmi.Remote {
    String sayHello() throws java.rmi.RemoteException;
}


 - remote interface를 구현하는 java remote object(server) 정의

www% vi HelloImpl.java

package examples.hello;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl
	extends UnicastRemoteObject
	implements Hello
{   
    private String name;
 
    public HelloImpl(String s) throws java.rmi.RemoteException {
	super();
	name = s;
    }
    
    public String sayHello() throws RemoteException {
	return  "Hello World!";
    }

    public static void main(String args[]) 
    {
	// Create and install the security manager
	System.setSecurityManager(new RMISecurityManager());

	try {
	    HelloImpl obj = new HelloImpl("HelloServer");
	    Naming.rebind("HelloServer", obj);
	    System.out.println("HelloImpl created and bound in the registry to the name HelloServer");

	} catch (Exception e) {
	    System.out.println("HelloImpl.main: an exception occurred:");
	    e.printStackTrace();
	}
    }
}


 - server의 method를 부르는(invoke) applet 생성

www% vi HelloApplet.java

package examples.hello;

import java.awt.*;
import java.rmi.*;

public class HelloApplet extends java.applet.Applet {

    String message = "";

    public void init() {

    try {
	Hello obj = (Hello)
	    Naming.lookup("//" + getCodeBase().getHost() + "/HelloServer");
	message = obj.sayHello();

	} catch (Exception e) {
	    System.out.println("HelloApplet: an exception occurred:");
	    e.printStackTrace();
	}
    }
   
    public void paint(Graphics g) {
	g.drawString(message, 25, 50);
    }

}


. Compile & Generate Stubs and Skeletons 

www% cd $HOME/temp/rmi/src/examples/hello

www% javac -d $HOME/public_html/codebase Hello.java HelloImpl.java HelloApplet.java

www% cd $HOME/public_html/codebase

www% rmic -d $HOME/public_html/codebase examples.hello.HelloImpl

www% cd ./examples/hello

 - html code 생성

www% vi index.html


Hello World

Hello World

The message from the HelloServer is:

www% ls Hello.class HelloImpl.class index.html HelloApplet.class HelloImpl_Skel.class HelloApplet.html HelloImpl_Stub.class - $HOME/public_html/codebase를 CLASSPATH 설정 . 실행 www% rmiregistry & /* rmi bootstrap registry start, default port 1099 */ www% cd $HOME/public_html/codebase www% java -Djava.rmi.server.codebase=http://www.svc.hei.co.kr/~java/codebase/ examples.hello.HelloImpl & /* Server start, "HelloImpl created and bound in the registry to the name server HelloServer" message 출력, ok */ - 당신의 시스템(client)에서 www% appletviewer http://www.svc.hei.co.kr/~java/codebase/examples/hello/index.html ---------------------------------------------------------------------------- Revision History 작성일자 : 97.07.10 작성자 : 이민호 수정일자 : 수정자 :