Repository navigation
Expand file tree
/
Copy pathDemoServer.java
More file actions
68 lines (61 loc) · 2.54 KB
/
Copy pathDemoServer.java
File metadata and controls
68 lines (61 loc) · 2.54 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package demo;
import com.apihub.*;
public class DemoServer {
public static void main(String[] args) {
System.out.println("=== Java API Hub Server Demo ===");
// 注册 Shutdown Hook 确保退出时清理(备用)
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("Shutdown hook triggered.");
ApiHub.exitMainThread();
ApiHub.shutdown();
}));
// 1. 创建应用
try (AppHandle app = new AppHandle("CalcService", "Calculator")) {
// 2. 注册 add API
CallCallback addCallback = (trigger, input, output) -> {
DataHandle in = DataHandle.wrapInput(input);
DataHandle out = DataHandle.wrapOutput(output);
try {
int a = in.readInt();
int b = in.readInt();
int sum = a + b;
out.writeInt(sum);
System.out.printf("[Server] add(%d, %d) = %d%n", a, b, sum);
} catch (Exception e) {
System.err.println("Add callback error: " + e.getMessage());
}
};
if (!app.registerCall("add", "a+b", addCallback)) {
System.err.println("Register 'add' failed");
return;
}
System.out.println("Registered 'add' API");
// 3. 网络准备
ApiHub.resetPrepare();
ApiHub.prepareService("ipc:calc_service", "ipc:calc_service");
ApiHub.prepareClient("ipc:calc_service", app);
if (!ApiHub.prepareDone()) {
System.err.println("Prepare failed. Please check console output for errors.");
return;
}
System.out.println("Service started on ipc:calc_service");
System.out.println("Press Ctrl+C to stop...");
// 4. 等待退出(可中断循环)
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted, shutting down...");
Thread.currentThread().interrupt(); // 保留中断状态
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 确保清理(Shutdown Hook 也会做,但这里双重保障)
ApiHub.exitMainThread();
ApiHub.shutdown();
}
}
}