Package org.apache.vinci.transport
Class BaseServer
java.lang.Object
org.apache.vinci.transport.BaseServer
- Direct Known Subclasses:
VinciServer
Class for running VinciServables in the Vinci framework. This implementation supports multiple
concurrent clients (in a Thread per client manner). Creating a service typically requires
defining implementing a VinciServable which is passed to this class to service requests.
This class can be used independently of VNS. For VNS-enhanced serving, use the VinciServer.
This class is designed to be extensible. For example you can extend to provide new Runnable
objects that are used to handle requests in the appropriate fashion. You can also override
configure socket to install SSL-supporting server sockets, and so on...
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private BaseServer.PooledThread[]
private static final int
private static final int
private int
private boolean
private int
private int
private VinciServable
private static final int
private ServerSocket
private Thread
private boolean
private int
private BaseServer.PooledThread[]
-
Constructor Summary
ConstructorsConstructorDescriptionBaseServer
(VinciServable my_servable) Create a new BaseServer that will delegate requests to the provided servable. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Cleanly shut down this server.private void
configureServerSocket
(int port) protected ServerSocket
createServerSocket
(int port) Create the server socket used to listen for requests.eval
(Transportable in, KeyValuePair header) This function is a callback for the (Base/Vinci)ServerRunnable.private void
Call only within a threadPool synchronization block.protected Runnable
getRunnable
(Socket client) Get a runnable object to run within a pooled thread that will handle the request.Get the servable object being used by this server.protected ServerSocket
Get the server socket that this server uses to listen for requests.private BaseServer.PooledThread
Get an available thread from the thread pool.protected void
handleRequest
(Socket client) Initialize a new socket connection.protected void
Enter the server socket accept() loop.private void
This is another callback for the (Base/Vinci)ServerRunnable that creates the document to be populated by the service with the request response.void
serve
(int port) Serve requests, blocking until a clean shutdown is triggered.void
setSocketTimeout
(int millis) This method is used to override the default timeout value of one minute.void
setThreadPoolSize
(int initial, int max) Set the intitial and maximum size of the threadpool used by this server.void
Cause the serve() method to terminate.void
startServing
(int port) Asynchronously start serving requests.
-
Field Details
-
DEFAULT_SOCKET_TIMEOUT
private static final int DEFAULT_SOCKET_TIMEOUT- See Also:
-
DEFAULT_MAX_POOL_SIZE
private static final int DEFAULT_MAX_POOL_SIZE- See Also:
-
SERVER_SOCKET_TIMEOUT
private static final int SERVER_SOCKET_TIMEOUT- See Also:
-
shutdown
private volatile boolean shutdown -
isServing
private volatile boolean isServing -
serverSocket
-
servable
-
servingThread
-
socketTimeout
private int socketTimeout -
initialPoolSize
private int initialPoolSize -
maxPoolSize
private int maxPoolSize -
pooledCount
private int pooledCount -
busyCount
private int busyCount -
threadPool
-
busyThreads
-
-
Constructor Details
-
BaseServer
Create a new BaseServer that will delegate requests to the provided servable.- Parameters:
my_servable
- The servable object implementing the service.
-
-
Method Details
-
getServable
Get the servable object being used by this server. -
setSocketTimeout
This method is used to override the default timeout value of one minute. You can provide "0" for "never timeout" but this is not recommended, as in some cases threads will end up forever blocking for input, eventually causing the threadpool to max out and block all pending requests.- Parameters:
millis
- The socket timeout value in milliseconds.- Throws:
IOException
-
expandOrWait
Call only within a threadPool synchronization block.- Throws:
InterruptedException
-
getThreadFromPool
Get an available thread from the thread pool. Block if no thread is available and the maximum pool size has been reached.- Throws:
InterruptedException
-
setThreadPoolSize
public void setThreadPoolSize(int initial, int max) Set the intitial and maximum size of the threadpool used by this server. This should be called before serving starts otherwise it has no effect. -
configureServerSocket
- Throws:
IOException
-
initializeServing
private void initializeServing() -
startServing
Asynchronously start serving requests. If this method returns without throwing an exception, then another thread has been successfully launched to begin serving requests.- Parameters:
port
- The port on which to listen for requests.- Throws:
IOException
- Thrown if there was some problem with the server socket.- Since:
- 2.0.15
-
serve
Serve requests, blocking until a clean shutdown is triggered.- Parameters:
port
- The port on which to listen for requests.- Throws:
IOException
- Thrown if there was some problem with the server socket.
-
getServerSocket
Get the server socket that this server uses to listen for requests.- Returns:
- The server's server socket
-
createServerSocket
Create the server socket used to listen for requests. This method can be overridden to provide non-standard server sockets, such as those supporting SSL, those requiring special configuration/initialization, etc.- Parameters:
port
- The port which is to be listened to by the created socket.- Returns:
- The server socket to be used by this server for accepting requests.
- Throws:
IOException
-
handleRequests
protected void handleRequests()Enter the server socket accept() loop. Loop terminates when shutdown shutdownServing() method is called. -
shutdownServing
public void shutdownServing()Cause the serve() method to terminate. -
getRunnable
Get a runnable object to run within a pooled thread that will handle the request. -
handleRequest
Initialize a new socket connection. -
cleanExit
protected void cleanExit()Cleanly shut down this server. Called when handleRequests completes the accept loop. -
eval
This function is a callback for the (Base/Vinci)ServerRunnable. It invokes the eval method of the appropriate VinciServable. -
makeTransportable
This is another callback for the (Base/Vinci)ServerRunnable that creates the document to be populated by the service with the request response.- Returns:
- The document the service will populate to create the request response.
-