com.sleepycat.je.rep.utilint
Class ServiceDispatcher
java.lang.Object
java.lang.Thread
com.sleepycat.je.utilint.StoppableThread
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.
Methods inherited from class java.lang.Thread |
activeCount, checkAccess, 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 |
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)
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 streamserviceName
- 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 serviceserviceName
- 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 blocksoTimeout
- 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 requestedserviceQueue
- 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.