public GatewayServerBuilder callbackClient(int pythonPort, InetAddress pythonAddress) { callbackClient = new CallbackClient(pythonPort, pythonAddress); return this; }
public void run() { periodicCleanup(); } }, minConnectionTime, minConnectionTime, minConnectionTimeUnit);
/** * <p> * Sends a command to the Python side. This method is typically used by * Python proxies to call Python methods or to request the garbage * collection of a proxy. * </p> * * @param command * The command to send. * @return The response. */ @Override public String sendCommand(String command) { return sendCommand(command, true); }
InetAddress address = callbackClient.getAddress(); callbackClient.shutdown(); callbackClient = new CallbackClient(port, address);
public String sendCommand(String command, boolean blocking) { String returnCommand = null; Py4JClientConnection cc = getConnectionLock(); if (shouldRetrySendCommand(cc, pe)) { returnCommand = sendCommand(command, blocking); } else { logger.log(Level.SEVERE, "Error while sending a command.", pe); giveBackConnection(cc); } catch (Exception e) { logger.log(Level.SEVERE, "Critical error while giving back connection.", e);
@Override protected boolean shouldRetrySendCommand(Py4JClientConnection cc, Py4JNetworkException pne) { boolean shouldRetry = super.shouldRetrySendCommand(cc, pne); if (shouldRetry && cc instanceof ClientServerConnection) { ClientServerConnection csc = (ClientServerConnection) cc; shouldRetry = csc.isInitiatedFromClient(); } return shouldRetry; }
protected Py4JClientConnection getConnectionLock() { Py4JClientConnection cc = null; try { logger.log(Level.INFO, "Getting CB Connection"); lock.lock(); if (!isShutdown) { cc = getConnection(); logger.log(Level.INFO, "Acquired CB Connection"); } else { logger.log(Level.INFO, "Shutting down, no connection can be created."); } } catch (Exception e) { logger.log(Level.SEVERE, "Critical error while sending a command", e); throw new Py4JException("Error while obtaining a new communication channel", e); } finally { lock.unlock(); } return cc; }
this.readTimeout = readTimeout; this.authToken = StringUtil.escape(authToken); setupCleaner();
/** * Set up the callback client to talk to the server running at the given address and port, * authenticating with the given token. If the token is null, no authentication will be * attempted. */ public GatewayServerBuilder callbackClient(int pythonPort, InetAddress pythonAddress, String authToken) { callbackClient = new CallbackClient(pythonPort, pythonAddress, authToken); return this; }
/** * <p> * Creates a callback client which connects to the given address and port, * but retains all the other settings (like the {@link #minConnectionTime} * and the {@link #socketFactory}. This method is useful if for some reason * your CallbackServer changes its address or you come to know of the * address after Gateway has already instantiated. * </p> * * @param pythonAddress * The address used by a PythonProxyHandler to connect to a * Python gateway. * @param pythonPort * The port used by a PythonProxyHandler to connect to a Python * gateway. Essentially the port used for Python callbacks. */ @Override public Py4JPythonClient copyWith(InetAddress pythonAddress, int pythonPort) { return new CallbackClient(pythonPort, pythonAddress, authToken, minConnectionTime, minConnectionTimeUnit, socketFactory, enableMemoryManagement, readTimeout); }
int connectTimeout, int readTimeout, List<Class<? extends Command>> customCommands) { this(entryPoint, port, address, connectTimeout, readTimeout, customCommands, new CallbackClient(pythonPort, pythonAddress), ServerSocketFactory.getDefault());
/** * <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); } }
/** * * @param entryPoint * The entry point of this Gateway. Can be null. * @param port * The port the GatewayServer is listening to. * @param pythonPort * The port used by a PythonProxyHandler to connect to a Python * gateway. Essentially the port used for Python callbacks. * @param connectTimeout * Time in milliseconds (0 = infinite). If a GatewayServer does * not receive a connection request after this time, it closes * the server socket and no other connection is accepted. * @param readTimeout * Time in milliseconds (0 = infinite). Once a Python program is * connected, if a GatewayServer does not receive a request * (e.g., a method call) after this time, the connection with the * Python program is closed. * @param customCommands * A list of custom Command classes to augment the Server * features. These commands will be accessible from Python * programs. Can be null. */ public GatewayServer(Object entryPoint, int port, int pythonPort, int connectTimeout, int readTimeout, List<Class<? extends Command>> customCommands) { this(entryPoint, port, defaultAddress(), connectTimeout, readTimeout, customCommands, new CallbackClient(pythonPort, defaultAddress()), ServerSocketFactory.getDefault()); }