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...
-
Constructor Summary
ConstructorDescriptionBaseServer
(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.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.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.protected void
handleRequest
(Socket client) Initialize a new socket connection.protected void
Enter the server socket accept() loop.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.
-
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.- Returns:
- -
-
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
- -
-
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
- -
-
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.- Parameters:
client
- -- Returns:
- -
-
handleRequest
Initialize a new socket connection.- Parameters:
client
- -
-
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.- Parameters:
in
- -header
- -- Returns:
- -
-
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.
-