Class CallbackClient
- All Implemented Interfaces:
Py4JPythonClient
- Direct Known Subclasses:
PythonClient
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.
- Author:
- Barthelemy Dagenais
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final InetAddress
protected final String
protected final Deque<Py4JClientConnection>
static final String
static final long
static final TimeUnit
protected final boolean
protected final Lock
protected final long
protected final TimeUnit
protected final int
protected final int
protected final SocketFactory
-
Constructor Summary
ConstructorsConstructorDescriptionCallbackClient
(int port) CallbackClient
(int port, InetAddress address) CallbackClient
(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit) CallbackClient
(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory) CallbackClient
(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, boolean enableMemoryManagement) CallbackClient
(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, boolean enableMemoryManagement, int readTimeout) CallbackClient
(int port, InetAddress address, String authToken) CallbackClient
(int port, InetAddress address, String authToken, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, boolean enableMemoryManagement, int readTimeout) -
Method Summary
Modifier and TypeMethodDescriptioncopyWith
(InetAddress pythonAddress, int pythonPort) Creates a callback client which connects to the given address and port, but retains all the other settings (like theminConnectionTime
and thesocketFactory
.protected Py4JClientConnection
protected Py4JClientConnection
int
getPort()
getPythonServerEntryPoint
(Gateway gateway, Class[] interfacesToImplement) Gets a reference to the entry point on the Python side.int
protected void
boolean
void
Closes communication channels that have not been used for a time specified at the creation of the callback client.sendCommand
(String command) Sends a command to the Python side.sendCommand
(String command, boolean blocking) Sends a command to the Python side.protected void
protected boolean
void
shutdown()
Closes all active channels, stops the periodic cleanup of channels and mark the client as shutting down.
-
Field Details
-
DEFAULT_ADDRESS
- See Also:
-
port
protected final int port -
address
-
socketFactory
-
connections
-
lock
-
DEFAULT_MIN_CONNECTION_TIME
public static final long DEFAULT_MIN_CONNECTION_TIME- See Also:
-
DEFAULT_MIN_CONNECTION_TIME_UNIT
-
minConnectionTime
protected final long minConnectionTime -
minConnectionTimeUnit
-
enableMemoryManagement
protected final boolean enableMemoryManagement -
readTimeout
protected final int readTimeout -
authToken
-
-
Constructor Details
-
CallbackClient
public CallbackClient(int port) -
CallbackClient
-
CallbackClient
-
CallbackClient
public CallbackClient(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit) -
CallbackClient
public CallbackClient(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory) - Parameters:
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 makeSocket
s.
-
CallbackClient
public CallbackClient(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, boolean enableMemoryManagement) - Parameters:
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 makeSocket
s.enableMemoryManagement
- If False, we do not send tell the Python side when a PythonProxy is no longer used by the Java side.
-
CallbackClient
public CallbackClient(int port, InetAddress address, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, boolean enableMemoryManagement, int readTimeout) - Parameters:
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 makeSocket
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.
-
CallbackClient
public CallbackClient(int port, InetAddress address, String authToken, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, boolean enableMemoryManagement, int readTimeout) - Parameters:
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 makeSocket
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.
-
-
Method Details
-
getAddress
- Specified by:
getAddress
in interfacePy4JPythonClient
-
isMemoryManagementEnabled
public boolean isMemoryManagementEnabled()- Specified by:
isMemoryManagementEnabled
in interfacePy4JPythonClient
-
getConnection
- Throws:
IOException
-
getConnectionLock
-
getPort
public int getPort()- Specified by:
getPort
in interfacePy4JPythonClient
-
getReadTimeout
public int getReadTimeout()- Specified by:
getReadTimeout
in interfacePy4JPythonClient
-
copyWith
Creates a callback client which connects to the given address and port, but retains all the other settings (like the
minConnectionTime
and thesocketFactory
. 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.- Specified by:
copyWith
in interfacePy4JPythonClient
- Parameters:
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.
-
giveBackConnection
-
periodicCleanup
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.
-
sendCommand
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.
- Specified by:
sendCommand
in interfacePy4JPythonClient
- Parameters:
command
- The command to send.- Returns:
- The response.
-
sendCommand
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.
- Specified by:
sendCommand
in interfacePy4JPythonClient
- Parameters:
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).- Returns:
- The response.
-
getPythonServerEntryPoint
Description copied from interface: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.
- Specified by:
getPythonServerEntryPoint
in interfacePy4JPythonClient
- Returns:
-
shouldRetrySendCommand
-
setupCleaner
protected void setupCleaner() -
shutdown
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..
- Specified by:
shutdown
in interfacePy4JPythonClient
-