JavaBlog.fr / Java.lu DEVELOPMENT,Java Java: A concrete example of Queue’s utilization: Multi Servers Dispatching (Part8)

Java: A concrete example of Queue’s utilization: Multi Servers Dispatching (Part8)

After my posts concerning presentation of Queue/BlockingQueue, and the Queue/BlockingQueue implementations I would continue with a post about a concrete example of these implementations: the Multi servers dispatching.

Multi-server operating system:
This system provides a pool of available connections due to:

  • ConfigParametersAndUtils: Configuration’s parameters and useful methods
  • MyHostConnectionProxy: Encapsulates the real connection. This class manage the connection transaction timeout too, if a connection duration exceed the defined transactionTimeOut, it will be automatically closed.
  • MyHostConnectionProxyFactory: The main goals of MyHostConnectionProxyFactory class is to provide and close the connection via its connection pools:
    – Unused and available connections (connectionPool) ;
    – Used connections (usedconnectionPool) ;
    – Unavailable connections (unavailableConnectionPool);
    -> Open Connection: execute the MyHostConnectionProxyFactory.openConnection method;
    -> Open Connection with a same server: execute the MyHostConnectionProxyFactory.openConnectionWithSameServer method;
    -> Close a connection: execute the MyHostConnectionProxyFactory.closeConnection method;
  • MyHostConnectionProxyAutoCloseConnection: It is a runnable is attached to each used connection in order to detect a timeout (MyHostConnectionProxy.transactionTimeOut) and close the connection (use of ScheduledExecutorService),
  • MyHostConnectionMock: Mock connection encapsulated by the instance of MyHostConnectionProxy in the tests

Test n°1:
The test case Launch5ThreadsTest checks the working of multi server dispatching with a pool of 2 connections available and 5 runnable clients.

Initializing the connection pools:

main  --------------- Initializing the connection pools... --------------- 
main Following connection added : 		host:port=server0/8200 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false
main Following connection added : 		host:port=server1/8201 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false
main  --------------- Connection pools initialized... --------------- 

Here, the traces concerning the connections established for each client thread and the mock remote server:

Thread1  --------> Connection established with : 		host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true
...
Thread5  --------> Connection established with : 		host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true
...
Thread5  --------> Connection established with : 		host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true
Thread5  --------> OpenConnectionWithSameServer : following connection opened : 		host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true
...

the traces concerning the connections closure:

Thread1  --------> CloseConnection : connection released connectionToRelease : 		host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true )
...
Thread5  --------> CloseConnection : releasing connection connectionToRelease : 		host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true )
...
Thread4  --------> CloseConnection : connection released connectionToRelease : 		host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true )
...
Thread2  --------> CloseConnection : connection released connectionToRelease : 		host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true )
...
Thread3  --------> CloseConnection : releasing connection connectionToRelease : 		host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true )
...

Test n°2:
The test case Launch3ThreadsTimeOutWithAutoCloseTest checks the working of the AutoClose runnable used to detect a timeout and close the connection with a pool of 2 connections available and 3 runnable clients. This runnable is attached to each connection.

Initializing the connection pools:

main  --------------- Initializing the connection pools... --------------- 
main Following connection added : 		host:port=server0/8200 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false
main Following connection added : 		host:port=server1/8201 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false
main  --------------- Connection pools initialized... --------------- 

To enforce the timeout, we have changed the connection time out to 1 seconds. So, if an connection is open more than this time. A thread will close it automatically. Here, the traces concerning the MyHostConnectionProxyAutoCloseConnection runnable for each connection:

ThreadAutoCloseConnection - server0:8200  --------> Start of the run() function : trying to automatically close the connection having a transaction time out.
ThreadAutoCloseConnection - server0:8200  --------> CloseConnection : releasing connection connectionToRelease : 		host:port=server0/8200 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true )
ThreadAutoCloseConnection - server0:8200  --------> Start of the cancel() function : trying to unschedule the runnable that automatically close the connection if a transaction time out occurs.
ThreadAutoCloseConnection - server0:8200  --------> End of the cancel() function.
ThreadAutoCloseConnection - server0:8200  --------> MyHostConnectionMock.close() on 'server0/8200' and going to sleep for 2170 milliseconds.
...
ThreadAutoCloseConnection - server1:8201  --------> Start of the run() function : trying to automatically close the connection having a transaction time out.
ThreadAutoCloseConnection - server1:8201  --------> CloseConnection : releasing connection connectionToRelease : 		host:port=server1/8201 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true )
ThreadAutoCloseConnection - server1:8201  --------> Start of the cancel() function : trying to unschedule the runnable that automatically close the connection if a transaction time out occurs.
ThreadAutoCloseConnection - server1:8201  --------> End of the cancel() function.
ThreadAutoCloseConnection - server1:8201  --------> MyHostConnectionMock.close() on 'server1/8201' and going to sleep for 1992 milliseconds.
...
ThreadAutoCloseConnection - server0:8200  --------> CloseConnection : connection released connectionToRelease : 		host:port=server0/8200 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true )
ThreadAutoCloseConnection - server0:8200  --------> End of the run() function.
ThreadAutoCloseConnection - server1:8201  --------> CloseConnection : connection released connectionToRelease : 		host:port=server1/8201 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true )
ThreadAutoCloseConnection - server1:8201  --------> End of the run() function.
...

Here, a concrete example of the Queues implementations, however, contact me for more explanations.

The source code of this article and the a test cases are in the ZIP file attachement.

Download: java_queue.zip

Best regards,

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post