- Timestamp:
- 10/11/18 11:42:21 (6 years ago)
- Location:
- SRUClient/trunk/src/main/java/eu/clarin/sru/client
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClientConfig.java
r6938 r7191 21 21 import java.util.List; 22 22 23 import org.apache.http.client.protocol.HttpClientContext; 24 import org.apache.http.impl.client.CloseableHttpClient; 25 23 26 /** 24 27 * A class for encapsulating the configuration of an SRU client. … … 34 37 private final int connectTimeout; 35 38 private final int socketTimeout; 39 private final CloseableHttpClient httpClient; 40 private final HttpClientContext httpContext; 36 41 private final int threadCount; 37 42 private final List<SRURecordDataParser> recordParsers; … … 50 55 51 56 /** 52 * Get the connect timeout. 57 * Get the connect timeout. This value is ignored if a customized HTTP 58 * client is provided. 53 59 * 54 60 * @return the connect timeout … … 60 66 61 67 /** 62 * Get the socket timeout. 68 * Get the socket timeout. This value is ignored if a customized HTTP client 69 * is provided. 63 70 * 64 71 * @return the connect timeout … … 66 73 public int getSocketTimeout() { 67 74 return socketTimeout; 75 } 76 77 78 /** 79 * Get the customized HTTP client which is to be used. 80 * 81 * @return a configured HTTP client instance or <code>null</code> 82 */ 83 public CloseableHttpClient getCustomizedHttpClient() { 84 return httpClient; 85 } 86 87 88 /** 89 * Get the HTTP client context which is to be used. Only relevant, if a 90 * customized HTTP client is set, see {{@link #getCustomizedHttpClient()}. 91 * 92 * @return a HTTP client context instance or <code>null</code> 93 */ 94 public HttpClientContext getHttpClientContext() { 95 return httpContext; 68 96 } 69 97 … … 104 132 throw new NullPointerException("builder == null"); 105 133 } 106 this.defaultVersion = builder.getDefaultVersion(); 107 this.connectTimeout = builder.getConnectTimeout(); 108 this.socketTimeout = builder.getSocketTimeout(); 109 this.threadCount = builder.getThreadCount(); 110 this.recordParsers = builder.getRecordDataParsers(); 111 this.extraDataParsers = builder.getExtraResponseDataParsers(); 134 this.defaultVersion = builder.defaultVersion; 135 this.connectTimeout = builder.connectTimeout; 136 this.socketTimeout = builder.socketTimeout; 137 if (builder.httpClient != null) { 138 this.httpClient = builder.httpClient; 139 this.httpContext = builder.httpContext; 140 } else { 141 this.httpClient = null; 142 this.httpContext = null; 143 } 144 this.threadCount = builder.threadCount; 145 if (builder.recordParsers != null) { 146 this.recordParsers = 147 Collections.unmodifiableList(builder.recordParsers); 148 } else { 149 this.recordParsers = null; 150 } 151 if (builder.extraDataParsers != null) { 152 this.extraDataParsers = 153 Collections.unmodifiableList(builder.extraDataParsers); 154 } else { 155 this.extraDataParsers = null; 156 } 157 } 158 159 160 /** 161 * Get a new builder for creating a new {@link SRUClientConfig} instance. 162 * 163 * @return a builder instance 164 */ 165 public static Builder builder() { 166 return new Builder(); 112 167 } 113 168 … … 121 176 public static class Builder { 122 177 private SRUVersion defaultVersion = DEFAULT_SRU_VERSION; 123 private int connectTimeout = DEFAULT_CONNECT_TIMEOUT; 124 private int socketTimeout = DEFAULT_SOCKET_TIMEOUT; 125 private int threadCount = 178 private int connectTimeout = DEFAULT_CONNECT_TIMEOUT; 179 private int socketTimeout = DEFAULT_SOCKET_TIMEOUT; 180 private CloseableHttpClient httpClient = null; 181 private HttpClientContext httpContext = null; 182 private int threadCount = 126 183 Runtime.getRuntime().availableProcessors() * 2; 127 184 private List<SRURecordDataParser> recordParsers = … … 139 196 140 197 /** 141 * Get the default SRU version to be used142 *143 * @return the defaultSRU version to be used144 */145 public SRUVersion getDefaultVersion() {146 return defaultVersion;147 }148 149 150 /**151 198 * Set the default SRU version to be used. 152 199 * … … 163 210 this.defaultVersion = defaultVersion; 164 211 return this; 165 }166 167 168 /**169 * Get the connect timeout.170 *171 * @return the connect timeout.172 */173 public int getConnectTimeout() {174 return connectTimeout;175 212 } 176 213 … … 193 230 this.connectTimeout = connectTimeout; 194 231 return this; 195 }196 197 198 /**199 * Get the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,200 * which is the timeout for waiting for data.201 *202 * @return the socket timeout in milliseconds203 */204 public int getSocketTimeout() {205 return socketTimeout;206 232 } 207 233 … … 229 255 230 256 /** 231 * Get the number of worker threads. This value is only relevant for the 232 * {@link SRUThreadedClient}. 233 * 234 * @return the number of worker threads 235 */ 236 public int getThreadCount() { 237 return threadCount; 257 * Set a customized HTTP client which is to be used. 258 * 259 * @param httpClient 260 * a configured HTTP client instance 261 * @return this {@link Builder} instance 262 */ 263 public Builder setCustomizedHttpClient(CloseableHttpClient httpClient) { 264 this.httpClient = httpClient; 265 return this; 266 } 267 268 269 /** 270 * Optionally set the HTTP context which is to be used by the customized 271 * HTTP client. 272 * 273 * @param httpContext 274 * a HTTP context instance 275 * @return this {@link Builder} instance 276 */ 277 public Builder setHttpContext(HttpClientContext httpContext) { 278 this.httpContext = httpContext; 279 return this; 238 280 } 239 281 … … 253 295 this.threadCount = threadCount; 254 296 return this; 255 }256 257 258 /**259 * Get the list of record data parsers.260 *261 * @return the list of record data parsers262 * @see SRURecordDataParser263 */264 public List<SRURecordDataParser> getRecordDataParsers() {265 return Collections.unmodifiableList(recordParsers);266 }267 268 269 /**270 * Get the list of extra response data parsers.271 *272 * @return the list of extra response data parsers273 * @see SRUExtraResponseDataParser274 */275 public List<SRUExtraResponseDataParser> getExtraResponseDataParsers() {276 if (extraDataParsers != null) {277 return Collections.unmodifiableList(extraDataParsers);278 } else {279 return null;280 }281 297 } 282 298 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java
r7188 r7191 44 44 import org.apache.http.impl.client.HttpClients; 45 45 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 46 import org.apache.http.protocol.HttpContext; 46 47 import org.slf4j.Logger; 47 48 import org.slf4j.LoggerFactory; … … 83 84 private final Map<String, SRURecordDataParser> parsers; 84 85 private final CloseableHttpClient httpClient; 86 private final HttpContext httpContext; 85 87 private final XmlStreamReaderProxy proxy = new XmlStreamReaderProxy(); 86 88 private final SRUExplainRecordDataParser explainRecordParser = … … 123 125 } 124 126 125 // create HTTP client 126 httpClient = createHttpClient(config.getConnectTimeout(), 127 config.getSocketTimeout()); 127 final CloseableHttpClient client = config.getCustomizedHttpClient(); 128 if (client != null) { 129 // use customized http client 130 this.httpClient = client; 131 this.httpContext = config.getHttpClientContext(); 132 } else { 133 // create HTTP client 134 httpClient = createHttpClient(config.getConnectTimeout(), 135 config.getSocketTimeout()); 136 httpContext = null; 137 } 128 138 } 129 139 … … 405 415 try { 406 416 HttpGet request = new HttpGet(uri); 407 response = httpClient.execute(request);417 response = httpClient.execute(request, httpContext); 408 418 StatusLine status = response.getStatusLine(); 409 419 if (status.getStatusCode() != HttpStatus.SC_OK) { … … 1437 1447 .setSocketTimeout(socketTimeout) 1438 1448 .setConnectionRequestTimeout(0) /* infinite */ 1439 .setStaleConnectionCheckEnabled(false)1440 1449 .build(); 1441 1450
Note: See TracChangeset
for help on using the changeset viewer.