public class SockIOPool extends Object
static {
String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverlist);
pool.initialize();
}
static {
String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
Integer[] weights = { new Integer(5), new Integer(2) };
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverlist);
pool.setWeights(weights);
pool.initialize();
}
static {
String[] serverlist = { "cache0.server.com:12345", "cache1.server.com:12345" };
Integer[] weights = { new Integer(5), new Integer(2) };
int initialConnections = 10;
int minSpareConnections = 5;
int maxSpareConnections = 50;
long maxIdleTime = 1000 * 60 * 30; // 30 minutes
long maxBusyTime = 1000 * 60 * 5; // 5 minutes
long maintThreadSleep = 1000 * 5; // 5 seconds
int socketTimeOut = 1000 * 3; // 3 seconds to block on reads
int socketConnectTO = 1000 * 3; // 3 seconds to block on initial
// connections. If 0, then will use blocking
// connect (default)
boolean failover = false; // turn off auto-failover in event of server down
boolean nagleAlg = false; // turn off Nagle's algorithm on all sockets in
// pool
boolean aliveCheck = false; // disable health check of socket on checkout
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverlist);
pool.setWeights(weights);
pool.setInitConn(initialConnections);
pool.setMinConn(minSpareConnections);
pool.setMaxConn(maxSpareConnections);
pool.setMaxIdle(maxIdleTime);
pool.setMaxBusyTime(maxBusyTime);
pool.setMaintSleep(maintThreadSleep);
pool.setSocketTO(socketTimeOut);
pool.setNagle(nagleAlg);
pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);
pool.setAliveCheck(true);
pool.initialize();
}
The easiest manner in which to initialize the pool is to set the servers and
rely on defaults as in the first example.
SockIOPool.SockIO sock = SockIOPool.getInstance().getSock( key );
try {
sock.write( "version\r\n" );
sock.flush();
System.out.println( "Version: " + sock.readLine() );
}
catch (IOException ioe) { System.out.println( "io exception thrown" ) };
sock.close();
| Modifier and Type | Class and Description |
|---|---|
protected static class |
SockIOPool.MaintThread
Class which extends thread and handles maintenance of the pool.
|
static class |
SockIOPool.SockIO
MemCached client for Java, utility class for Socket IO.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
CONSISTENT_HASH |
static long |
MAX_RETRY_DELAY |
static int |
NATIVE_HASH |
static int |
NEW_COMPAT_HASH |
static int |
OLD_COMPAT_HASH |
| Modifier | Constructor and Description |
|---|---|
protected |
SockIOPool() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addSocketToPool(Map<String,Map<SockIOPool.SockIO,Long>> pool,
String host,
SockIOPool.SockIO socket)
Adds a socket to a given pool for the given host.
|
protected void |
clearHostFromPool(Map<String,Map<SockIOPool.SockIO,Long>> pool,
String host)
Closes and removes all sockets from specified pool for host.
|
protected SockIOPool.SockIO |
createSocket(String host)
Creates a new SockIO obj for the given server.
|
boolean |
getAliveCheck()
Returns the current status of the aliveCheck flag.
|
int |
getBufferSize() |
SockIOPool.SockIO |
getConnection(String host)
Returns a SockIO object from the pool for the passed in host.
|
boolean |
getFailback()
Returns current state of failover flag.
|
boolean |
getFailover()
Returns current state of failover flag.
|
int |
getHashingAlg()
Returns current status of customHash flag
|
String |
getHost(String key) |
String |
getHost(String key,
Integer hashcode)
Gets the host that a particular key / hashcode resides on.
|
int |
getInitConn()
Returns the current setting for the initial number of connections per
server in the available pool.
|
static SockIOPool |
getInstance()
Single argument version of factory used for back compat.
|
static SockIOPool |
getInstance(boolean isTcp) |
static SockIOPool |
getInstance(String poolName)
Factory to create/retrieve new pools given a unique poolName.
|
static SockIOPool |
getInstance(String poolName,
boolean isTcp) |
long |
getMaintSleep()
Returns the current maint thread sleep time.
|
long |
getMaxBusy()
Returns the current max busy setting.
|
int |
getMaxConn()
Returns the maximum number of spare connections allowed in available
pool.
|
int |
getMaxIdle()
Returns the current max idle setting.
|
int |
getMinConn()
Returns the minimum number of spare connections in available pool.
|
boolean |
getNagle()
Returns current status of nagle flag
|
String[] |
getServers()
Returns the current list of all cache servers.
|
SockIOPool.SockIO |
getSock(String key)
Returns appropriate SockIO object given string cache key.
|
SockIOPool.SockIO |
getSock(String key,
Integer hashCode)
Returns appropriate SockIO object given string cache key and optional
hashcode.
|
int |
getSocketConnectTO()
Returns the socket timeout for connect.
|
int |
getSocketTO()
Returns the socket timeout for reads.
|
Integer[] |
getWeights()
Returns the current list of weights.
|
void |
initialize()
Initializes the pool.
|
boolean |
isInitialized()
Returns state of pool.
|
protected void |
removeSocketFromPool(Map<String,Map<SockIOPool.SockIO,Long>> pool,
String host,
SockIOPool.SockIO socket)
Removes a socket from specified pool for host.
|
protected void |
selfMaint()
Runs self maintenance on all internal pools.
|
void |
setAliveCheck(boolean aliveCheck)
Sets the aliveCheck flag for the pool.
|
void |
setBufferSize(int bufferSize)
In memcached 1.4+, user can specify memory size for each memcached item.
You can specify it with parameter "-I" in the server side. While in our client side, we make the max memcached item size with the default value 1Mb. In this scenario, you can extend the size as what you want, please be sure you have enough memory in the client side, since we are using direct buffer to hold the memory, they will not be free until shutdown. |
void |
setFailback(boolean failback)
Sets the failback flag for the pool.
|
void |
setFailover(boolean failover)
Sets the failover flag for the pool.
|
void |
setHashingAlg(int alg)
Sets the hashing algorithm we will use.
|
void |
setInitConn(int initConn)
Sets the initial number of connections per server in the available pool.
|
void |
setMaintSleep(long maintSleep)
Set the sleep time between runs of the pool maintenance thread.
|
void |
setMaxBusyTime(long maxBusyTime)
Sets the max busy time for threads in the busy pool.
|
void |
setMaxConn(int maxConn)
Sets the maximum number of spare connections allowed in our available
pool.
|
void |
setMaxIdle(int maxIdle)
Sets the max idle time for threads in the available pool.
|
void |
setMinConn(int minConn)
Sets the minimum number of spare connections to maintain in our available
pool.
|
void |
setNagle(boolean nagle)
Sets the Nagle alg flag for the pool.
|
void |
setServers(String[] servers)
Sets the list of all cache servers.
|
void |
setSocketConnectTO(int socketConnectTO)
Sets the socket timeout for connect.
|
void |
setSocketTO(int socketTO)
Sets the socket timeout for reads.
|
void |
setWeights(Integer[] weights)
Sets the list of weights to apply to the server list.
|
void |
shutDown()
Shuts down the pool.
|
public static final int NATIVE_HASH
public static final int OLD_COMPAT_HASH
public static final int NEW_COMPAT_HASH
public static final int CONSISTENT_HASH
public static final long MAX_RETRY_DELAY
public static SockIOPool getInstance(String poolName)
poolName - unique name of the poolpublic static SockIOPool getInstance(boolean isTcp)
public static SockIOPool getInstance(String poolName, boolean isTcp)
public static SockIOPool getInstance()
public void setServers(String[] servers)
servers - String array of servers [host:port]public String[] getServers()
public void setWeights(Integer[] weights)
weights - Integer array of weightspublic Integer[] getWeights()
public void setInitConn(int initConn)
initConn - int number of connectionspublic int getInitConn()
public void setMinConn(int minConn)
minConn - number of connectionspublic int getMinConn()
public void setMaxConn(int maxConn)
maxConn - number of connectionspublic int getMaxConn()
public void setMaxBusyTime(long maxBusyTime)
maxBusyTime - idle time in mspublic long getMaxBusy()
public void setSocketTO(int socketTO)
socketTO - timeout in mspublic int getSocketTO()
public void setSocketConnectTO(int socketConnectTO)
socketConnectTO - timeout in mspublic int getSocketConnectTO()
public void setMaxIdle(int maxIdle)
maxIdle - idle time in mspublic int getMaxIdle()
public void setMaintSleep(long maintSleep)
maintSleep - sleep time in mspublic long getMaintSleep()
public void setFailover(boolean failover)
failover - true/falsepublic boolean getFailover()
public void setFailback(boolean failback)
failback - true/falsepublic boolean getFailback()
public void setAliveCheck(boolean aliveCheck)
aliveCheck - true/falsepublic boolean getAliveCheck()
public void setNagle(boolean nagle)
nagle - true/falsepublic boolean getNagle()
public void setHashingAlg(int alg)
alg - int value representing hashing algorithmpublic int getHashingAlg()
public void initialize()
public boolean isInitialized()
true if initialized.public String getHost(String key, Integer hashcode)
key - hashcode - public void shutDown()
public void setBufferSize(int bufferSize)
bufferSize - specified buffer size.public int getBufferSize()
public SockIOPool.SockIO getSock(String key)
key - hashcode for cache keypublic SockIOPool.SockIO getSock(String key, Integer hashCode)
key - hashcode for cache keyhashCode - if not null, then the int hashcode to usepublic SockIOPool.SockIO getConnection(String host)
host - host from which to retrieve objectprotected SockIOPool.SockIO createSocket(String host)
host - host:port to connect toprotected void selfMaint()
protected void addSocketToPool(Map<String,Map<SockIOPool.SockIO,Long>> pool, String host, SockIOPool.SockIO socket)
pool - pool to add tohost - host this socket is connected tosocket - socket to addprotected void removeSocketFromPool(Map<String,Map<SockIOPool.SockIO,Long>> pool, String host, SockIOPool.SockIO socket)
pool - pool to remove fromhost - host poolsocket - socket to removeprotected void clearHostFromPool(Map<String,Map<SockIOPool.SockIO,Long>> pool, String host)
pool - pool to clearhost - host to clearCopyright © 2013 Schooner Information Technology. All Rights Reserved.