|
| 1 | +package io.evanwong.hipchat.v2; |
| 2 | + |
| 3 | +import io.evanwong.hipchat.v2.rooms.GetAllRoomsRequestBuilder; |
| 4 | +import io.evanwong.hipchat.v2.rooms.SendRoomNotificationRequestBuilder; |
| 5 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 6 | +import org.apache.http.impl.client.HttpClients; |
| 7 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.Executors; |
| 14 | + |
| 15 | +public class HipChatClient { |
| 16 | + |
| 17 | + private static final Logger log = LoggerFactory.getLogger(HipChatClient.class); |
| 18 | + |
| 19 | + private CloseableHttpClient httpClient; |
| 20 | + private ExecutorService executorService; |
| 21 | + private String defaultAccessToken; |
| 22 | + //TODO move this out |
| 23 | + private int maxConnections = 20; |
| 24 | + //TODO move this out |
| 25 | + private int maxConnectionsPerRoute = 4; |
| 26 | + |
| 27 | + |
| 28 | + public HipChatClient() { |
| 29 | + init(); |
| 30 | + } |
| 31 | + |
| 32 | + public HipChatClient(String defaultAccessToken) { |
| 33 | + this.defaultAccessToken = defaultAccessToken; |
| 34 | + init(); |
| 35 | + } |
| 36 | + |
| 37 | + private void init() { |
| 38 | + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); |
| 39 | + cm.setMaxTotal(maxConnections); |
| 40 | + log.debug("Max pool size: {}", maxConnections); |
| 41 | + cm.setDefaultMaxPerRoute(maxConnectionsPerRoute); |
| 42 | + log.debug("Max per route: {}", maxConnectionsPerRoute); |
| 43 | + |
| 44 | + httpClient = HttpClients.custom().setConnectionManager(cm).build(); |
| 45 | + //setting the thread pool size equal to the max connections size |
| 46 | + executorService = Executors.newFixedThreadPool(maxConnections); |
| 47 | + } |
| 48 | + |
| 49 | + public void setDefaultAccessToken(String defaultAccessToken) { |
| 50 | + this.defaultAccessToken = defaultAccessToken; |
| 51 | + } |
| 52 | + |
| 53 | + public GetAllRoomsRequestBuilder prepareGetAllRoomsRequestBuilder(String accessToken) { |
| 54 | + return new GetAllRoomsRequestBuilder(accessToken, httpClient, executorService); |
| 55 | + } |
| 56 | + |
| 57 | + public GetAllRoomsRequestBuilder prepareGetAllRoomsRequestBuilder() { |
| 58 | + return prepareGetAllRoomsRequestBuilder(defaultAccessToken); |
| 59 | + } |
| 60 | + |
| 61 | + public SendRoomNotificationRequestBuilder prepareSendRoomNotificationRequestBuilder(String idOrName, String message, String accessToken) { |
| 62 | + return new SendRoomNotificationRequestBuilder(idOrName, message, accessToken, httpClient, executorService); |
| 63 | + } |
| 64 | + |
| 65 | + public SendRoomNotificationRequestBuilder prepareSendRoomNotificationRequestBuilder(String idOrName, String message) { |
| 66 | + return prepareSendRoomNotificationRequestBuilder(idOrName, message, defaultAccessToken); |
| 67 | + } |
| 68 | + |
| 69 | + public void close() { |
| 70 | + log.info("Shutting down..."); |
| 71 | + try { |
| 72 | + httpClient.close(); |
| 73 | + } catch (IOException e) { |
| 74 | + log.error("Failed to close the HttpClient.", e); |
| 75 | + } |
| 76 | + executorService.shutdown(); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments