{"id":16119209,"url":"https://github.com/cescoffier/vertx-completable-future","last_synced_at":"2025-03-16T08:32:48.029Z","repository":{"id":53294847,"uuid":"65304790","full_name":"cescoffier/vertx-completable-future","owner":"cescoffier","description":"An implementation of CompletableFuture for Vert.x","archived":false,"fork":false,"pushed_at":"2023-04-02T15:23:50.000Z","size":104,"stargazers_count":80,"open_issues_count":7,"forks_count":29,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-27T06:02:28.171Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cescoffier.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-09T15:08:17.000Z","updated_at":"2024-12-09T21:10:54.000Z","dependencies_parsed_at":"2022-08-19T19:20:50.828Z","dependency_job_id":null,"html_url":"https://github.com/cescoffier/vertx-completable-future","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cescoffier%2Fvertx-completable-future","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cescoffier%2Fvertx-completable-future/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cescoffier%2Fvertx-completable-future/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cescoffier%2Fvertx-completable-future/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cescoffier","download_url":"https://codeload.github.com/cescoffier/vertx-completable-future/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243807556,"owners_count":20351012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T20:53:38.720Z","updated_at":"2025-03-16T08:32:47.556Z","avatar_url":"https://github.com/cescoffier.png","language":"Java","funding_links":[],"categories":["并发编程"],"sub_categories":[],"readme":"# Vert.x Completable Future\n\nThis project provides a way to use the Completable Future API with Vert.x.\n\n## Why can't you use Completable Future with Vert.x\n\nUsing Completable Future with Vert.x may lead to some thread issues. \n  \nWhen you execute an asynchronous operation with Vert.x, the `Handler\u003cAsyncResult\u003cT\u003e\u003e` is called on on the same thread as the method having enqueued the asynchronous operation. Thanks to this _single-threaded_ aspect, Vert.x removes the need of synchronization and also it improves performances.\n   \nCompletable Future uses a fork-join thread pool. So callbacks (dependent stages) are called in a thread of this pool, so it's not the caller thread.\n    \nThis project provides the Completable Future API but enforces the Vert.x threading model:\n\n* When using `xAsync` methods (without `executor`), the callbacks are called on the Vert.x context\n* When using non-async, it uses the caller thread. If it's a Vert.x thread the same thread is used. If not called from a Vert.x thread, \nit still uses the caller thread \n* When using `xAsync` methods with an `Executor` parameter, this executor is used to execute the callback (does not enforce the Vert.x thread system)\n     \n## Examples\n\nAll methods taking a `Vertx` instance as parameter exists with a `Context` parameter.\n     \n### Creating a VertxCompletableFuture\n     \n```\n// From a Vert.x instance\nCompletableFuture\u003cT\u003e future = new VertxCompletableFuture(vertx);\n\n// From a specific context\nContext context = ...\nCompletableFuture\u003cT\u003e future = new VertxCompletableFuture(context);\n\n// From a completable future\nCompletableFuture cf = ...\nCompletableFuture\u003cT\u003e future = VertxCompletableFuture.from(context, cf);\n\n// From a Vert.x future\nFuture\u003cT\u003e fut = ...\nCompletableFuture\u003cT\u003e future = VertxCompletableFuture.from(context, fut);\n```     \n\nYou can also pass a `Supplier` or a `Runnable`:\n\n```\n// Run in context\nCompletableFuture\u003cString\u003e future = VertxCompletableFuture.supplyAsync(vertx, () -\u003e return \"foo\";);\n// Run in Vert.x worker thread\nCompletableFuture\u003cString\u003e future = VertxCompletableFuture.supplyBlockingAsync(vertx, () -\u003e return \"foo\");\n \nCompletableFuture\u003cVoid\u003e future = VertxCompletableFuture.runAsync(vertx, () -\u003e System.out.println(foo\"));\nCompletableFuture\u003cVoid\u003e future = VertxCompletableFuture.runBlockingAsync(vertx, () -\u003e System.out.println(foo\"));\n```\n\n_*BlockingAsync_ method uses a Vert.x worker thread and do not block the Vert.x Event Loop.\n\n### Stages\n\nOnce you have the `VertxCompletableFuture` instance, you can use the `CompletableFuture` API:\n\n```\nVertxCompletableFuture\u003cInteger\u003e future = new VertxCompletableFuture\u003c\u003e(vertx);\nfuture\n    .thenApply(this::square)\n    .thenAccept(System.out::print)\n    .thenRun(System.out::println);\n\n// Somewhere in your code, later...\nfuture.complete(42);\n```\n\nYou can compose, combine, join completable futures with:\n\n* combine\n* runAfterEither\n* acceptEither\n* runAfterBoth\n* ...        \n\n### All and Any operations\n\nThe `VertxCompletableFuture` class offers two composition operators:\n    \n* `allOf` - execute all the passed `CompletableFuture` (not necessarily `VertxCompletableFuture`) and calls dependant stages when all of the futures have been completed or one has failed \n* `anyOf` - execute all the passed `CompletableFuture` (not necessarily `VertxCompletableFuture`) and calls dependant stages when one of them has been completed\n     \nUnlike `CompletableFuture`, the dependent stages are called in the Vert.x context.     \n\n```\nHttpClientOptions options = new HttpClientOptions().setDefaultPort(8080).setDefaultHost(\"localhost\");\nHttpClient client1 = vertx.createHttpClient(options);\nHttpClient client2 = vertx.createHttpClient(options);\n\nVertxCompletableFuture\u003cInteger\u003e requestA = new VertxCompletableFuture\u003c\u003e(vertx);\nclient1.get(\"/A\").handler(resp -\u003e {\n  resp.exceptionHandler(requestA::completeExceptionally)\n      .bodyHandler(buffer -\u003e {\n        requestA.complete(Integer.parseInt(buffer.toString()));\n      });\n}).exceptionHandler(requestA::completeExceptionally).end();\n\nVertxCompletableFuture\u003cInteger\u003e requestB = new VertxCompletableFuture\u003c\u003e(vertx);\nclient2.get(\"/B\").handler(resp -\u003e {\n  resp.exceptionHandler(requestB::completeExceptionally)\n      .bodyHandler(buffer -\u003e {\n        requestB.complete(Integer.parseInt(buffer.toString()));\n      });\n}).exceptionHandler(requestB::completeExceptionally).end();\n\n\nVertxCompletableFuture.allOf(requestA, requestB).thenApply(v -\u003e requestA.join() + requestB.join())\n    .thenAccept(i -\u003e {\n      tc.assertEquals(65, i);\n      async.complete();\n    });\n}\n```\n\n### From / To Vert.x Futures\n\nYou can transform a `VertxCompletableFuture` to a Vert.x `Future` with the `toFuture` method.\n\nYou can also creates a new `VertxCompletableFuture` from a Vert.x `Future` using:\n\n```\nFuture\u003cInteger\u003e vertxFuture = ...\nVertxCompletableFuture\u003cInteger\u003e vcf = VertxCompletableFuture.from(vertx, vertxFuture);\n\nvcf.thenAccept(i -\u003e {...}).whenComplete((res, err) -\u003e {...})\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcescoffier%2Fvertx-completable-future","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcescoffier%2Fvertx-completable-future","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcescoffier%2Fvertx-completable-future/lists"}