Java Multi-threaded Dictionary Server
Tags: showdev, java, javafx, distributedsystems
Published: 2021-09-07 17:38:14

The system follows a client-server architecture. The server uses a thread pool to manage incoming connections. Every incoming request is passed to the thread pool for execution when a thread in the pool becomes idle.

The main reason for choosing the thread pool server architecture over the a simple multi-threaded server is to control the machine resources more effectively. By controlling the maximum number of threads we can minimize resource depletion. 

Using thread pool, we can queue requests and process concurrently only a limited amount. This will also benefit the client, as concurrently executing many requests will slow down all requests processed.

### Server

The major jobs of the server is to open a socket connection with the client, take the client request, process the request and send response with the appropriate data. The design decisions on the server is to make it as loosely coupled as possible to allow for new implementations to be integrated without much refactoring.

**Communication handling**

The Server opens up a port to accept connections. When a connection is established a new SocketHandler thread is created and added to the thread pool. The SocketHandler handles requests form client and sends response back and closes the connection.

**Database**

Persistent data operations for the server are provided by the DictionaryRepository interface.

**Message exchange**

The MessageExchange interface provides the means to implement a messaging protocol between the client and server.

**Request processing**

By implementing the DictionaryService with an implementation of the DictionaryRepository, a request processing scheme can be built for the server. 

Also, implementing the MethodResolver utility interface using a DictionaryService implementation, will result in a much easier request handling. Every response returned must provide a code from the `Codes` enumeration to enhance communication between the server and client.

![server class diagram](https://raw.githubusercontent.com/eyuelberga/Multi-threaded-Dictionary-Server/main/images/server-class.png)

### Client

The client sends request to server and display the response to the user. Same as the server, the design aims to create a loosely coupled code for further extensions.

**Communication handling**

For every request sent to the server, the SocketHandler class is responsible for creating a socket connection, sending the request and after response has been received close the
connection.

**Message exchange**

The MessageExchange interface provides the means to implement a messaging protocol between the client and server. Same implementations must be used to avoid parsing errors.

**Response processing**

The ClientUtil class provides a function to resolve response received form the server, using the code sent with the response.

![client class diagram](https://raw.githubusercontent.com/eyuelberga/Multi-threaded-Dictionary-Server/main/images/client-class.png)


The full source-code for the project is on GitHub:

{% github eyuelberga/Multi-threaded-Dictionary-Server %}