Connection pools

From JPPF Documentation

Jump to: navigation, search

Contents

Main Page > Development guide > Connection pools


All server connections in a JPPF client are organized into connection pools, whose number is determined by the client configuration properties, and whose size is based on the configuration as well and can also be changed dynamically via the JPPF APIs. In the next sections, we will see how connection pools can be configured, explored and programmatically accessed.

The JPPFConnectionPool class

A connection pool is represented by the JPPFConnectionPool class, defined as follows:

public class JPPFConnectionPool extends AbstractConnectionPool<JPPFClientConnection>
  implements Comparable<JPPFConnectionPool> {
  // Get the name of this pool
  public String getName()
  // Get the id of this pool, unique within a JPPFClient instance
  public int getId()
  // Get the priority associated with this pool
  public int getPriority()
  // Check whether this pool is for SSL connections
  public boolean isSslEnabled()
  // Get the uuid of the driver to which connections in this pool are connected
  public String getDriverUuid()
  // Get the host name or IP address of the remote driver
  public String getDriverHost()
  // Get the port number to which the client connects on the remote driver
  public int getDriverPort()
  // Set the maximum size of this pool, starting or stopping connections as needed
  public int setMaxSize(int maxSize)
  // Get a list of connections in this pool whose status is one of those specified
  public List<JPPFClientConnection> getConnections(JPPFClientConnectionStatus...statuses)
  // Get the number of connections in this pool whose status is one of those specified
  public int connectionCount(JPPFClientConnectionStatus...statuses)
}

As we can see, JPPFConnectionPool extends the class AbstractConnectionPool, which in turn implements the interface ConnectionPool, defined as follows:

public interface ConnectionPool<E extends AutoCloseable>
  extends Iterable<E>, AutoCloseable {
  // Get the next connection that is connected and available
  E getConnection();
  // Determine whether this pool is empty
  boolean isEmpty();
  // Get the current size of this pool
  int connectionCount();
  // Get the core size of this connection pool
  int getCoreSize();
  // Get the maximum size of this connection pool
  int getMaxSize();
  // Set the maximum size of this pool, starting or stopping connections as needed
  int setMaxSize(int maxSize);
  // Get a list of connections held by this pool
  List<E> getConnections();
}

The pool name is based on the client configuration properties and defined as follows:

With server discovery disabled:

jppf.discovery.enabled = false
jppf.drivers = <driver_name_1> ... <driver_name_N>

Each driver name specified in “jppf.drivers” will be the name of the corresponding connection pool.

With discovery enabled: for each discovered server, the JPPF client will create a connection pool named “jppf_discovery-n” where n is a sequence number automatically assigned by the client.

The id attribute of a pool is a sequence number that is guaranteed to be unique within a JPPF client. It is used to distinguish pools that may have the same driver uuid, priority and size. The name may be used similarly, however JPPF does not do any checking on pool names, so they should be used with caution.

The pool's priority is as defined in the configuration. For instance if we have the following:

jppf.drivers = myPool
myPool.jppf.server.host = www.myhost.com
myPool.jppf.server.port = 11111
myPool.jppf.priority = 10
# core pool size
myPool.jppf.pool.size = 5

The corresponding JPPFConnectionPool object's getPriority() method will return 10.

In the same way, the getCoreSize() method will return 5.

The pool's actual size can be grown or shrunk dynamically, using the setMaxSize(int) method. The JPPF client will create or close connections accordingly. An attempt to set a max size smaller than the core size, or equal to the current max size, will have no effect whatsoever. In some cases, whern trying to reduce the connection pool's max size, there may be too many connections in the pool busy executing jobs and the client will not be close all the requested connections. In this case, setMaxSize() will return the new actual size, which will be smaller than the requested size.

The pool core size is defined in the configuration, either with the <poolName>.jppf.pool.size property for manually configured pools, or with jppf.pool.size for auto-discovered pools. Its value cannot be changed for any JPPFConnectionPool instance.

The two getConnections() methods allow you to explore the connections currently in the pool. The overloaded version of this method permits filtering of the connections by their status, represented by one or more JPPFClientConnectionStatus enum values.

Associated JMX connection pool

Each connection pool has an associated pool of JMX connections to the same remote driver.. To access and manipulate this JMX pool, the JPPFConnectionPool class provides the following API:

public class JPPFConnectionPool extends AbstractConnectionPool<JPPFClientConnection>
  implements Comparable<JPPFConnectionPool> {
  // Get a connected JMX connection among those in the JMX pool
  public JMXDriverConnectionWrapper getJmxConnection()
  // Get a JMX connection in the specified state from the JMX pool
  public JMXDriverConnectionWrapper getJmxConnection(boolean connectedOnly)
  // Get the jmx port to use on the remote driver
  public int getJmxPort()
  // Get a connected JMX connection among those in the JMX pool
  public JMXDriverConnectionWrapper getJmxConnection()
  // Get a JMX connection with the specified state among those in the JMX pool
  public JMXDriverConnectionWrapper getJmxConnection(final boolean connectedOnly)
  // Get the core size of the associated JMX connection pool
  public int getJMXPoolCoreSize()
  // Get the current maximum size of the associated JMX connection pool
  public int getJMXPoolMaxSize()
  // Set a new maximum size for the associated pool of JMX connections,
  // adding new or closing existing connections as needed
  public int setJMXPoolMaxSize(int maxSize)
  // Get the list of connections currently in the JMX pool
  public List<JMXDriverConnectionWrapper> getJMXConnections()
}

Note that the JMX pool core size, when left unspecified, defaults to 1. Otherwise, it is defined in the configuration as:

When discovery is enabled:

jppf.jmx.pool.size = 5

When discovery is disabled:

driver-1.jppf.jmx.pool.size = 5

Also note that the driver host for a JMX connection is the same as JPPFConnectionPool.getDriverHost(). In the same way, to determine whether a JMX connection is secure, JPPFConnectionPool.isSSLEnabled() should be used.

Exploring the connection pools

The JPPFClient class, or more exactly its super-super class AbstractJPPFClient, provides a number of methods to discover and explore the connection pools currently handled by the client:

public class JPPFClient extends AbstractGenericClient { ... }

public abstract class AbstractGenericClient extends AbstractJPPFClient { ... }

public abstract class AbstractJPPFClient
  implements ClientConnectionStatusListener, AutoCloseable {
  // Find the connection pool with the specified priority and id
  public JPPFConnectionPool findConnectionPool(int priority, int poolId)
  // Find the connection pool with the specified id
  public JPPFConnectionPool findConnectionPool(int poolId)
  // Find the connection pool with the specified name
  public JPPFConnectionPool findConnectionPool(String name)
  // Find the connection pools whose name matches the specified regular expression
  public List<JPPFConnectionPool> findConnectionPools(String name)
  // Find the connection pools that have at least one connection matching
  // one of the specified statuses
  public List<JPPFConnectionPool> findConnectionPools(
    JPPFClientConnectionStatus...statuses)
  // Get a set of existing connection pools with the specified priority
  public List<JPPFConnectionPool> getConnectionPools(int priority)
  // Get a list of all priorities for the currently existing pools in descending order
  public List<Integer> getPoolPriorities()
  // Get a list of existing connection pools, ordered by descending priority
  public List<JPPFConnectionPool> getConnectionPools()
  // Get a pool with the highest possible priority that has at least 1 active connection
  public JPPFConnectionPool getConnectionPool()
  // Get the connection pools that pass the specified filter
  public List<JPPFConnectionPool> findConnectionPools(
    ConnectionPoolFilter<JPPFConnectionPool> filter)
}

Note that the connection pools are held in a multimap-like data structure, where the key is the pool priority sorted in descending order (highest priority first). Consequently, all getXXX() and findXXX() methods which return a list of connection pools are guaranteed to have the resulting elements od the list sorted by order of descending priority.

The last findConnectionPools() method provides a generic way of filtering the existing connection pools, by making use of a ConnectionPoolFilter, defined as follows:

public interface ConnectionPoolFilter<E extends ConnectionPool> {
  // Determine whether this filter accepts the specified connection pool
  boolean accepts(E pool);
}

Main Page > Development guide > Connection pools

Support This Project Powered by MediaWiki