Java jersey2 client: get/set connect timeout

This commit is contained in:
xhh
2016-01-08 19:05:43 +08:00
parent 9657d4437e
commit 3d65218619
4 changed files with 81 additions and 29 deletions

View File

@@ -59,8 +59,6 @@ public class ApiClient {
private DateFormat dateFormat;
public ApiClient() {
httpClient = buildHttpClient(debugging);
mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -69,6 +67,8 @@ public class ApiClient {
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
mapper.registerModule(new JodaModule());
httpClient = buildHttpClient(debugging);
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

View File

@@ -12,7 +12,9 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.MultiPart;
@@ -43,12 +45,13 @@ import {{invokerPackage}}.auth.OAuth;
{{>generatedAnnotation}}
public class ApiClient {
private Client client;
private Map<String, Client> hostMap = new HashMap<String, Client>();
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private boolean debugging = false;
private String basePath = "{{basePath}}";
private JSON json = new JSON();
private boolean debugging = false;
private int connectionTimeout = 0;
private Client httpClient;
private JSON json;
private Map<String, Authentication> authentications;
@@ -58,6 +61,9 @@ public class ApiClient {
private DateFormat dateFormat;
public ApiClient() {
json = new JSON();
httpClient = buildHttpClient(debugging);
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
@@ -70,8 +76,6 @@ public class ApiClient {
// Set default User-Agent.
setUserAgent("Java-Swagger");
buildClient();
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
@@ -226,10 +230,29 @@ public class ApiClient {
*/
public ApiClient setDebugging(boolean debugging) {
this.debugging = debugging;
buildClient();
// Rebuild HTTP Client according to the new "debugging" value.
this.httpClient = buildHttpClient(debugging);
return this;
}
/**
* Connect timeout (in milliseconds).
*/
public int getConnectTimeout() {
return connectionTimeout;
}
/**
* Set the connect timeout (in milliseconds).
* A value of 0 means no timeout, otherwise values must be between 1 and
* {@link Integer#MAX_VALUE}.
*/
public ApiClient setConnectTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
httpClient.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout);
return this;
}
/**
* Get the date format used to parse/format date parameters.
*/
@@ -472,7 +495,7 @@ public class ApiClient {
public <T> T invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType) throws ApiException {
updateParamsForAuth(authNames, queryParams, headerParams);
WebTarget target = client.target(this.basePath).path(path);
WebTarget target = httpClient.target(this.basePath).path(path);
if (queryParams != null) {
for (Pair queryParam : queryParams) {
@@ -545,15 +568,18 @@ public class ApiClient {
}
}
private void buildClient() {
/**
* Build the Client used to make HTTP requests.
*/
private Client buildHttpClient(boolean debugging) {
final ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MultiPartFeature.class);
clientConfig.register(json);
clientConfig.register(org.glassfish.jersey.jackson.JacksonFeature.class);
clientConfig.register(JacksonFeature.class);
if (debugging) {
clientConfig.register(LoggingFilter.class);
}
this.client = ClientBuilder.newClient(clientConfig);
return ClientBuilder.newClient(clientConfig);
}
private Map<String, List<String>> buildResponseHeaders(Response response) {