-
Notifications
You must be signed in to change notification settings - Fork 96
[DE-535] Provide the capability to reuse an existing Vertx instance #557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@jamesnetherton |
Hi, i have project that uses Vertx as framework. In one of the verticles I create a connection to arangoDB with java driver. With a configuration: if I add .protocolConfig(HttpProtocolConfig.builder().vertx(Vertx.currentContext().owner()).build()) to the build call, then my request: |
Hi, Are you invoking it from a Vert.x thread? In such case, you would block the Vert.x thread at the calling site and the driver would have no executor thread available to perform the request. Instead, from within Vert.x threads you need to use the async driver API:
Furthermore beware of the caveats of using CompletableFuture from within Vert.x threads, see https://github.com/cescoffier/vertx-completable-future Or convert the CompletableFuture to Vert.x Future:
|
Hi, Thank you very much for your answer, but that does not explain why does it work, when you are creating a new vertx instance inside of HTTPConnection. Regards, |
The Vert.x instance created inside HTTPConnection is used exclusively from the driver connection and it is never blocked. In such case, you would have multiple Vert.x instances in your applications, each one using different threads. So if you use the blocking API in your Vert.x threads you do not prevent the driver from executing the request, which is executed on different threads. Note that even in this case, you would block Vert.x threads, thus preventing execution of other Vert.x tasks (not from the driver, but related to other components of your application). On the other side, when reusing the existing Vert.x instance, the driver does not create new Vert.x instaces or new threads, but reuses the existing ones. |
Thank you very much for clarification. But that generally means, that if i use now the driver inside of my Vertx application and reuse the existing context, for each call to db (query, create, update) i need now create a Future? Before in my application I got a request, sent it over eventBus to DB vertical and there directly did a query. |
So if i have a simple application like: public class MainVerticle extends AbstractVerticle {
.protocolConfig(HttpProtocolConfig.builder().vertx(Vertx.currentContext().owner()).build()).build(); Thank you, |
No, using the async API of the driver you get CompletableFuture back.
this should work and not hang. |
Yes, i am, as i get the following: Sure, i start it now directly from IDE with io.vertx.core.Launcher run var.verticles.MainVerticle
} |
You are blocking the thread in your code:
Note that |
Yes, i found it out two. But for all who used previous version in Vertx it will be quite a big change, even if you put aside all the changes to serializer. |
Using any blocking API from Vert.x would be a mistake. I.e. also using the driver synch API without reusing the existing Vert.x. Doing that you are actually still blocking the Vert.x threads. See https://vertx.io/docs/vertx-core/java/#_dont_block_me |
No, not really. It will also not work in the case that you have a HTTP Vertical that listens to a request, then send to eventBus and Database Vertical sends a request to DB and answers back. You will have to add also in this case an async call with all the callbacks. I just do not understand why to use Vertx at all just to add a WebClient. |
Also blocking the thread from an event bus handler would be a mistake. Maybe you are just fine with that, as long as you serve few requests and they take short time to execute. If you like keeping this approach, you can just avoid reusing the Vert.x instance, i.e. do not configure
This will create a new Vert.x instance internal to the driver which will not interfere with your one. |
OK, got it. Thank you for clarification. Regards, |
You are welcome. Note that this is currently just the internal implementation of the HttpConnection. Feel free to contribute with your feedback and ideas about it. |
Ah, got it. The problem is that the code with .protocolConfig does not work even if you use these: So even if you connect to DB from the worker verticle (which i do) i get the same probem. Andrey |
Also in the case of worker verticles, the same consideration holds. When using the sync API, the thread will be blocked to wait the pending request to complete, i.e. in
|
Hi, after reading a documentation a bit more and searching for best practice, i would disagree with you. Worker verticles are exactly designed for calling blocking code. And it allows you to control how many concurrent connection you would like to have in your application by deploying needed amount of verticles. So i would say it is a bug of the 7.7.0, that allowed to provide your own Vertx context. It should be possible just to use synchronous api inside of the worker verticle. |
You can try running my code from above in a worker verticle, you will see that it will hang forever.
|
Yes, can be, but i do not really care about the webClient, i just want to open connection to a DB and execute synchronous request against it. |
Unfortunately this is not possible because of the way Vert.x works. If the user blocks the thread, then the same thread cannot be used by Vert.x! If you really want to do it, then you need 2 threads: one user thread which will be blocked on the calling site and another executing the request. The example above about the WebClient is to explain how things are. Under the hood, the driver executes the requests with the Vert.x WebClient. |
@jamesnetherton how did you organize the requests? |
My use case is probably quite a bit different to yours. The context of mine is a Quarkus application that uses the Apache Camel ArangoDB component. In Quarkus, there's a singleton Vertx CDI bean. Camel resolves it from the CDI bean factory and then passes it to the ArangoDB client builder. |
My project is based on a Framework (Quarkus) that uses Vert.x.
When I use the ArangoDB Java Driver to connect to a server I see:
It happens because
HttpConnection
, creates its ownVertx
:arangodb-java-driver/http/src/main/java/com/arangodb/http/HttpConnection.java
Line 111 in 3c5d8be
In my case, there's already an existing
Vertx
instance managed by Quarkus. So it'd be useful if there were some way to reuse it in the ArangoDB Java Driver.The text was updated successfully, but these errors were encountered: