net.spy.memcached
Class MemcachedConnection

java.lang.Object
  extended by java.lang.Thread
      extended by net.spy.memcached.compat.SpyThread
          extended by net.spy.memcached.MemcachedConnection
All Implemented Interfaces:
java.lang.Runnable

public class MemcachedConnection
extends SpyThread

Connection to a cluster of memcached servers.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> addedQueue
           
protected  FailureMode failureMode
           
protected  NodeLocator locator
           
protected  java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> nodesToShutdown
           
protected  boolean running
           
protected  java.nio.channels.Selector selector
           
protected  boolean shutDown
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
MemcachedConnection(int bufSize, ConnectionFactory f, java.util.List<java.net.InetSocketAddress> a, java.util.Collection<ConnectionObserver> obs, FailureMode fm, OperationFactory opfactory)
          Construct a memcached connection.
 
Method Summary
 boolean addObserver(ConnectionObserver obs)
          Add a connection observer.
protected  void addOperation(MemcachedNode node, Operation o)
           
protected  void addOperation(java.lang.String key, Operation o)
          Add an operation to the given connection.
 void addOperations(java.util.Map<MemcachedNode,Operation> ops)
           
 java.util.concurrent.CountDownLatch broadcastOperation(BroadcastOpFactory of)
          Broadcast an operation to all nodes.
 java.util.concurrent.CountDownLatch broadcastOperation(BroadcastOpFactory of, java.util.Collection<MemcachedNode> nodes)
          Broadcast an operation to a specific collection of nodes.
protected  void checkState()
           
protected  java.util.List<MemcachedNode> createConnections(java.util.Collection<java.net.InetSocketAddress> a)
           
 void enqueueOperation(java.lang.String key, Operation o)
           
 NodeLocator getLocator()
          Get the node locator used by this connection.
 void handleIO()
          MemcachedClient calls this method to handle IO over the connections.
 void insertOperation(MemcachedNode node, Operation o)
           
static void opSucceeded(Operation op)
          helper method: reset timeout counter.
static void opTimedOut(Operation op)
          helper method: increase timeout count on node attached to this op.
protected  void queueReconnect(MemcachedNode qa)
           
 boolean removeObserver(ConnectionObserver obs)
          Remove a connection observer.
 void run()
          Infinitely loop processing IO.
 void shutdown()
          Shut down all of the connections.
 java.lang.String toString()
           
 
Methods inherited from class net.spy.memcached.compat.SpyThread
getLogger
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

shutDown

protected volatile boolean shutDown

selector

protected java.nio.channels.Selector selector

locator

protected final NodeLocator locator

failureMode

protected final FailureMode failureMode

addedQueue

protected final java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> addedQueue

running

protected volatile boolean running

nodesToShutdown

protected final java.util.concurrent.ConcurrentLinkedQueue<MemcachedNode> nodesToShutdown
Constructor Detail

MemcachedConnection

public MemcachedConnection(int bufSize,
                           ConnectionFactory f,
                           java.util.List<java.net.InetSocketAddress> a,
                           java.util.Collection<ConnectionObserver> obs,
                           FailureMode fm,
                           OperationFactory opfactory)
                    throws java.io.IOException
Construct a memcached connection.

Parameters:
bufSize - the size of the buffer used for reading from the server
f - the factory that will provide an operation queue
a - the addresses of the servers to connect to
Throws:
java.io.IOException - if a connection attempt fails early
Method Detail

createConnections

protected java.util.List<MemcachedNode> createConnections(java.util.Collection<java.net.InetSocketAddress> a)
                                                   throws java.io.IOException
Throws:
java.io.IOException

handleIO

public void handleIO()
              throws java.io.IOException
MemcachedClient calls this method to handle IO over the connections.

Throws:
java.io.IOException

addObserver

public boolean addObserver(ConnectionObserver obs)
Add a connection observer.

Returns:
whether the observer was successfully added

removeObserver

public boolean removeObserver(ConnectionObserver obs)
Remove a connection observer.

Returns:
true if the observer existed and now doesn't

queueReconnect

protected void queueReconnect(MemcachedNode qa)

getLocator

public NodeLocator getLocator()
Get the node locator used by this connection.


enqueueOperation

public void enqueueOperation(java.lang.String key,
                             Operation o)

addOperation

protected void addOperation(java.lang.String key,
                            Operation o)
Add an operation to the given connection.

Parameters:
key - the key the operation is operating upon
o - the operation

insertOperation

public void insertOperation(MemcachedNode node,
                            Operation o)

addOperation

protected void addOperation(MemcachedNode node,
                            Operation o)

addOperations

public void addOperations(java.util.Map<MemcachedNode,Operation> ops)

broadcastOperation

public java.util.concurrent.CountDownLatch broadcastOperation(BroadcastOpFactory of)
Broadcast an operation to all nodes.


broadcastOperation

public java.util.concurrent.CountDownLatch broadcastOperation(BroadcastOpFactory of,
                                                              java.util.Collection<MemcachedNode> nodes)
Broadcast an operation to a specific collection of nodes.


shutdown

public void shutdown()
              throws java.io.IOException
Shut down all of the connections.

Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Thread

opTimedOut

public static void opTimedOut(Operation op)
helper method: increase timeout count on node attached to this op.

Parameters:
op -

opSucceeded

public static void opSucceeded(Operation op)
helper method: reset timeout counter.

Parameters:
op -

checkState

protected void checkState()

run

public void run()
Infinitely loop processing IO.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread


Copyright © 2006-2009 Dustin Sallings, 2009-2012 Couchbase, Inc.