풀그림

간단 RMI 구현

파이팅야 2008. 8. 8. 17:37

. 먼저 구현하고자 하는 기능의 Interface를 만든다.

public interface IHello extends Remote{

         public String sayHello(String msg) throws RemoteException;

}

 

. (서버에서 실행해야 하는) Interface를 구현하는 2개의 클래스를 만든다.

public class HelloImpl extends UnicastRemoteObject implements IHello {

         protected HelloImpl() throws RemoteException {

                  super();

         }

         public String sayHello(String msg) {

                  return "Hello RMI :" + msg;

         }

}

public class HelloImpl2 extends UnicastRemoteObject implements IHello {

         protected HelloImpl2() throws RemoteException {

                  super();

         }

         public String sayHello(String msg)

                  throws RemoteException {

                  return "Hello RMI #2 :" + msg;

         }

}

. 서버에서 구동할 클래스를 생성함

public class Server {

public static void main(String[] args) {

         try {

                  HelloImpl hello = new HelloImpl();

                  Naming.rebind("rmi://localhost:1099/Hello", hello);

                 

                  HelloImpl2 hello2 = new HelloImpl2();

                  Naming.rebind("rmi://localhost:1099/Hello2", hello2);

                 

                  System.out.println("레지스트리 등록 후 클라이언트 호출 대기");

         } catch(AccessException accessException) {

                  System.out.println("권한 오류");

         } catch (RemoteException remoteException) {

                  System.out.println("레지스트리에 접근이 안될때");

         } catch (MalformedURLException malformedURLException) {

                  System.out.println("URL형식이 잘못된 경우");

         }

}

}

 

. 호출 해야 하는 클라이언트 생성

public class Client {

public static void main(String[] args) {

         Object obj = null;

         IHello hello = null;

         String returnMsg;

         try {

                  obj = Naming.lookup("rmi://localhost:1099/Hello");

                  hello = (IHello)obj;

                  returnMsg = hello.sayHello("첫번째 호출");

                  System.out.println("원격으로 받은 메세지 #1[" + returnMsg + "]");

                 

                  obj = Naming.lookup("rmi://localhost:1099/Hello2");

                  hello = (IHello)obj;

                  returnMsg = hello.sayHello("두번째 호출");

                  System.out.println("원격으로 받은 메세지 #2[" + returnMsg + "]");

         } catch (MalformedURLException malformedURLException) {

                  malformedURLException.printStackTrace();

         } catch (RemoteException remoteException) {

remoteException.printStackTrace();

         } catch (NotBoundException notBoundException) {

                  notBoundException.printStackTrace();

         }

}

}

. 커멘드 창에서 5개의 파일의 .class파일이 있는 곳에서

         rmic HelloImpl

        rmic HelloImpl2

를 실행하면 HelloImpl_Stub.class파일과 HelloImpl_Stub2.class파일이 생성되는 것을 볼 수 있다.

(java 1.2버젼 이후에는 stub만 생성됨)

 

. 커멘드 창에서 아래의 같이 입력해서 서버와 rmiregister를 시작하고

         start rmiregistry 1099

        start java Server

 

java Client 명령으로 실행해 보면

원격으로 받은 메세지 #1[Hello RMI :첫번째 호출]

원격으로 받은 메세지 #2[Hello RMI #2 :두번째 호출]

와 같이 출력되는 것을 확인 할 수 있다.