Remote Method Invocation

RMI의 장점

아키텍처 구조

코드예제

서버

// 1. java.rmi.Remote를 상속받는 User Interface 선언
// 2. 해당 User Interface 내에 메서드 정의. throws RemoteException 필수
interface HelloInterface extends Remote {
	public String sayHello(String name) throws RemoteExcpetion
}

// 3. 해당 User Interface를 구현하는 Remote Object를 정의
public class HelloImpl extends UnicastRemoteObject implements ?HelloInterface {
	public HelloImpl() throws RemoteException {
		super();
	}
	@Override
	public String sayHello(String name) throws RemoteException {
		return "Hello,"+name;
	}	
}

// 4. Remote Object를 RMI Registry에 Binding
public App {
	public static void main(String[] args) {
		try {
			Naming.bind("rmi://localhost/MyRemote", new HelloImpl());
			System.out.println("Server is registerd");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

클라이언트

// 1. RMI Registry LookUp 및 Remote Object의 레퍼런스 획득
HelloInterface hello = (HelloInterface) Naming.lookup("rmi://주소");

// 2. Remote Object의 메서드 수행
String response = hello.sayHello("Jin");
System.out.println(response);

https://jisblee.me/board/view/0/2/164