Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to access jvm #672

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions python/zingg/ExampleApplication.java
Original file line number Diff line number Diff line change
@@ -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();

}
}

}
50 changes: 50 additions & 0 deletions python/zingg/test2.py
Original file line number Diff line number Diff line change
@@ -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])