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:
1 | main --------------- Initializing the connection pools... --------------- |
2 | main Following connection added : host:port=server0/8200 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false |
3 | main Following connection added : host:port=server1/8201 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false |
4 | main --------------- Connection pools initialized... --------------- |
Here, the traces concerning the connections established for each client thread and the mock remote server:
1 | Thread1 --------> Connection established with : host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true |
3 | Thread5 --------> Connection established with : host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true |
5 | Thread5 --------> Connection established with : host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true |
6 | Thread5 --------> OpenConnectionWithSameServer : following connection opened : host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true |
the traces concerning the connections closure:
01 | Thread1 --------> CloseConnection : connection released connectionToRelease : host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true ) |
03 | Thread5 --------> CloseConnection : releasing connection connectionToRelease : host:port=server1/8201 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true ) |
05 | Thread4 --------> CloseConnection : connection released connectionToRelease : host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true ) |
07 | Thread2 --------> CloseConnection : connection released connectionToRelease : host:port=server0/8200 - transactionTimeOut=300 - autoCloseConnectionRunnable is done=true ) |
09 | 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:
1 | main --------------- Initializing the connection pools... --------------- |
2 | main Following connection added : host:port=server0/8200 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false |
3 | main Following connection added : host:port=server1/8201 - transactionTimeOut=null - autoCloseConnectionRunnable is done=false |
4 | 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:
01 | ThreadAutoCloseConnection - server0:8200 --------> Start of the run() function : trying to automatically close the connection having a transaction time out. |
02 | ThreadAutoCloseConnection - server0:8200 --------> CloseConnection : releasing connection connectionToRelease : host:port=server0/8200 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true ) |
03 | ThreadAutoCloseConnection - server0:8200 --------> Start of the cancel() function : trying to unschedule the runnable that automatically close the connection if a transaction time out occurs. |
04 | ThreadAutoCloseConnection - server0:8200 --------> End of the cancel() function. |
05 | ThreadAutoCloseConnection - server0:8200 --------> MyHostConnectionMock.close() on 'server0/8200' and going to sleep for 2170 milliseconds. |
07 | ThreadAutoCloseConnection - server1:8201 --------> Start of the run() function : trying to automatically close the connection having a transaction time out. |
08 | ThreadAutoCloseConnection - server1:8201 --------> CloseConnection : releasing connection connectionToRelease : host:port=server1/8201 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true ) |
09 | ThreadAutoCloseConnection - server1:8201 --------> Start of the cancel() function : trying to unschedule the runnable that automatically close the connection if a transaction time out occurs. |
10 | ThreadAutoCloseConnection - server1:8201 --------> End of the cancel() function. |
11 | ThreadAutoCloseConnection - server1:8201 --------> MyHostConnectionMock.close() on 'server1/8201' and going to sleep for 1992 milliseconds. |
13 | ThreadAutoCloseConnection - server0:8200 --------> CloseConnection : connection released connectionToRelease : host:port=server0/8200 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true ) |
14 | ThreadAutoCloseConnection - server0:8200 --------> End of the run() function. |
15 | ThreadAutoCloseConnection - server1:8201 --------> CloseConnection : connection released connectionToRelease : host:port=server1/8201 - transactionTimeOut=1 - autoCloseConnectionRunnable is done=true ) |
16 | 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,
Related