in paimon-python-java-bridge/src/main/java/org/apache/paimon/python/PythonGatewayServer.java [44:102]
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, ClassNotFoundException {
GatewayServer gatewayServer = PythonEnvUtils.startGatewayServer();
PythonEnvUtils.setGatewayServer(gatewayServer);
int boundPort = gatewayServer.getListeningPort();
Py4JPythonClient callbackClient = gatewayServer.getCallbackClient();
int callbackPort = callbackClient.getPort();
if (boundPort == -1) {
System.out.println("GatewayServer failed to bind; exiting");
System.exit(1);
}
// Tells python side the port of our java rpc server
String handshakeFilePath = System.getenv("_PYPAIMON_CONN_INFO_PATH");
File handshakeFile = new File(handshakeFilePath);
File tmpPath =
Files.createTempFile(handshakeFile.getParentFile().toPath(), "connection", ".info")
.toFile();
FileOutputStream fileOutputStream = new FileOutputStream(tmpPath);
DataOutputStream stream = new DataOutputStream(fileOutputStream);
stream.writeInt(boundPort);
stream.writeInt(callbackPort);
stream.close();
fileOutputStream.close();
if (!tmpPath.renameTo(handshakeFile)) {
System.out.println(
"Unable to write connection information to handshake file: "
+ handshakeFilePath
+ ", now exit...");
System.exit(1);
}
try {
// This ensures that the server dies if its parent program dies.
Map<String, Object> entryPoint =
(Map<String, Object>) gatewayServer.getGateway().getEntryPoint();
for (int i = 0; i < TIMEOUT_MILLIS / CHECK_INTERVAL; i++) {
if (entryPoint.containsKey("Watchdog")) {
break;
}
Thread.sleep(CHECK_INTERVAL);
}
if (!entryPoint.containsKey("Watchdog")) {
System.out.println("Unable to get the Python watchdog object, now exit.");
System.exit(1);
}
Watchdog watchdog = (Watchdog) entryPoint.get("Watchdog");
while (watchdog.ping()) {
Thread.sleep(CHECK_INTERVAL);
}
gatewayServer.shutdown();
System.exit(0);
} finally {
System.exit(1);
}
}