public class GatewayServer extends DefaultGatewayServerListener implements Py4JJavaServer, java.lang.Runnable
This class enables Python programs to access a Java program. When a GatewayServer instance is started, Python programs can connect to the JVM by calling:
gateway = JavaGateway()
The
entryPoint
passed to a GatewayServer can be accessed with the entry_point
member:
gateway.entry_point
Technically, a GatewayServer is only responsible for accepting connection.
Each connection is then handled by a GatewayConnection
instance and the various states (e.g., entryPoint,
reference to returned objects) are managed by a Gateway
instance.
Modifier and Type | Class and Description |
---|---|
static class |
GatewayServer.GatewayServerBuilder
Helper class to make it easier and self-documenting how a
GatewayServer is constructed. |
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
authToken |
static java.lang.String |
DEFAULT_ADDRESS |
static int |
DEFAULT_CONNECT_TIMEOUT |
static java.lang.String |
DEFAULT_IPv6_ADDRESS |
static int |
DEFAULT_PORT |
static int |
DEFAULT_PYTHON_PORT |
static int |
DEFAULT_READ_TIMEOUT |
static java.lang.String |
GATEWAY_SERVER_ID |
static java.util.logging.Logger |
PY4J_LOGGER |
Constructor and Description |
---|
GatewayServer()
Creates a GatewayServer instance with default port (25333), default
address (127.0.0.1), and default timeout value (no timeout).
|
GatewayServer(Gateway gateway,
int port,
java.net.InetAddress address,
int connectTimeout,
int readTimeout,
java.util.List<java.lang.Class<? extends Command>> customCommands,
javax.net.ServerSocketFactory sSocketFactory) |
GatewayServer(java.lang.Object entryPoint)
Creates a GatewayServer instance with default port (25333), default
address (127.0.0.1), and default timeout value (no timeout).
|
GatewayServer(java.lang.Object entryPoint,
int port) |
GatewayServer(java.lang.Object entryPoint,
int port,
java.net.InetAddress address,
int connectTimeout,
int readTimeout,
java.util.List<java.lang.Class<? extends Command>> customCommands,
Py4JPythonClient cbClient) |
GatewayServer(java.lang.Object entryPoint,
int port,
java.net.InetAddress address,
int connectTimeout,
int readTimeout,
java.util.List<java.lang.Class<? extends Command>> customCommands,
Py4JPythonClient cbClient,
javax.net.ServerSocketFactory sSocketFactory) |
GatewayServer(java.lang.Object entryPoint,
int port,
int pythonPort,
java.net.InetAddress address,
java.net.InetAddress pythonAddress,
int connectTimeout,
int readTimeout,
java.util.List<java.lang.Class<? extends Command>> customCommands) |
GatewayServer(java.lang.Object entryPoint,
int port,
int connectTimeout,
int readTimeout) |
GatewayServer(java.lang.Object entryPoint,
int port,
int pythonPort,
int connectTimeout,
int readTimeout,
java.util.List<java.lang.Class<? extends Command>> customCommands) |
GatewayServer(java.lang.Object entryPoint,
int port,
int connectTimeout,
int readTimeout,
java.util.List<java.lang.Class<? extends Command>> customCommands,
Py4JPythonClient cbClient) |
Modifier and Type | Method and Description |
---|---|
void |
addListener(GatewayServerListener listener) |
void |
connectionStopped(Py4JServerConnection gatewayConnection) |
protected Py4JServerConnection |
createConnection(Gateway gateway,
java.net.Socket socket)
Creates a server connection from a Python call to the Java side.
|
static java.net.InetAddress |
defaultAddress() |
static java.net.InetAddress |
defaultIPv6Address() |
protected void |
fireConnectionError(java.lang.Exception e) |
protected void |
fireConnectionStarted(Py4JServerConnection gatewayConnection) |
protected void |
fireServerError(java.lang.Exception e) |
protected void |
fireServerPostShutdown() |
protected void |
fireServerPreShutdown() |
protected void |
fireServerStarted() |
protected void |
fireServerStopped() |
java.net.InetAddress |
getAddress() |
Py4JPythonClient |
getCallbackClient() |
int |
getConnectTimeout() |
java.util.List<java.lang.Class<? extends Command>> |
getCustomCommands() |
Gateway |
getGateway() |
java.util.List<GatewayServerListener> |
getListeners() |
int |
getListeningPort() |
int |
getPort() |
java.net.InetAddress |
getPythonAddress() |
int |
getPythonPort() |
java.lang.Object |
getPythonServerEntryPoint(java.lang.Class[] interfacesToImplement)
Gets a reference to the entry point on the Python side.
|
int |
getReadTimeout() |
static void |
main(java.lang.String[] args)
Main method to start a local GatewayServer on either a given port or the default one.
|
protected void |
processSocket(java.net.Socket socket) |
void |
removeListener(GatewayServerListener listener) |
void |
resetCallbackClient(java.net.InetAddress pythonAddress,
int pythonPort)
Replace the callback client with the new one which connects to the given address
and port.
|
void |
run() |
void |
shutdown()
Stops accepting connections, closes all current connections, and calls
Gateway.shutdown() |
void |
shutdown(boolean shutdownCallbackClient)
Stops accepting connections, closes all current connections, and calls
Gateway.shutdown() |
void |
start()
Starts to accept connections in a second thread (non-blocking call).
|
void |
start(boolean fork)
Starts to accept connections.
|
protected void |
startSocket()
Starts the ServerSocket.
|
static void |
turnAllLoggingOn()
Utility method to turn logging on.
|
static void |
turnLoggingOff()
Utility method to turn logging off.
|
static void |
turnLoggingOn()
Utility method to turn logging on.
|
connectionError, connectionStarted, serverError, serverPostShutdown, serverPreShutdown, serverStarted, serverStopped
public static final java.lang.String DEFAULT_ADDRESS
public static final java.lang.String DEFAULT_IPv6_ADDRESS
public static final int DEFAULT_PORT
public static final int DEFAULT_PYTHON_PORT
public static final int DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_READ_TIMEOUT
public static final java.lang.String GATEWAY_SERVER_ID
public static final java.util.logging.Logger PY4J_LOGGER
protected final java.lang.String authToken
public GatewayServer()
Creates a GatewayServer instance with default port (25333), default address (127.0.0.1), and default timeout value (no timeout).
public GatewayServer(java.lang.Object entryPoint)
Creates a GatewayServer instance with default port (25333), default address (127.0.0.1), and default timeout value (no timeout).
entryPoint
- The entry point of this Gateway. Can be null.public GatewayServer(java.lang.Object entryPoint, int port)
entryPoint
- The entry point of this Gateway. Can be null.port
- The port the GatewayServer is listening to.public GatewayServer(java.lang.Object entryPoint, int port, int pythonPort, java.net.InetAddress address, java.net.InetAddress pythonAddress, int connectTimeout, int readTimeout, java.util.List<java.lang.Class<? extends Command>> customCommands)
entryPoint
- The entry point of this Gateway. Can be null.port
- The port the GatewayServer is listening to.pythonPort
- The port used by a PythonProxyHandler to connect to a Python
gateway. Essentially the port used for Python callbacks.address
- The address the GatewayServer is listening to.pythonAddress
- The address used by a PythonProxyHandler to connect to a
Python gateway.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.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.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(java.lang.Object entryPoint, int port, int connectTimeout, int readTimeout)
entryPoint
- The entry point of this Gateway. Can be null.port
- The port the GatewayServer is listening to.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.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 GatewayServer(java.lang.Object entryPoint, int port, int pythonPort, int connectTimeout, int readTimeout, java.util.List<java.lang.Class<? extends Command>> customCommands)
entryPoint
- The entry point of this Gateway. Can be null.port
- The port the GatewayServer is listening to.pythonPort
- The port used by a PythonProxyHandler to connect to a Python
gateway. Essentially the port used for Python callbacks.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.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.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(java.lang.Object entryPoint, int port, int connectTimeout, int readTimeout, java.util.List<java.lang.Class<? extends Command>> customCommands, Py4JPythonClient cbClient)
entryPoint
- The entry point of this Gateway. Can be null.port
- The port the GatewayServer is listening to.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.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.customCommands
- A list of custom Command classes to augment the Server
features. These commands will be accessible from Python
programs. Can be null.cbClient
- An instance of a callback client.public GatewayServer(java.lang.Object entryPoint, int port, java.net.InetAddress address, int connectTimeout, int readTimeout, java.util.List<java.lang.Class<? extends Command>> customCommands, Py4JPythonClient cbClient)
entryPoint
- The entry point of this Gateway. Can be null.port
- The port the GatewayServer is listening to.address
- The address the GatewayServer is listening to.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.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.customCommands
- A list of custom Command classes to augment the Server
features. These commands will be accessible from Python
programs. Can be null.cbClient
- An instance of a callback client.public GatewayServer(java.lang.Object entryPoint, int port, java.net.InetAddress address, int connectTimeout, int readTimeout, java.util.List<java.lang.Class<? extends Command>> customCommands, Py4JPythonClient cbClient, javax.net.ServerSocketFactory sSocketFactory)
entryPoint
- The entry point of this Gateway. Can be null.port
- The port the GatewayServer is listening to.address
- The address the GatewayServer is listening to.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.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.customCommands
- A list of custom Command classes to augment the Server
features. These commands will be accessible from Python
programs. Can be null.sSocketFactory
- A factory that creates the server sockets that we listen on.public GatewayServer(Gateway gateway, int port, java.net.InetAddress address, int connectTimeout, int readTimeout, java.util.List<java.lang.Class<? extends Command>> customCommands, javax.net.ServerSocketFactory sSocketFactory)
gateway
- gateway instance (or subclass). Must not be null
.port
- the host port to usuaddress
- the host address to useconnectTimeout
- the connect timeout (ms)readTimeout
- the read timeout (ms)customCommands
- any customCommands to use. May be null
sSocketFactory
- socketFactory to use. Must not be null
public static void turnAllLoggingOn()
Utility method to turn logging on. Logging is turned off by default. All log messages will be logged.
public static void turnLoggingOff()
Utility method to turn logging off. Logging is turned off by default.
public static void turnLoggingOn()
Utility method to turn logging on. Logging is turned off by default. Log messages up to INFO level will be logged.
public static java.net.InetAddress defaultAddress()
public static java.net.InetAddress defaultIPv6Address()
public void resetCallbackClient(java.net.InetAddress pythonAddress, int pythonPort)
Replace the callback client with the new one which connects to the given address and port. 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.
This method is not thread-safe! Make sure that only one thread calls this method.
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.public void addListener(GatewayServerListener listener)
addListener
in interface Py4JJavaServer
public void connectionStopped(Py4JServerConnection gatewayConnection)
connectionStopped
in interface GatewayServerListener
connectionStopped
in class DefaultGatewayServerListener
protected Py4JServerConnection createConnection(Gateway gateway, java.net.Socket socket) throws java.io.IOException
Creates a server connection from a Python call to the Java side.
gateway
- socket
- java.io.IOException
protected void fireConnectionError(java.lang.Exception e)
protected void fireConnectionStarted(Py4JServerConnection gatewayConnection)
protected void fireServerError(java.lang.Exception e)
protected void fireServerPostShutdown()
protected void fireServerPreShutdown()
protected void fireServerStarted()
protected void fireServerStopped()
public java.net.InetAddress getAddress()
getAddress
in interface Py4JJavaServer
public Py4JPythonClient getCallbackClient()
public int getConnectTimeout()
public Gateway getGateway()
getGateway
in interface Py4JJavaServer
public int getListeningPort()
getListeningPort
in interface Py4JJavaServer
public int getPort()
getPort
in interface Py4JJavaServer
public java.net.InetAddress getPythonAddress()
getPythonAddress
in interface Py4JJavaServer
public int getPythonPort()
getPythonPort
in interface Py4JJavaServer
public int getReadTimeout()
protected void processSocket(java.net.Socket socket)
public void removeListener(GatewayServerListener listener)
removeListener
in interface Py4JJavaServer
public void run()
run
in interface java.lang.Runnable
public void shutdown()
Stops accepting connections, closes all current connections, and calls
Gateway.shutdown()
shutdown
in interface Py4JJavaServer
public void shutdown(boolean shutdownCallbackClient)
Stops accepting connections, closes all current connections, and calls
Gateway.shutdown()
shutdown
in interface Py4JJavaServer
shutdownCallbackClient
- If True, shuts down the CallbackClient
instance.public void start()
Starts to accept connections in a second thread (non-blocking call).
start
in interface Py4JJavaServer
public void start(boolean fork)
Starts to accept connections.
start
in interface Py4JJavaServer
fork
- If true, the GatewayServer accepts connection in another
thread and this call is non-blocking. If False, the
GatewayServer accepts connection in this thread and the call
is blocking (until the Gateway is shutdown by another thread).Py4JNetworkException
- If the server socket cannot start.protected void startSocket() throws Py4JNetworkException
Starts the ServerSocket.
Py4JNetworkException
- If the port is busy.public java.lang.Object getPythonServerEntryPoint(java.lang.Class[] interfacesToImplement)
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.
interfacesToImplement
- public static void main(java.lang.String[] args)
Main method to start a local GatewayServer on either a given port or the default one. The listening port is printed to stdout so that clients can start servers on ephemeral ports.
If authentication is enabled, the server will create an auth secret with 256 bits of entropy and print it to stdout after the server port. Clients should then provide this secret when connecting to the server. Note that no second line of output is printed if authentication is not enabled.
public java.util.List<java.lang.Class<? extends Command>> getCustomCommands()
public java.util.List<GatewayServerListener> getListeners()
getListeners
in interface Py4JJavaServer