public class CallbackClient extends java.lang.Object implements Py4JPythonClient
A CallbackClient is responsible for managing communication channels: channels are created as needed (e.g., one per concurrent thread) and are closed after a certain time.
Modifier and Type | Field and Description |
---|---|
protected java.net.InetAddress |
address |
protected java.lang.String |
authToken |
protected java.util.Deque<Py4JClientConnection> |
connections |
static java.lang.String |
DEFAULT_ADDRESS |
static long |
DEFAULT_MIN_CONNECTION_TIME |
static java.util.concurrent.TimeUnit |
DEFAULT_MIN_CONNECTION_TIME_UNIT |
protected boolean |
enableMemoryManagement |
protected java.util.concurrent.locks.Lock |
lock |
protected long |
minConnectionTime |
protected java.util.concurrent.TimeUnit |
minConnectionTimeUnit |
protected int |
port |
protected int |
readTimeout |
protected javax.net.SocketFactory |
socketFactory |
Constructor and Description |
---|
CallbackClient(int port) |
CallbackClient(int port,
java.net.InetAddress address) |
CallbackClient(int port,
java.net.InetAddress address,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit) |
CallbackClient(int port,
java.net.InetAddress address,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit,
javax.net.SocketFactory socketFactory) |
CallbackClient(int port,
java.net.InetAddress address,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit,
javax.net.SocketFactory socketFactory,
boolean enableMemoryManagement) |
CallbackClient(int port,
java.net.InetAddress address,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit,
javax.net.SocketFactory socketFactory,
boolean enableMemoryManagement,
int readTimeout) |
CallbackClient(int port,
java.net.InetAddress address,
java.lang.String authToken) |
CallbackClient(int port,
java.net.InetAddress address,
java.lang.String authToken,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit,
javax.net.SocketFactory socketFactory,
boolean enableMemoryManagement,
int readTimeout) |
Modifier and Type | Method and Description |
---|---|
Py4JPythonClient |
copyWith(java.net.InetAddress pythonAddress,
int pythonPort)
Creates a callback client which connects to the given address and port,
but retains all the other settings (like the
minConnectionTime
and the socketFactory . |
java.net.InetAddress |
getAddress() |
protected Py4JClientConnection |
getConnection() |
protected Py4JClientConnection |
getConnectionLock() |
int |
getPort() |
java.lang.Object |
getPythonServerEntryPoint(Gateway gateway,
java.lang.Class[] interfacesToImplement)
Gets a reference to the entry point on the Python side.
|
int |
getReadTimeout() |
protected void |
giveBackConnection(Py4JClientConnection cc) |
boolean |
isMemoryManagementEnabled() |
void |
periodicCleanup()
Closes communication channels that have not been used for a time
specified at the creation of the callback client.
|
java.lang.String |
sendCommand(java.lang.String command)
Sends a command to the Python side.
|
java.lang.String |
sendCommand(java.lang.String command,
boolean blocking)
Sends a command to the Python side.
|
protected void |
setupCleaner() |
protected boolean |
shouldRetrySendCommand(Py4JClientConnection cc,
Py4JNetworkException pne) |
void |
shutdown()
Closes all active channels, stops the periodic cleanup of channels and
mark the client as shutting down.
|
public static final java.lang.String DEFAULT_ADDRESS
protected final int port
protected final java.net.InetAddress address
protected final javax.net.SocketFactory socketFactory
protected final java.util.Deque<Py4JClientConnection> connections
protected final java.util.concurrent.locks.Lock lock
public static final long DEFAULT_MIN_CONNECTION_TIME
public static final java.util.concurrent.TimeUnit DEFAULT_MIN_CONNECTION_TIME_UNIT
protected final long minConnectionTime
protected final java.util.concurrent.TimeUnit minConnectionTimeUnit
protected final boolean enableMemoryManagement
protected final int readTimeout
protected final java.lang.String authToken
public CallbackClient(int port)
public CallbackClient(int port, java.net.InetAddress address)
public CallbackClient(int port, java.net.InetAddress address, java.lang.String authToken)
public CallbackClient(int port, java.net.InetAddress address, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit)
public CallbackClient(int port, java.net.InetAddress address, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit, javax.net.SocketFactory socketFactory)
port
- The port used by channels to connect to the Python side.address
- The addressed used by channels to connect to the Python side..minConnectionTime
- The minimum connection time: channels are guaranteed to stay
connected for this time after sending a command.minConnectionTimeUnit
- The minimum coonnection time unit.socketFactory
- The non-null
factory to make Socket
s.public CallbackClient(int port, java.net.InetAddress address, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit, javax.net.SocketFactory socketFactory, boolean enableMemoryManagement)
port
- The port used by channels to connect to the Python side.address
- The addressed used by channels to connect to the Python side..minConnectionTime
- The minimum connection time: channels are guaranteed to stay
connected for this time after sending a command.minConnectionTimeUnit
- The minimum coonnection time unit.socketFactory
- The non-null
factory to make Socket
s.enableMemoryManagement
- If False, we do not send tell the Python side when a PythonProxy
is no longer used by the Java side.public CallbackClient(int port, java.net.InetAddress address, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit, javax.net.SocketFactory socketFactory, boolean enableMemoryManagement, int readTimeout)
port
- The port used by channels to connect to the Python side.address
- The addressed used by channels to connect to the Python side..minConnectionTime
- The minimum connection time: channels are guaranteed to stay
connected for this time after sending a command.minConnectionTimeUnit
- The minimum coonnection time unit.socketFactory
- The non-null
factory to make Socket
s.enableMemoryManagement
- If False, we do not send tell the Python side when a PythonProxy
is no longer used by the Java side.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.public CallbackClient(int port, java.net.InetAddress address, java.lang.String authToken, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit, javax.net.SocketFactory socketFactory, boolean enableMemoryManagement, int readTimeout)
port
- The port used by channels to connect to the Python side.address
- The addressed used by channels to connect to the Python side.authToken
- Token for authenticating with the callback server.minConnectionTime
- The minimum connection time: channels are guaranteed to stay
connected for this time after sending a command.minConnectionTimeUnit
- The minimum coonnection time unit.socketFactory
- The non-null
factory to make Socket
s.enableMemoryManagement
- If False, we do not send tell the Python side when a PythonProxy
is no longer used by the Java side.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.public java.net.InetAddress getAddress()
getAddress
in interface Py4JPythonClient
public boolean isMemoryManagementEnabled()
isMemoryManagementEnabled
in interface Py4JPythonClient
protected Py4JClientConnection getConnection() throws java.io.IOException
java.io.IOException
protected Py4JClientConnection getConnectionLock()
public int getPort()
getPort
in interface Py4JPythonClient
public int getReadTimeout()
getReadTimeout
in interface Py4JPythonClient
public Py4JPythonClient copyWith(java.net.InetAddress pythonAddress, int pythonPort)
Creates a callback client which connects to the given address and port,
but retains all the other settings (like the minConnectionTime
and the 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.
copyWith
in interface Py4JPythonClient
pythonAddress
- The address used by a PythonProxyHandler to connect to a
Python gateway.pythonPort
- The port used by a PythonProxyHandler to connect to a Python
gateway. Essentially the port used for Python callbacks.protected void giveBackConnection(Py4JClientConnection cc)
public void periodicCleanup()
Closes communication channels that have not been used for a time specified at the creation of the callback client.
Clients should not directly call this method: it is called by a periodic cleaner thread.
public java.lang.String sendCommand(java.lang.String command)
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.
sendCommand
in interface Py4JPythonClient
command
- The command to send.public java.lang.String sendCommand(java.lang.String command, boolean blocking)
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.
sendCommand
in interface Py4JPythonClient
command
- The command to send.blocking
- If the CallbackClient should wait for an answer (default
should be True, except for critical cases such as a
finalizer sending a command).public java.lang.Object getPythonServerEntryPoint(Gateway gateway, java.lang.Class[] interfacesToImplement)
Py4JPythonClient
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.
getPythonServerEntryPoint
in interface Py4JPythonClient
protected boolean shouldRetrySendCommand(Py4JClientConnection cc, Py4JNetworkException pne)
protected void setupCleaner()
public void shutdown()
Closes all active channels, stops the periodic cleanup of channels and mark the client as shutting down. No more commands can be sent after this method has been called, except commands that were initiated before the shutdown method was called..
shutdown
in interface Py4JPythonClient