@@ -75,19 +75,19 @@ public class HttpConnection implements Connection {
75
75
private static final String CONTENT_TYPE_VPACK = "application/x-velocypack" ;
76
76
private static final String USER_AGENT = getUserAgent ();
77
77
private static final AtomicInteger THREAD_COUNT = new AtomicInteger ();
78
- private String auth ;
78
+ private volatile String auth ;
79
79
private final int compressionThreshold ;
80
80
private final Encoder encoder ;
81
81
private final WebClient client ;
82
82
private final Integer timeout ;
83
83
private final MultiMap commonHeaders = MultiMap .caseInsensitiveMultiMap ();
84
- private final Vertx vertx ;
84
+ private final Vertx vertxToClose ;
85
85
86
86
private static String getUserAgent () {
87
87
return "JavaDriver/" + PackageVersion .VERSION + " (JVM/" + System .getProperty ("java.specification.version" ) + ")" ;
88
88
}
89
89
90
- HttpConnection (final ArangoConfig config , final HostDescription host ) {
90
+ HttpConnection (final ArangoConfig config , final HostDescription host , final Vertx existingVertx ) {
91
91
super ();
92
92
Protocol protocol = config .getProtocol ();
93
93
ContentType contentType = ContentTypeFactory .of (protocol );
@@ -108,14 +108,25 @@ private static String getUserAgent() {
108
108
}
109
109
commonHeaders .add ("x-arango-driver" , USER_AGENT );
110
110
timeout = config .getTimeout ();
111
- vertx = Vertx .vertx (new VertxOptions ().setPreferNativeTransport (true ).setEventLoopPoolSize (1 ));
112
- vertx .runOnContext (e -> {
113
- Thread .currentThread ().setName ("adb-http-" + THREAD_COUNT .getAndIncrement ());
114
- auth = new UsernamePasswordCredentials (
115
- config .getUser (), Optional .ofNullable (config .getPassword ()).orElse ("" )
116
- ).toHttpAuthorization ();
117
- LOGGER .debug ("Created Vert.x context" );
118
- });
111
+ auth = new UsernamePasswordCredentials (
112
+ config .getUser (), Optional .ofNullable (config .getPassword ()).orElse ("" )
113
+ ).toHttpAuthorization ();
114
+
115
+ Vertx vertxToUse ;
116
+ if (existingVertx != null ) {
117
+ // reuse existing Vert.x
118
+ vertxToUse = existingVertx ;
119
+ // Vert.x will not be closed when connection is closed
120
+ vertxToClose = null ;
121
+ LOGGER .debug ("Reusing existing Vert.x instance" );
122
+ } else {
123
+ // create a new Vert.x instance
124
+ LOGGER .debug ("Creating new Vert.x instance" );
125
+ vertxToUse = Vertx .vertx (new VertxOptions ().setPreferNativeTransport (true ).setEventLoopPoolSize (1 ));
126
+ vertxToUse .runOnContext (e -> Thread .currentThread ().setName ("adb-http-" + THREAD_COUNT .getAndIncrement ()));
127
+ // Vert.x be closed when connection is closed
128
+ vertxToClose = vertxToUse ;
129
+ }
119
130
120
131
int intTtl = Optional .ofNullable (config .getConnectionTtl ())
121
132
.map (ttl -> Math .toIntExact (ttl / 1000 ))
@@ -193,7 +204,7 @@ public SslContextFactory sslContextFactory() {
193
204
});
194
205
}
195
206
196
- client = WebClient .create (vertx , webClientOptions );
207
+ client = WebClient .create (vertxToUse , webClientOptions );
197
208
}
198
209
199
210
private static String buildUrl (final InternalRequest request ) {
@@ -229,7 +240,10 @@ private static void addHeader(final InternalRequest request, final HttpRequest<?
229
240
@ Override
230
241
public void close () {
231
242
client .close ();
232
- vertx .close ();
243
+ if (vertxToClose != null ) {
244
+ LOGGER .debug ("Closing Vert.x instance" );
245
+ vertxToClose .close ();
246
+ }
233
247
}
234
248
235
249
private HttpMethod requestTypeToHttpMethod (RequestType requestType ) {
@@ -254,7 +268,7 @@ private HttpMethod requestTypeToHttpMethod(RequestType requestType) {
254
268
@ UnstableApi
255
269
public CompletableFuture <InternalResponse > executeAsync (@ UnstableApi final InternalRequest request ) {
256
270
CompletableFuture <InternalResponse > rfuture = new CompletableFuture <>();
257
- vertx . runOnContext ( e -> doExecute (request , rfuture ) );
271
+ doExecute (request , rfuture );
258
272
return rfuture ;
259
273
}
260
274
@@ -308,7 +322,7 @@ private InternalResponse buildResponse(final HttpResponse<Buffer> httpResponse)
308
322
@ Override
309
323
public void setJwt (String jwt ) {
310
324
if (jwt != null ) {
311
- vertx . runOnContext ( e -> auth = new TokenCredentials (jwt ).toHttpAuthorization () );
325
+ auth = new TokenCredentials (jwt ).toHttpAuthorization ();
312
326
}
313
327
}
314
328
0 commit comments