|
2 | 2 |
|
3 | 3 | https://vertx.io/[Vertx] Reactive applications on the JVM. |
4 | 4 |
|
| 5 | +This module export the following services: |
| 6 | + |
| 7 | +- io.vertx.core.Vertx |
| 8 | +- io.vertx.core.eventbus.EventBus |
| 9 | +- io.vertx.core.file.FileSystem |
| 10 | +
|
5 | 11 | === Usage |
6 | 12 |
|
7 | 13 | 1) Add the dependency: |
@@ -120,4 +126,132 @@ import io.jooby.vertx.VertxModule; |
120 | 126 | } |
121 | 127 | ---- |
122 | 128 |
|
123 | | -include::vertx-sql-client.adoc[] |
| 129 | +include::modules/vertx-sql-client.adoc[] |
| 130 | + |
| 131 | +== Vertx Server (Advanced) |
| 132 | + |
| 133 | +The javadoc:vertx.VertxServer[] allow you to share the Vertx acceptor and event loop groups within |
| 134 | +the javadoc:netty.NettyServer[], so both vertx and netty web server implementation share the same |
| 135 | +resources. See https://vertx.io/docs/guides/advanced-vertx-guide/#integrating-netty[for more information]. |
| 136 | + |
| 137 | +The vertx server must be manually provided at boot time (there is no service loader support for it): |
| 138 | + |
| 139 | +.Java |
| 140 | +[source, java, role="primary"] |
| 141 | +---- |
| 142 | +import io.jooby.vertx.VertxServer; |
| 143 | +
|
| 144 | +{ |
| 145 | + get("/vertx", ctx -> { |
| 146 | + return "Running from vertx event loop"; |
| 147 | + }); |
| 148 | +} |
| 149 | +
|
| 150 | +public static void main(String[] args) { |
| 151 | + runApp(args, new VertxServer(), EVENT_LOOP, App::new); |
| 152 | +} |
| 153 | +---- |
| 154 | + |
| 155 | +.Kotlin |
| 156 | +[source, kt, role="secondary"] |
| 157 | +---- |
| 158 | +import io.jooby.vertx.VertxServer |
| 159 | +
|
| 160 | +import io.jooby.vertx.VertxServer; |
| 161 | +
|
| 162 | +{ |
| 163 | + get("/vertx") { |
| 164 | + "Running from vertx event loop" |
| 165 | + } |
| 166 | +} |
| 167 | +
|
| 168 | +un main(args: Array<String>) { |
| 169 | + runApp(args, VertxServer(), EVENT_LOOP, ::App) |
| 170 | +} |
| 171 | +---- |
| 172 | + |
| 173 | +If you run multiple applications, they all share a single Vertx instance. |
| 174 | + |
| 175 | +[IMPORTANT] |
| 176 | +==== |
| 177 | +*VertxModule must NOT be installed while running on VertxServer* |
| 178 | +==== |
| 179 | + |
| 180 | +One of the main benefits of running on the VertxServer is to share/reuse the event loop threads. |
| 181 | +You can run non-blocking code on the Vertx event loop thread without context switch. A good example |
| 182 | +of this is to run within |
| 183 | + |
| 184 | +=== Sql Connection |
| 185 | + |
| 186 | +These modules exist as part of the performance tests required by https://www.techempower.com/benchmarks[Techempower]. |
| 187 | + |
| 188 | +You are free to use them, but keep in mind they are for very specific kind of application. |
| 189 | + |
| 190 | +include::vertx-sql-connection.template[artifactId="jooby-vertx-mysql-client" sqlClient="MySQL" moduleName="VertxMySQLConnectionModule"] |
| 191 | + |
| 192 | +include::vertx-sql-connection.template[artifactId="jooby-vertx-pg-client" sqlClient="Postgres" moduleName="VertxPgConnectionModule"] |
| 193 | + |
| 194 | + |
| 195 | +==== How it works? |
| 196 | + |
| 197 | +There is an internal Verticle (one per IO threads) with a dedicated https://vertx.io/docs/apidocs/io/vertx/sqlclient/SqlConnection.html[SqlConnection]. This connection is only |
| 198 | +accessible from a Vertx thread any attempt to access to the connection from a non Vertx thread |
| 199 | +will result in exception. |
| 200 | + |
| 201 | +Same applies for the https://vertx.io/docs/apidocs/io/vertx/sqlclient/PreparedStatement.html[PreparedStatement] / https://vertx.io/docs/apidocs/io/vertx/sqlclient/PreparedQuery.html[PreparedQuery] instances. |
| 202 | + |
| 203 | +.PreparedStatement example: |
| 204 | +[source,java] |
| 205 | +---- |
| 206 | +import io.jooby.Reified; |
| 207 | +
|
| 208 | +String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1"; |
| 209 | +
|
| 210 | +Reified<PreparedQuery<RowSet<Row>>> PreparedQueryType = |
| 211 | + getParameterized(PreparedQuery.class, getParameterized(RowSet.class, Row.class)); |
| 212 | +
|
| 213 | +{ |
| 214 | + install(new VertxPgConnectionModule() |
| 215 | + .preparedStatement(Map.of("selectWorld", List.of(SELECT_WORLD))) <1> |
| 216 | + ); |
| 217 | +
|
| 218 | + use(vertx()); <2> |
| 219 | +
|
| 220 | + var selectWorldQuery = ctx.require(PreparedQueryType, "selectWorld"); <3> |
| 221 | + get("/world/{id}", ctx -> { |
| 222 | + return selectWorldQuery.execute(Tuple.of(ctx.path("id").longValue())) |
| 223 | + .map( |
| 224 | + result -> { |
| 225 | + var row = result.iterator().next(); |
| 226 | + return new World(row.getInteger(0), row.getInteger(1)); |
| 227 | + }); |
| 228 | + }); |
| 229 | +} |
| 230 | +
|
| 231 | +public static void main(String[] args) { |
| 232 | + runApp(args, new VertxServer(), EVENT_LOOP, App::new); |
| 233 | +} |
| 234 | +---- |
| 235 | + |
| 236 | +<1> Created a prepared statement. This is done when Verticle is deployed |
| 237 | +<2> Add Vertx handler, so it can render Future and Promise. |
| 238 | +<3> Get a reference to the prepared statement |
| 239 | + |
| 240 | +The line: |
| 241 | + |
| 242 | + var selectWorldQuery = ctx.require(PreparedQueryType, "selectWorld"); |
| 243 | + |
| 244 | +Allow you to get a reference/proxy to the prepared statement/query. The proxy will thrown an exception |
| 245 | +if you try to use from a non Vertx thread. The prepared statement is compiled when the Verticle |
| 246 | +is deployed (at application startup time). |
| 247 | + |
| 248 | +If you work with any of the dependency injection supported framework, you can inject the prepared |
| 249 | +statement like: |
| 250 | + |
| 251 | +[source,java] |
| 252 | +---- |
| 253 | +@Inject |
| 254 | +public WorldRepository(@Named("selectWorld") PreparedQuery<RowSet<Row>> selectWorld) { |
| 255 | + this.selectWorld = selectWorld; |
| 256 | +} |
| 257 | +---- |
0 commit comments