From e9c46016441c6a860bc75d4df99d03477cdd1a50 Mon Sep 17 00:00:00 2001 From: gnanaprakash-ravi Date: Thu, 21 Sep 2023 12:41:33 +0530 Subject: [PATCH] try to access jvm --- python/zingg/ExampleApplication.java | 81 ++++++++++++++++++++++++++++ python/zingg/test2.py | 50 +++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 python/zingg/ExampleApplication.java create mode 100644 python/zingg/test2.py diff --git a/python/zingg/ExampleApplication.java b/python/zingg/ExampleApplication.java new file mode 100644 index 000000000..41e602b1c --- /dev/null +++ b/python/zingg/ExampleApplication.java @@ -0,0 +1,81 @@ +package py4j.examples; + +import javax.net.SocketFactory; + +import py4j.CallbackClient; +import py4j.GatewayServer; + +public class ExampleApplication { + + /** + * @param args + */ + public static void main(String[] args) { + GatewayServer.turnLoggingOff(); + GatewayServer server = new GatewayServer(new ExampleEntryPoint()); + server.start(); + } + + public static class ExampleNoMemManagementApplication { + public static void main(String[] args) { + GatewayServer.turnLoggingOff(); + CallbackClient callbackClient = new CallbackClient(GatewayServer.DEFAULT_PYTHON_PORT, + GatewayServer.defaultAddress(), CallbackClient.DEFAULT_MIN_CONNECTION_TIME, + CallbackClient.DEFAULT_MIN_CONNECTION_TIME_UNIT, SocketFactory.getDefault(), false); + GatewayServer server = new GatewayServer(new ExampleEntryPoint(), GatewayServer.DEFAULT_PORT, + GatewayServer.defaultAddress(), GatewayServer.DEFAULT_CONNECT_TIMEOUT, + GatewayServer.DEFAULT_READ_TIMEOUT, null, callbackClient); + server.start(); + } + } + + public static class ExamplePythonEntryPointApplication { + + public static void main(String[] args) { + String authToken = null; + if (args.length > 0) { + authToken = args[0]; + } + GatewayServer.turnLoggingOff(); + GatewayServer server = new GatewayServer.GatewayServerBuilder() + .callbackClient(GatewayServer.DEFAULT_PYTHON_PORT, GatewayServer.defaultAddress(), authToken) + .build(); + server.start(); + IHello hello = (IHello) server.getPythonServerEntryPoint(new Class[] { IHello.class }); + try { + hello.sayHello(); + hello.sayHello(2, "Hello World"); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public static class ExampleShortTimeoutApplication { + public static void main(String[] args) { + GatewayServer.turnLoggingOff(); + CallbackClient callbackClient = new CallbackClient(GatewayServer.DEFAULT_PYTHON_PORT, + GatewayServer.defaultAddress(), CallbackClient.DEFAULT_MIN_CONNECTION_TIME, + CallbackClient.DEFAULT_MIN_CONNECTION_TIME_UNIT, SocketFactory.getDefault(), false, 250); + GatewayServer server = new GatewayServer.GatewayServerBuilder().readTimeout(250) + .entryPoint(new ExampleEntryPoint()).callbackClient(callbackClient).build(); + server.start(); + + } + } + + public static class ExampleIPv6Application { + public static void main(String[] args) { + GatewayServer.turnLoggingOff(); + CallbackClient callbackClient = new CallbackClient(GatewayServer.DEFAULT_PYTHON_PORT, + GatewayServer.defaultIPv6Address(), CallbackClient.DEFAULT_MIN_CONNECTION_TIME, + CallbackClient.DEFAULT_MIN_CONNECTION_TIME_UNIT, SocketFactory.getDefault(), false, 250); + GatewayServer server = new GatewayServer.GatewayServerBuilder().readTimeout(250) + .entryPoint(new ExampleEntryPoint()).callbackClient(callbackClient) + .javaAddress(GatewayServer.defaultIPv6Address()).build(); + server.start(); + + } + } + +} \ No newline at end of file diff --git a/python/zingg/test2.py b/python/zingg/test2.py new file mode 100644 index 000000000..0b9163c52 --- /dev/null +++ b/python/zingg/test2.py @@ -0,0 +1,50 @@ +import unittest +import sys + +from zingg import * +from zingg.client import * +from zingg.pipes import * + +import subprocess +from py4j.java_gateway import JavaGateway +from py4j.protocol import Py4JNetworkError +from time import sleep + +# PY4J_JAVA_PATH = '~/.local/share/py4j/py4j0.10.9.7.jar' +PY4J_JAVA_PATH = '.' + +def start_example_server(): + subprocess.call([ + "java", "-Xmx512m", "-cp", PY4J_JAVA_PATH, + "ExampleApplication"]) + +def check_connection(gateway): + try: + gateway.jvm.System.currentTimeMillis() + except Py4JNetworkError: + sleep(2) + +class MyJavaClass: + def addition(self, a, b): + return a + b + +class MyJavaIntegrationTest(unittest.TestCase): + def setUp(self): + start_example_server() + self.gateway = JavaGateway() + check_connection(self.gateway) + + def tearDown(self): + self.gateway.close() + + def test_jvm_access(self): + print("Accessing the JVM...") + try: + current_time = self.gateway.jvm.System.currentTimeMillis() + print("Current time from JVM:", current_time) + except Py4JNetworkError: + print("Failed to access the JVM.") + + +if __name__ == '__main__': + unittest.main(argv=sys.argv[:1]) \ No newline at end of file