Package org.apache.vinci.transport
Class BaseServer
- java.lang.Object
-
- org.apache.vinci.transport.BaseServer
-
- Direct Known Subclasses:
VinciServer
public class BaseServer extends java.lang.Object
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 Modifier and Type Class Description private class
BaseServer.PooledThread
-
Field Summary
Fields Modifier and Type Field Description private int
busyCount
private BaseServer.PooledThread[]
busyThreads
private static int
DEFAULT_MAX_POOL_SIZE
private static int
DEFAULT_SOCKET_TIMEOUT
private int
initialPoolSize
private boolean
isServing
private int
maxPoolSize
private int
pooledCount
private VinciServable
servable
private static int
SERVER_SOCKET_TIMEOUT
private java.net.ServerSocket
serverSocket
private java.lang.Thread
servingThread
private boolean
shutdown
private int
socketTimeout
private BaseServer.PooledThread[]
threadPool
-
Constructor Summary
Constructors Constructor Description BaseServer(VinciServable my_servable)
Create a new BaseServer that will delegate requests to the provided servable.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
cleanExit()
Cleanly shut down this server.private void
configureServerSocket(int port)
protected java.net.ServerSocket
createServerSocket(int port)
Create the server socket used to listen for requests.Transportable
eval(Transportable in, KeyValuePair header)
This function is a callback for the (Base/Vinci)ServerRunnable.private void
expandOrWait()
Call only within a threadPool synchronization block.protected java.lang.Runnable
getRunnable(java.net.Socket client)
Get a runnable object to run within a pooled thread that will handle the request.VinciServable
getServable()
Get the servable object being used by this server.protected java.net.ServerSocket
getServerSocket()
Get the server socket that this server uses to listen for requests.private BaseServer.PooledThread
getThreadFromPool()
Get an available thread from the thread pool.protected void
handleRequest(java.net.Socket client)
Initialize a new socket connection.protected void
handleRequests()
Enter the server socket accept() loop.private void
initializeServing()
Transportable
makeTransportable()
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
shutdownServing()
Cause the serve() method to terminate.void
startServing(int port)
Asynchronously start serving requests.
-
-
-
Field Detail
-
DEFAULT_SOCKET_TIMEOUT
private static final int DEFAULT_SOCKET_TIMEOUT
- See Also:
- Constant Field Values
-
DEFAULT_MAX_POOL_SIZE
private static final int DEFAULT_MAX_POOL_SIZE
- See Also:
- Constant Field Values
-
SERVER_SOCKET_TIMEOUT
private static final int SERVER_SOCKET_TIMEOUT
- See Also:
- Constant Field Values
-
shutdown
private volatile boolean shutdown
-
isServing
private volatile boolean isServing
-
serverSocket
private java.net.ServerSocket serverSocket
-
servable
private VinciServable servable
-
servingThread
private java.lang.Thread servingThread
-
socketTimeout
private int socketTimeout
-
initialPoolSize
private int initialPoolSize
-
maxPoolSize
private int maxPoolSize
-
pooledCount
private int pooledCount
-
busyCount
private int busyCount
-
threadPool
private BaseServer.PooledThread[] threadPool
-
busyThreads
private BaseServer.PooledThread[] busyThreads
-
-
Constructor Detail
-
BaseServer
public BaseServer(VinciServable my_servable)
Create a new BaseServer that will delegate requests to the provided servable.- Parameters:
my_servable
- The servable object implementing the service.
-
-
Method Detail
-
getServable
public VinciServable getServable()
Get the servable object being used by this server.- Returns:
- -
-
setSocketTimeout
public void setSocketTimeout(int millis) throws java.io.IOException
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:
java.io.IOException
- -
-
expandOrWait
private void expandOrWait() throws java.lang.InterruptedException
Call only within a threadPool synchronization block.- Throws:
java.lang.InterruptedException
-
getThreadFromPool
private BaseServer.PooledThread getThreadFromPool() throws java.lang.InterruptedException
Get an available thread from the thread pool. Block if no thread is available and the maximum pool size has been reached.- Throws:
java.lang.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.- Parameters:
initial
- -max
- -
-
configureServerSocket
private void configureServerSocket(int port) throws java.io.IOException
- Throws:
java.io.IOException
-
initializeServing
private void initializeServing()
-
startServing
public void startServing(int port) throws java.io.IOException
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:
java.io.IOException
- Thrown if there was some problem with the server socket.- Since:
- 2.0.15
-
serve
public void serve(int port) throws java.io.IOException
Serve requests, blocking until a clean shutdown is triggered.- Parameters:
port
- The port on which to listen for requests.- Throws:
java.io.IOException
- Thrown if there was some problem with the server socket.
-
getServerSocket
protected java.net.ServerSocket getServerSocket()
Get the server socket that this server uses to listen for requests.- Returns:
- The server's server socket
-
createServerSocket
protected java.net.ServerSocket createServerSocket(int port) throws java.io.IOException
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:
java.io.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
protected java.lang.Runnable getRunnable(java.net.Socket client)
Get a runnable object to run within a pooled thread that will handle the request.- Parameters:
client
- -- Returns:
- -
-
handleRequest
protected void handleRequest(java.net.Socket client)
Initialize a new socket connection.- Parameters:
client
- -
-
cleanExit
protected void cleanExit()
Cleanly shut down this server. Called when handleRequests completes the accept loop.
-
eval
public Transportable eval(Transportable in, KeyValuePair header)
This function is a callback for the (Base/Vinci)ServerRunnable. It invokes the eval method of the appropriate VinciServable.- Parameters:
in
- -header
- -- Returns:
- -
-
makeTransportable
public Transportable 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.
-
-