|
31 | 31 |
|
32 | 32 | /** |
33 | 33 | * @author Mark Vollmary |
34 | | - * |
35 | 34 | */ |
36 | 35 | public class ConnectionPoolImpl implements ConnectionPool { |
37 | | - |
38 | | - private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionPoolImpl.class); |
39 | | - |
40 | | - private final HostDescription host; |
41 | | - private final int maxConnections; |
42 | | - private final List<Connection> connections; |
43 | | - private int current; |
44 | | - private final ConnectionFactory factory; |
45 | | - |
46 | | - public ConnectionPoolImpl(final HostDescription host, final Integer maxConnections, |
47 | | - final ConnectionFactory factory) { |
48 | | - super(); |
49 | | - this.host = host; |
50 | | - this.maxConnections = maxConnections; |
51 | | - this.factory = factory; |
| 36 | + |
| 37 | + private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionPoolImpl.class); |
| 38 | + |
| 39 | + private final HostDescription host; |
| 40 | + private final int maxConnections; |
| 41 | + private final List<Connection> connections; |
| 42 | + private int current; |
| 43 | + private final ConnectionFactory factory; |
| 44 | + |
| 45 | + public ConnectionPoolImpl(final HostDescription host, final Integer maxConnections, |
| 46 | + final ConnectionFactory factory) { |
| 47 | + super(); |
| 48 | + this.host = host; |
| 49 | + this.maxConnections = maxConnections; |
| 50 | + this.factory = factory; |
52 | 51 | connections = new ArrayList<>(); |
53 | 52 | current = 0; |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - public Connection createConnection(final HostDescription host) { |
58 | | - return factory.create(host); |
59 | | - } |
60 | | - |
61 | | - @Override |
62 | | - public synchronized Connection connection() { |
63 | | - |
64 | | - final Connection connection; |
65 | | - |
66 | | - if (connections.size() < maxConnections) { |
67 | | - connection = createConnection(host); |
68 | | - connections.add(connection); |
69 | | - current++; |
70 | | - } else { |
71 | | - final int index = (current++) % connections.size(); |
72 | | - connection = connections.get(index); |
73 | | - } |
74 | | - |
75 | | - if(connection instanceof VstConnectionSync) { |
76 | | - LOGGER.debug("Return Connection " + ((VstConnection)connection).getConnectionName()); |
77 | | - } |
78 | | - |
79 | | - return connection; |
80 | | - } |
81 | | - |
82 | | - @Override |
83 | | - public void close() throws IOException { |
84 | | - for (final Connection connection : connections) { |
85 | | - connection.close(); |
86 | | - } |
87 | | - connections.clear(); |
88 | | - } |
89 | | - |
90 | | - @Override |
91 | | - public String toString() { |
92 | | - return "ConnectionPoolImpl [host=" + host + ", maxConnections=" + maxConnections + ", connections=" |
93 | | - + connections.size() + ", current=" + current + ", factory=" + factory.getClass().getSimpleName() + "]"; |
94 | | - } |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Connection createConnection(final HostDescription host) { |
| 57 | + return factory.create(host); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public synchronized Connection connection() { |
| 62 | + |
| 63 | + final Connection connection; |
| 64 | + |
| 65 | + if (connections.size() < maxConnections) { |
| 66 | + connection = createConnection(host); |
| 67 | + connections.add(connection); |
| 68 | + current++; |
| 69 | + } else { |
| 70 | + final int index = Math.floorMod(current++, connections.size()); |
| 71 | + connection = connections.get(index); |
| 72 | + } |
| 73 | + |
| 74 | + if (connection instanceof VstConnectionSync) { |
| 75 | + LOGGER.debug("Return Connection " + ((VstConnection) connection).getConnectionName()); |
| 76 | + } |
| 77 | + |
| 78 | + return connection; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void close() throws IOException { |
| 83 | + for (final Connection connection : connections) { |
| 84 | + connection.close(); |
| 85 | + } |
| 86 | + connections.clear(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String toString() { |
| 91 | + return "ConnectionPoolImpl [host=" + host + ", maxConnections=" + maxConnections + ", connections=" |
| 92 | + + connections.size() + ", current=" + current + ", factory=" + factory.getClass().getSimpleName() + "]"; |
| 93 | + } |
95 | 94 |
|
96 | 95 | } |
0 commit comments