[JAVA-VERTX] make Vertx client support running on different contexts (#6283)

* [java-vertx] make vertx client support running on different contexts

* [java-vertx] make vertx client support running on different contexts (samples)
This commit is contained in:
lopesmcc 2017-08-12 10:56:20 +01:00 committed by wing328
parent bc633766f8
commit 2ebb1bcaf0
33 changed files with 55 additions and 53 deletions

View File

@ -40,10 +40,11 @@ public class ApiClient {
private static final OpenOptions FILE_DOWNLOAD_OPTIONS = new OpenOptions().setCreate(true).setTruncateExisting(true);
private final Vertx vertx;
private final JsonObject config;
private final String identifier;
private MultiMap defaultHeaders = MultiMap.caseInsensitiveMultiMap();
private Map<String, Authentication> authentications;
private WebClient webClient;
private String basePath = "{{{basePath}}}";
private DateFormat dateFormat;
private ObjectMapper objectMapper;
@ -84,9 +85,8 @@ public class ApiClient {
// Configurations
this.basePath = config.getString("basePath", this.basePath);
this.downloadsDir = config.getString("downloadsDir", this.downloadsDir);
// Build WebClient
this.webClient = buildWebClient(vertx, config);
this.config = config;
this.identifier = UUID.randomUUID().toString();
}
public Vertx getVertx() {
@ -102,15 +102,16 @@ public class ApiClient {
return this;
}
public WebClient getWebClient() {
public synchronized WebClient getWebClient() {
String webClientIdentifier = "web-client-" + identifier;
WebClient webClient = Vertx.currentContext().get(webClientIdentifier);
if (webClient == null) {
webClient = buildWebClient(vertx, config);
Vertx.currentContext().put(webClientIdentifier, webClient);
}
return webClient;
}
public ApiClient setWebClient(WebClient webClient) {
this.webClient = webClient;
return this;
}
public String getBasePath() {
return basePath;
}
@ -428,7 +429,7 @@ public class ApiClient {
}
HttpMethod httpMethod = HttpMethod.valueOf(method);
HttpRequest<Buffer> request = webClient.requestAbs(httpMethod, basePath + path);
HttpRequest<Buffer> request = getWebClient().requestAbs(httpMethod, basePath + path);
if (httpMethod == HttpMethod.PATCH) {
request.putHeader("X-HTTP-Method-Override", "PATCH");

View File

@ -94,7 +94,7 @@ This endpoint does not need any parameter.
### Return type
[**Map&lt;String, Integer&gt;**](Map.md)
**Map&lt;String, Integer&gt;**
### Authorization

View File

@ -40,10 +40,11 @@ public class ApiClient {
private static final OpenOptions FILE_DOWNLOAD_OPTIONS = new OpenOptions().setCreate(true).setTruncateExisting(true);
private final Vertx vertx;
private final JsonObject config;
private final String identifier;
private MultiMap defaultHeaders = MultiMap.caseInsensitiveMultiMap();
private Map<String, Authentication> authentications;
private WebClient webClient;
private String basePath = "http://petstore.swagger.io:80/v2";
private DateFormat dateFormat;
private ObjectMapper objectMapper;
@ -85,9 +86,8 @@ public class ApiClient {
// Configurations
this.basePath = config.getString("basePath", this.basePath);
this.downloadsDir = config.getString("downloadsDir", this.downloadsDir);
// Build WebClient
this.webClient = buildWebClient(vertx, config);
this.config = config;
this.identifier = UUID.randomUUID().toString();
}
public Vertx getVertx() {
@ -103,15 +103,16 @@ public class ApiClient {
return this;
}
public WebClient getWebClient() {
public synchronized WebClient getWebClient() {
String webClientIdentifier = "web-client-" + identifier;
WebClient webClient = Vertx.currentContext().get(webClientIdentifier);
if (webClient == null) {
webClient = buildWebClient(vertx, config);
Vertx.currentContext().put(webClientIdentifier, webClient);
}
return webClient;
}
public ApiClient setWebClient(WebClient webClient) {
this.webClient = webClient;
return this;
}
public String getBasePath() {
return basePath;
}
@ -429,7 +430,7 @@ public class ApiClient {
}
HttpMethod httpMethod = HttpMethod.valueOf(method);
HttpRequest<Buffer> request = webClient.requestAbs(httpMethod, basePath + path);
HttpRequest<Buffer> request = getWebClient().requestAbs(httpMethod, basePath + path);
if (httpMethod == HttpMethod.PATCH) {
request.putHeader("X-HTTP-Method-Override", "PATCH");

View File

@ -127,6 +127,6 @@ public class AdditionalPropertiesClass {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -115,6 +115,6 @@ public class Animal {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -60,6 +60,6 @@ public class AnimalFarm extends ArrayList<Animal> {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -96,6 +96,6 @@ public class ArrayOfArrayOfNumberOnly {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -96,6 +96,6 @@ public class ArrayOfNumberOnly {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -158,6 +158,6 @@ public class ArrayTest {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -200,6 +200,6 @@ public class Capitalization {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -87,6 +87,6 @@ public class Cat extends Animal {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -108,6 +108,6 @@ public class Category {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -86,6 +86,6 @@ public class ClassModel {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -85,6 +85,6 @@ public class Client {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -87,6 +87,6 @@ public class Dog extends Animal {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -188,6 +188,6 @@ public class EnumArrays {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -262,6 +262,6 @@ public class EnumTest {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -375,6 +375,6 @@ public class FormatTest {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -90,6 +90,6 @@ public class HasOnlyReadOnly {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -162,6 +162,6 @@ public class MapTest {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -145,6 +145,6 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -109,6 +109,6 @@ public class Model200Response {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -131,6 +131,6 @@ public class ModelApiResponse {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -86,6 +86,6 @@ public class ModelReturn {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -137,6 +137,6 @@ public class Name {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -86,6 +86,6 @@ public class NumberOnly {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -238,6 +238,6 @@ public class Order {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -132,6 +132,6 @@ public class OuterComposite {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -254,6 +254,6 @@ public class Pet {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -99,6 +99,6 @@ public class ReadOnlyFirst {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -85,6 +85,6 @@ public class SpecialModelName {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -108,6 +108,6 @@ public class Tag {
}
return o.toString().replace("\n", "\n ");
}
}

View File

@ -246,6 +246,6 @@ public class User {
}
return o.toString().replace("\n", "\n ");
}
}