com.sleepycat.je.rep.utilint
Class ServiceDispatcher

java.lang.Object
  extended by java.lang.Thread
      extended by com.sleepycat.je.utilint.StoppableThread
          extended by com.sleepycat.je.rep.utilint.ServiceDispatcher
All Implemented Interfaces:
ExceptionListenerUser, Runnable

public class ServiceDispatcher
extends StoppableThread

ServiceDispatcher listens on a specific socket for service requests and dispatches control to the service that is being requested. A service request message has the format: Service: The format of the message is binary, with all text being encoded in ascii. Upon receipt of service request message, the new SocketChannel is queued for processing by the service in the Queue associated with the service. The SocketChannel is the responsibility of the service after this point. It can configure the channel to best suit the requirements of the specific service. The dispatcher returns a single byte to indicate success or failure. The byte value encodes a ServiceDispatcher.Response enumerator.


Nested Class Summary
static class ServiceDispatcher.ExecutingRunnable
           
static class ServiceDispatcher.ExecutingService
          A service that is run immediately in a thread allocated to it.
 class ServiceDispatcher.LazyQueuingService
          A queuing service that starts the thread that services the requests lazily, upon first request and terminates the thread when the service is unregistered.
 class ServiceDispatcher.QueuingService
          A service where requests are simply added to the supplied queue.
static class ServiceDispatcher.Response
          The response to a service request.
static class ServiceDispatcher.ServiceConnectFailedException
           
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class com.sleepycat.je.utilint.StoppableThread
envImpl
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ServiceDispatcher(InetSocketAddress socketAddress)
          Convenience overloading for when the dispatcher is created without a replicated environment, e.g.
ServiceDispatcher(InetSocketAddress socketAddress, RepImpl repImpl)
          Create a ServiceDispatcher listening on a specific socket for service requests.
 
Method Summary
 void cancel(String serviceName)
          Cancels the registration of a service.
static void doServiceHandshake(SocketChannel channel, String serviceName)
          A variation on the method above.
protected  Logger getLogger()
           
static OutputStream getServiceOutputStream(Socket socket, String serviceName)
          Used by the client to establish an output stream for the service on the socket.
 InetSocketAddress getSocketAddress()
          Returns the socket associated with the dispatcher
protected  int initiateSoftShutdown()
          Threads that use shutdownThread() must define this method.
 boolean isRegistered(String serviceName)
           
 void preShutdown()
          Stop accepting new connections, while the individual services quiesce and shut themselves down.
 void register(com.sleepycat.je.rep.utilint.ServiceDispatcher.Service service)
           
 void register(String serviceName, BlockingQueue<SocketChannel> serviceQueue)
          Registers a service queue with the ServiceDispatcher.
 void run()
          The central run method.
 void shutdown()
          Shuts down the dispatcher, so that it's no longer listening for service requests.
 SocketChannel takeChannel(String serviceName, boolean blocking, int soTimeout)
          Returns the next socketChannel created in response to a request for the service.
 
Methods inherited from class com.sleepycat.je.utilint.StoppableThread
cleanup, getSavedShutdownException, getTotalCpuTime, getTotalUserTime, isShutdown, saveShutdownException, setExceptionListener, shutdownDone, shutdownThread
 
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, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ServiceDispatcher

public ServiceDispatcher(InetSocketAddress socketAddress,
                         RepImpl repImpl)
                  throws IOException
Create a ServiceDispatcher listening on a specific socket for service requests. This service dispatcher has been created on behalf of a replicated environment, and the node will be informed of any unexpected failures seen by the dispatcher.

Parameters:
socketAddress - the socket on which it listens for service requests
Throws:
IOException - if the socket could not be bound.

ServiceDispatcher

public ServiceDispatcher(InetSocketAddress socketAddress)
                  throws IOException
Convenience overloading for when the dispatcher is created without a replicated environment, e.g. when used by the Monitor, and in unit test situations.

Throws:
IOException
See Also:
ServiceDispatcher(InetSocketAddress, RepImpl)
Method Detail

preShutdown

public void preShutdown()
Stop accepting new connections, while the individual services quiesce and shut themselves down.


shutdown

public void shutdown()
Shuts down the dispatcher, so that it's no longer listening for service requests. The port is freed up upon return and the thread used to listen on the port is shutdown.


initiateSoftShutdown

protected int initiateSoftShutdown()
Description copied from class: StoppableThread
Threads that use shutdownThread() must define this method. It's invoked by shutdwonThread as an attempt at a soft shutdown. This method makes provisions for this thread to exit on its own. The technique used to make the thread exit can vary based upon the nature of the service being provided by the thread. For example, the thread may be known to poll some shutdown flag on a periodic basis, or it may detect that a channel that it waits on has been closed by this method.

Overrides:
initiateSoftShutdown in class StoppableThread
Returns:
the amount of time in ms that the shutdownThread method will wait for the thread to exit. A -ve value means that the method will not wait. A zero value means it will wait indefinitely.

getLogger

protected Logger getLogger()
Specified by:
getLogger in class StoppableThread
Returns:
a logger to use when logging uncaught exceptions.
See Also:
StoppableThread.getLogger()

getServiceOutputStream

public static OutputStream getServiceOutputStream(Socket socket,
                                                  String serviceName)
                                           throws IOException,
                                                  ServiceDispatcher.ServiceConnectFailedException
Used by the client to establish an output stream for the service on the socket. It sends out the request for the service and interprets the response to determine if it was successful.

Parameters:
socket - the connected socket that will be the basis for the stream
serviceName - the service running on the stream
Returns:
the output stream ready for subsequent output
Throws:
IOException - if the output stream could not be established
ServiceDispatcher.ServiceConnectFailedException - if the connection could not be made.

doServiceHandshake

public static void doServiceHandshake(SocketChannel channel,
                                      String serviceName)
                               throws IOException,
                                      ServiceDispatcher.ServiceConnectFailedException
A variation on the method above. It's used by the client to setup a channel for the service. It performs the initial handshake requesting the service and interpreting the response to determine if it was successful.

Parameters:
channel - the channel that is the basis for the service
serviceName - the service running on the channel
Throws:
ServiceDispatcher.ServiceConnectFailedException - if the connection could not be made.
IOException

takeChannel

public SocketChannel takeChannel(String serviceName,
                                 boolean blocking,
                                 int soTimeout)
                          throws InterruptedException
Returns the next socketChannel created in response to a request for the service. The socketChannel and the associated socket is configured as requested in the arguments.

Parameters:
serviceName - the service for which the channel must be created.
blocking - true if the channel must be configured to block
soTimeout - the timeout for the underlying socket
Returns:
the configured channel or null if there are no more channels, because the service has been shut down.
Throws:
InterruptedException

getSocketAddress

public InetSocketAddress getSocketAddress()
Returns the socket associated with the dispatcher


register

public void register(String serviceName,
                     BlockingQueue<SocketChannel> serviceQueue)
Registers a service queue with the ServiceDispatcher. Requests for a service result in a new SocketChannel being created on which the service can communicate with the requester of the service.

Parameters:
serviceName - the name of the service being requested
serviceQueue - the queue that will be used to hold channels established for the service.

register

public void register(com.sleepycat.je.rep.utilint.ServiceDispatcher.Service service)

isRegistered

public boolean isRegistered(String serviceName)

cancel

public void cancel(String serviceName)
Cancels the registration of a service. Subsequent attempts to access the service will be ignored and the channel will be closed and will not be queued.

Parameters:
serviceName - the name of the service being cancelled

run

public void run()
The central run method. It dispatches to the "accept" and "read" event processing methods. Upon a completed read, it verifies the validity of the service name and queues the channel for subsequent consumption by the service.

Specified by:
run in interface Runnable
Overrides:
run in class Thread


Copyright (c) 2004-2010 Oracle. All rights reserved.