|
1 | 1 | package org.baeldung.httpclient.conn; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | | -import java.util.logging.Level; |
5 | | -import java.util.logging.Logger; |
6 | 4 |
|
7 | 5 | import org.apache.http.HttpResponse; |
8 | 6 | import org.apache.http.client.ClientProtocolException; |
9 | 7 | import org.apache.http.client.methods.HttpGet; |
10 | 8 | import org.apache.http.impl.client.CloseableHttpClient; |
11 | 9 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
12 | 10 | import org.apache.http.util.EntityUtils; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
13 | 13 |
|
14 | 14 | public class MultiHttpClientConnThread extends Thread { |
| 15 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 16 | + |
15 | 17 | private final CloseableHttpClient client; |
16 | 18 | private final HttpGet get; |
17 | | - private PoolingHttpClientConnectionManager connManager = null; |
| 19 | + |
| 20 | + private PoolingHttpClientConnectionManager connManager; |
18 | 21 | private static HttpResponse response; |
19 | | - private Logger logger; |
20 | 22 | public int leasedConn; |
21 | 23 |
|
22 | 24 | public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) { |
23 | 25 | this.client = client; |
24 | 26 | this.get = get; |
25 | 27 | this.connManager = connManager; |
26 | | - logger = Logger.getLogger(MultiHttpClientConnThread.class.getName()); |
27 | | - leasedConn = 0; |
| 28 | + this.leasedConn = 0; |
28 | 29 | } |
29 | 30 |
|
30 | 31 | public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get) { |
31 | 32 | this.client = client; |
32 | 33 | this.get = get; |
33 | | - logger = Logger.getLogger(MultiHttpClientConnThread.class.getName()); |
34 | 34 | } |
35 | 35 |
|
36 | | - public int getLeasedConn() { |
| 36 | + // API |
| 37 | + |
| 38 | + public final int getLeasedConn() { |
37 | 39 | return leasedConn; |
38 | 40 | } |
39 | 41 |
|
40 | | - @Override |
41 | | - public void run() { |
| 42 | + // |
42 | 43 |
|
| 44 | + @Override |
| 45 | + public final void run() { |
43 | 46 | try { |
44 | | - if (this != null) |
45 | | - logger.log(Level.SEVERE, "Thread Running: " + getName()); |
| 47 | + logger.info("Thread Running: " + getName()); |
| 48 | + |
46 | 49 | response = client.execute(get); |
47 | 50 | if (connManager != null) { |
48 | | - logger.log(Level.SEVERE, "Leased Connections " + connManager.getTotalStats().getLeased()); |
| 51 | + logger.info("Leased Connections " + connManager.getTotalStats().getLeased()); |
49 | 52 | leasedConn = connManager.getTotalStats().getLeased(); |
50 | | - logger.log(Level.SEVERE, "Available Connections " + connManager.getTotalStats().getAvailable()); |
| 53 | + logger.info("Available Connections " + connManager.getTotalStats().getAvailable()); |
51 | 54 | } |
52 | 55 | EntityUtils.consume(response.getEntity()); |
53 | | - |
54 | 56 | } catch (final ClientProtocolException ex) { |
55 | | - |
| 57 | + logger.error("", ex); |
56 | 58 | } catch (final IOException ex) { |
57 | | - |
| 59 | + logger.error("", ex); |
58 | 60 | } |
59 | | - |
60 | 61 | } |
| 62 | + |
61 | 63 | } |
0 commit comments