py4j
Class GatewayServer

java.lang.Object
  extended by py4j.GatewayServer
All Implemented Interfaces:
Runnable

public class GatewayServer
extends Object
implements 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.

Author:
Barthelemy Dagenais

Field Summary
static int DEFAULT_CONNECT_TIMEOUT
           
static int DEFAULT_PORT
           
static int DEFAULT_READ_TIMEOUT
           
 
Constructor Summary
GatewayServer(Object entryPoint)
           Creates a GatewayServer instance with default port (25333), default address (localhost), and default timeout value (no timeout).
GatewayServer(Object entryPoint, int port)
           
GatewayServer(Object entryPoint, int port, int connectTimeout, int readTimeout, boolean acceptOnlyOne)
           
 
Method Summary
protected  Object createConnection(GatewayServer server, Gateway gateway, Socket socket)
           
 int getConnect_timeout()
           
 int getRead_timeout()
           
 boolean isAcceptOnlyOne()
           
 void run()
           
 void shutdown()
          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.
static void turnLoggingOff()
          Utility method to turn logging off.
static void turnLoggingOn()
          Utility method to turn logging on.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT

public static final int DEFAULT_PORT
See Also:
Constant Field Values

DEFAULT_CONNECT_TIMEOUT

public static final int DEFAULT_CONNECT_TIMEOUT
See Also:
Constant Field Values

DEFAULT_READ_TIMEOUT

public static final int DEFAULT_READ_TIMEOUT
See Also:
Constant Field Values
Constructor Detail

GatewayServer

public GatewayServer(Object entryPoint,
                     int port,
                     int connectTimeout,
                     int readTimeout,
                     boolean acceptOnlyOne)
Parameters:
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.
acceptOnlyOne - If true, only one Python program can connect to this GatewayServer at a time.

GatewayServer

public GatewayServer(Object entryPoint)

Creates a GatewayServer instance with default port (25333), default address (localhost), and default timeout value (no timeout).

Parameters:
entryPoint - The entry point of this Gateway. Can be null.

GatewayServer

public GatewayServer(Object entryPoint,
                     int port)
Parameters:
entryPoint - The entry point of this Gateway. Can be null.
port - The port the GatewayServer is listening to.
Method Detail

run

public void run()
Specified by:
run in interface Runnable

createConnection

protected Object createConnection(GatewayServer server,
                                  Gateway gateway,
                                  Socket socket)
                           throws IOException
Throws:
IOException

start

public void start(boolean fork)

Starts to accept connections.

Parameters:
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).

start

public void start()

Starts to accept connections in a second thread (non-blocking call).


shutdown

public void shutdown()

Stops accepting connections, closes all current connections, and calls Gateway.shutdown()


isAcceptOnlyOne

public boolean isAcceptOnlyOne()

getConnect_timeout

public int getConnect_timeout()

getRead_timeout

public int getRead_timeout()

turnLoggingOff

public static void turnLoggingOff()

Utility method to turn logging off. Logging is turned off by default.


turnLoggingOn

public static void turnLoggingOn()

Utility method to turn logging on. Logging is turned off by default.