private void initClientServer() { this.clientServer = new ClientServer(port, GatewayServer.defaultAddress(), pythonPort, GatewayServer.defaultAddress(), GatewayServer.DEFAULT_CONNECT_TIMEOUT, GatewayServer.DEFAULT_READ_TIMEOUT, ServerSocketFactory.getDefault(), SocketFactory.getDefault(), null); }
/** * @param args */ public static void main(String[] args) { GatewayServer server = new GatewayServer(new Object()); server.start(); }
/** * Starts a Py4j gateway server. * * @param portFile the file to have the gateway server listening port (in string format) written to * @return the gateway server * @throws IOException if failed to start the server or failed to write out the port. */ public static GatewayServer startPy4jGateway(Path portFile) throws IOException { GatewayServer server = new GatewayServer(null, 0); try { server.start(); } catch (Py4JNetworkException e) { throw new IOException(e); } // Write the port number in string form to the port file Files.write(portFile, Integer.toString(server.getListeningPort()).getBytes(StandardCharsets.UTF_8)); return server; } }
gatewayServer.start(); int listening_port = gatewayServer.getListeningPort(); System.out.println("" + listening_port);
/** * <p> * Starts to accept connections in a second thread (non-blocking call). * </p> */ public void start() { start(true); }
server.start(); } else { logger.info("Using the manually created Py4J Server. Note that the server should have already been started."); setFacade((T) server.getPythonServerEntryPoint(new Class[] { ClassUtils.getClass(getFacadeInterfaceName()) })); } catch (Exception e) { throw SpongeUtils.wrapException(e);
/** * <p> * Builds a GatewayServer instance using the provided parameters. If gateway is provided, * some parameters will be ignored (callbackClient and entryPoint). * </p> * * @return */ public GatewayServer build() { if (gateway == null) { if (callbackClient == null) { callbackClient = new CallbackClient(GatewayServer.DEFAULT_PYTHON_PORT); } return new GatewayServer(entryPoint, javaPort, javaAddress, connectTimeout, readTimeout, customCommands, callbackClient, serverSocketFactory, authToken); } else { return new GatewayServer(gateway, javaPort, javaAddress, connectTimeout, readTimeout, customCommands, serverSocketFactory, authToken); } }
/** * Updates the python callback port in the {@link GatewayServer}. */ public static void setGatewayCallbackPort(GatewayServer gatewayServer, int port) { gatewayServer.resetCallbackClient(gatewayServer.getCallbackClient().getAddress(), port); }
/** * <p> * Gets a reference to the entry point on the Python side. This is often * necessary if Java is driving the communication because Java cannot call * static methods, initialize Python objects or load Python modules yet. * </p> * * @param interfacesToImplement * @return */ public Object getPythonServerEntryPoint(@SuppressWarnings("rawtypes") Class[] interfacesToImplement) { return getCallbackClient().getPythonServerEntryPoint(gateway, interfacesToImplement); }
/** * <p> * Stops accepting connections, closes all current connections, and calls * {@link py4j.Gateway#shutdown() Gateway.shutdown()} * </p> */ public void shutdown() { this.shutdown(true); }
//AdditionApplication.java import py4j.GatewayServer; public class AdditionApplication { public static void main(String[] args) { AdditionApplication app = new AdditionApplication(); // app is now the gateway.entry_point GatewayServer server = new GatewayServer(app); server.start(); } }
/** * Starts a Py4j gateway server. * * @param portFile the file to have the gateway server listening port (in string format) written to * @return the gateway server * @throws IOException if failed to start the server or failed to write out the port. */ public static GatewayServer startPy4jGateway(Path portFile) throws IOException { GatewayServer server = new GatewayServer(null, 0); try { server.start(); } catch (Py4JNetworkException e) { throw new IOException(e); } // Write the port number in string form to the port file Files.write(portFile, Integer.toString(server.getListeningPort()).getBytes(StandardCharsets.UTF_8)); return server; } }
/** * Updates the python callback port in the {@link GatewayServer}. */ public static void setGatewayCallbackPort(GatewayServer gatewayServer, int port) { gatewayServer.resetCallbackClient(gatewayServer.getCallbackClient().getAddress(), port); }
CallbackClient callbackClient = gatewayServer.getCallbackClient(); InetAddress address = callbackClient.getAddress(); callbackClient.shutdown();
public void run() { System.out.println(); System.out.println("Shutting down py-powsybl server"); server.shutdown(); } });
import py4j.GatewayServer; public class myTest { public static void main(String[] args) { myTest app = new myTest(); // app is now the gateway.entry_point GatewayServer server = new GatewayServer(app); server.start(); } }
/** * Starts a Py4j gateway server. * * @param portFile the file to have the gateway server listening port (in string format) written to * @return the gateway server * @throws IOException if failed to start the server or failed to write out the port. */ public static GatewayServer startPy4jGateway(Path portFile) throws IOException { GatewayServer server = new GatewayServer(null, 0); try { server.start(); } catch (Py4JNetworkException e) { throw new IOException(e); } // Write the port number in string form to the port file Files.write(portFile, Integer.toString(server.getListeningPort()).getBytes(StandardCharsets.UTF_8)); return server; } }
public CallbackClient(int port) { this(port, GatewayServer.defaultAddress(), DEFAULT_MIN_CONNECTION_TIME, DEFAULT_MIN_CONNECTION_TIME_UNIT, SocketFactory.getDefault(), true); }
@Override public void onShutdown() { // Reversed order of killing the script process. if (!isPythonScriptBeforeStartup()) { killPythonScript(); } if (server != null) { server.shutdown(); } if (isPythonScriptBeforeStartup()) { killPythonScript(); } }
@Override public void run(CommandLine line, ToolRunningContext context) throws Exception { GatewayServer server = new GatewayServer(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { System.out.println(); System.out.println("Shutting down py-powsybl server"); server.shutdown(); } }); // execute the py4j Gateway service //TODO customize service parameters: address, port, ..... server.start(); System.out.println("py-powsybl server started; CTRL+C to stop it"); } }