반응형
Java RMI (Remote Method Invocation) (3)
1
2
3
4
5
6
7
8
|
// SampleI.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SampleI extends Remote {
int Add(int num1, int num2) throws RemoteException;
String Echo(String Msg) throws RemoteException;
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements SampleI {
public Server() {}
public String Echo(String Msg) {
return Msg;
}
public int Add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
public static void main(String args[]) {
try {
// remote invocation export
Server obj = new Server();
SampleI stub = (SampleI) UnicastRemoteObject.exportObject(obj, 0);
// RMI registry에 원격 객체(인터페이스가 상속)의 stub을 honbob와 bind 한다.
Registry registry = LocateRegistry.getRegistry();
registry.bind("honbob", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
SampleI stub = (SampleI) registry.lookup("honbob");
// server의 함수를 호출한다.
String ResultEcho = stub.Echo("Hi Hon Bob");
int ResultAdd = stub.Add(125, 175);
System.out.println("ResultEcho : " + ResultEcho);
System.out.println("ResultAdd : " + ResultAdd);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
|
cs |
출처 : 0yumin.tistory.com/17?category=606010
반응형
'공부 > Object-Oriented Design Pattern' 카테고리의 다른 글
Java Socket 메시지 주고받기 (0) | 2021.04.03 |
---|---|
Java Socket Test Example (1) 자바 소켓 예시 (0) | 2021.04.03 |
Java RMI (Remote Method Invocation) (2) (0) | 2021.04.03 |
Java RMI (Remote Method Invocation)을 알아보자 (0) | 2021.04.03 |
[OODP] Singleton Pattern (싱글톤 패턴) (0) | 2021.04.01 |
댓글