You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Moved Java code samples to the examples folder, and gave the Vertx
topics a copy edit, and structured the tutorial according to new
template
---------
Co-authored-by: Rob Swain <rob.swain@hazelcast.com>
Co-authored-by: Jack Green <jack.green@hazelcast.com>
Let's modify the code, so we can start multiple instances easily - the application will start on the defined port, and when the port is not available it will search for another port:
149
+
Let's modify the code, so we can start multiple instances easily — the application will start on the defined port and, if the port is unavailable, it will search for another port:
145
150
146
151
Add the following method to the `MainVerticle.java` class:
147
152
148
153
[source,java]
149
154
----
150
-
private int findFreePort(int from) {
155
+
private int findFreePort(int from) {
151
156
for (int port = from; port < from + 100; port++) {
152
157
try {
153
158
new ServerSocket(port).close();
@@ -160,20 +165,20 @@ Add the following method to the `MainVerticle.java` class:
160
165
}
161
166
----
162
167
163
-
and use it in the `start` method:
168
+
And then use it in the `start` method:
164
169
165
170
[source,java]
166
171
----
167
-
...
168
-
int port = findFreePort(8888);
169
-
170
-
// Create the HTTP server
171
-
vertx.createHttpServer()
172
-
// Handle every request using the router
173
-
.requestHandler(router)
174
-
// Start listening
175
-
.listen(port)
176
-
...
172
+
...
173
+
int port = findFreePort(8888);
174
+
175
+
// Create the HTTP server
176
+
vertx.createHttpServer()
177
+
// Handle every request using the router
178
+
.requestHandler(router)
179
+
// Start listening
180
+
.listen(port)
181
+
...
177
182
----
178
183
179
184
Now, we can start two instances:
@@ -193,7 +198,7 @@ Aug 30, 2024 9:09:47 AM io.vertx.launcher.application.VertxApplication
193
198
INFO: Succeeded in deploying verticle
194
199
----
195
200
196
-
and we can see the session is not shared between the instances. Here is the request to the first instance:
201
+
We can see the session is not shared between the instances. Here is the request to the first instance:
and here is the request to the 2nd instance. Notice the different port and that we use the cookie we received, but the data does not contain the previous message.
218
+
And here is the request to the 2nd instance. Notice the different port and that we use the cookie we received, but the data does not contain the previous message.
We will fix that by using a Hazelcast Cluster Manager. There are two modules that provide Hazelcast Cluster Manager:
231
236
232
-
- `io.vertx:vertx-hazelcast` - this module is maintained by the Vert.x team, with contributions from Hazelcast, and is built on top of open-source Hazelcast
237
+
- `io.vertx:vertx-hazelcast` - this module is maintained by the Vert.x team, with contributions from Hazelcast, and is built on top of open-source Hazelcast.
233
238
- `com.hazelcast:vertx-hazelcast-enterprise` - this module is maintained by the Hazelcast team and is built on top of the `vertx-hazelcast` but uses Hazelcast Enterprise instead. You need an enterprise license to use Hazelcast Enterprise.
234
239
235
-
You can use either module for most of this tutorial. At the end of this tutorial you will need the `vertx-hazelcast-enterprise` module.
240
+
You can use either module for most of this tutorial, however, at the end you will need the `vertx-hazelcast-enterprise` module.
236
241
237
-
NOTE: You can get your trial key at https://hazelcast.com/get-started/?utm_source=docs-website[hazelcast.com] or you can use `vertx-hazelcast` and a community edition of Hazelcast.
242
+
NOTE: You can get a trial key at https://hazelcast.com/get-started/?utm_source=docs-website[hazelcast.com] or you can use `vertx-hazelcast` and a {open-source-product-name} of Hazelcast.
238
243
239
244
Add the following dependency to the `pom.xml`:
240
245
@@ -263,7 +268,7 @@ to the following:
263
268
SessionStore store = ClusteredSessionStore.create(vertx);
264
269
----
265
270
266
-
and from now on we will start the application with `-server` parameter, which tells Vert.x to look for a cluster manager implementation.
271
+
From now on, we will start the application with the `-server` parameter, which tells Vert.x to look for a cluster manager implementation.
267
272
268
273
We also need to provide a Hazelcast configuration file, and create a file cluster.xml in the `src/main/resources` directory:
Next, we'll use Hazelcast's distributed counter functionality to generate unique, incrementing IDs across a distributed system. The counter is maintained across all nodes in the Hazelcast cluster, ensuring each request gets a unique ID even when multiple servers are handling requests.
384
+
378
385
Replace this part of the code at the end of the `start()` method:
379
386
380
387
[source,java]
@@ -404,7 +411,7 @@ context.vertx()
404
411
});
405
412
----
406
413
407
-
When you now try the application, you can see the response contains an additional field named `requestId` and its value increments for every request.
414
+
Now, when you try the application, you can see the response contains an additional field named `requestId` and its value increments for every request.
The module `vertx-hazelcast-enterprise` provides a different implementation of the `io.vertx.core.shareddata.Counter` and `io.vertx.core.shareddata.Lock` data structures. The implementation in `vertx-hazelcast` is based on the IMap data structure and provides guarantees defined in the xref:architecture:data-partitioning.adoc#best-effort-consistency[Best-effort consistency] section. This means that under certain network partition conditions the counter doesn't provide strong consistency guarantees and can generate duplicate values.
434
+
The `vertx-hazelcast-enterprise` module provides a different implementation of the `io.vertx.core.shareddata.Counter` and `io.vertx.core.shareddata.Lock` data structures. The implementation in `vertx-hazelcast` is based on the IMap data structure and provides guarantees defined in the xref:architecture:data-partitioning.adoc#best-effort-consistency[Best-effort consistency] section—however, this means that under certain network partition conditions, the counter does not provide strong consistency guarantees and can generate duplicate values.
428
435
429
-
The module `vertx-hazelcast-enterprise` uses the CP Subsystem from {enterprise-product-name} to implement the Lock and Counter.
436
+
In contrast, the `vertx-hazelcast-enterprise` module uses the CP Subsystem from {enterprise-product-name} to implement strong consistency for the Lock and Counter.
430
437
431
438
NOTE: For the rest of this tutorial you need to have an {enterprise-product-name} license.
432
439
@@ -441,7 +448,7 @@ Make sure you have the following dependency:
441
448
</dependency>
442
449
----
443
450
444
-
and your XML config contains a valid license key:
451
+
And your XML config contains a valid license key:
445
452
446
453
[source,xml]
447
454
----
@@ -459,3 +466,15 @@ Enable the CP subsystem, and in cluster.xml change the value of the `` property
459
466
460
467
You need to start at least 3 instances for the cluster to form successfully. For complete documentation, see the xref:cp-subsystem:cp-subsystem.adoc[CP Subsystem] section.
461
468
469
+
== Summary
470
+
471
+
In this tutorial, you learned how to add the vertx-hazelcast module and enable distributed session management, as well as how to use the `io.vertx.core.shareddata.Counter` data structure to implement a unique id generator.
472
+
473
+
== Next steps
474
+
475
+
You can dive deeper into the two available modules for integrating Hazelcast with Vert.x:
476
+
477
+
- `io.vertx:vertx-hazelcast`: the open-source module maintained by the Vert.x team.
478
+
- `com.hazelcast:vertx-hazelcast-enterprise`: the enterprise module with advanced features like strong consistency for locks and counters using the CP Subsystem.
479
+
480
+
See: https://hazelcast.com/blog/seamless-integration-with-vert-x-boosting-performance-and-scalability-in-java-applications/[Seamless Integration with Vert.x]
Copy file name to clipboardExpand all lines: docs/modules/integrate/pages/integrate-with-vertx.adoc
+22-20Lines changed: 22 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -2,23 +2,25 @@
2
2
3
3
Vert.x is a reactive application toolkit for creating resource-efficient, concurrent, asynchronous and flexible applications on the JVM.
4
4
5
-
Hazelcast integrates with Vert.x in a form of a cluster manager - the link:https://vertx.io/docs/vertx-hazelcast/java/[Hazelcast Cluster Manager].
5
+
Hazelcast integrates with Vert.x in the form of a cluster manager — the link:https://vertx.io/docs/vertx-hazelcast/java/[Hazelcast Cluster Manager].
6
6
7
+
In Vert.x, a cluster manager is used for various functions including:
7
8
8
-
In Vert.x a cluster manager is used for various functions including:
9
-
10
-
- Discovery and group membership of Vert.x nodes in a cluster
11
-
- Maintaining cluster-wide topic subscriber lists (so we know which nodes are interested in which event bus addresses)
12
-
- Distributed Map support
13
-
- Distributed Locks
14
-
- Distributed Counters
9
+
- Discovery and group membership of Vert.x nodes in a cluster.
10
+
- Maintaining cluster-wide topic subscriber lists (so we know which nodes are interested in which event bus addresses).
11
+
- Distributed Map support.
12
+
- Distributed Locks.
13
+
- Distributed Counters.
15
14
16
15
There are 2 modules to choose from:
17
-
- `io.vertx:vertx-hazelcast` - this module is part of Vert.x and is maintained by the Vert.x team with contributions from Hazelcast developers. This module is licensed under the Apache 2 license.
18
16
19
-
- `com.hazelcast:vertx-hazelcast-enterprise` - this module is built on top of `vertx-hazelcast` and leverages functionality of {enterprise-product-name} to implement some of the cluster manager functionality (e.g. it uses the CP Subsystem to implement strongly consistent `io.vertx.core.shareddata.Lock` and `io.vertx.core.shareddata.Counter`).
17
+
- `io.vertx:vertx-hazelcast` — this module is part of Vert.x and is maintained by the Vert.x team with contributions from Hazelcast developers. This module is licensed under the Apache 2 license.
18
+
19
+
- `com.hazelcast:vertx-hazelcast-enterprise` — this module is built on top of `vertx-hazelcast` and leverages functionality of {enterprise-product-name} to implement some of the cluster manager functionality (e.g. it uses the CP Subsystem to implement strongly consistent Lock and Counter data structures: `io.vertx.core.shareddata.Lock` and `io.vertx.core.shareddata.Counter`).
20
+
21
+
TIP: To learn how to add the vertx-hazelcast module and enable distributed session management, as well as how to implement a unique ID generator, see xref:get-started-with-vertx.adoc[Get started with Vert.x].
20
22
21
-
== Using Vert.x Hazelcast Enterprise Cluster Manager
23
+
== Use Vert.x Hazelcast Enterprise Cluster Manager
22
24
[.enterprise]*Enterprise*
23
25
24
26
To use the Vert.x Hazelcast Enterprise Cluster Manager, add the following dependency to your Vert.x application:
@@ -47,7 +49,7 @@ The dependency is located in the Hazelcast private repository, and you need to a
47
49
</repositories>
48
50
----
49
51
50
-
Alternatively, if you need to use a snapshot version (you should never use a snapshot version in production,
52
+
Alternatively, if you need to use a snapshot version (note, you should never use a snapshot version in production,
51
53
but it might be useful to test if an issue has been fixed):
52
54
53
55
[source,xml]
@@ -73,24 +75,24 @@ For other configuration methods see the link:https://vertx.io/docs/vertx-hazelca
73
75
74
76
=== Versioning and Hazelcast compatibility
75
77
76
-
The Vert.x Hazelcast Enterprise module follows the versioning of Vertx. If you use an `x.y.z` version of Vert.x, you should use an `x.y.z` version of `vertx-hazelcast-enteprise`.
78
+
The Vert.x Hazelcast Enterprise module follows the versioning of Vert.x. If you use an `x.y.z` version of Vert.x, you should use an `x.y.z` version of `vertx-hazelcast-enteprise`.
77
79
78
-
The `vertx-hazelcast-enteprise` module is compatible with all Hazelcast versions supported at the time of the release of the `vertx-hazelcast-enteprise` module unless stated otherwise.
80
+
The `vertx-hazelcast-enteprise` module is compatible with all Hazelcast versions supported at the time of the release of the `vertx-hazelcast-enteprise` module, unless stated otherwise.
79
81
80
82
While older versions may work, such configurations are not supported.
81
83
82
-
== Using Vert.x Hazelcast Cluster Manager
84
+
== Use Vert.x Hazelcast Cluster Manager
83
85
84
-
See the Vert.x Hazelcast Cluster Manager site for reference documentation for the `vertx-hazelcast` module.
86
+
For reference documentation for the `vertx-hazelcast` module, see the Vert.x Hazelcast Cluster Manager site.
85
87
86
88
You can also follow our xref:get-started-with-vertx.adoc[Get started with Vert.x guide].
87
89
88
-
== Using a different Hazelcast version
90
+
== Use a different Hazelcast version
89
91
90
-
Due to Java compatibility reasons the Vert.x Hazelcast module doesn't depend on the latest version of Hazelcast.
91
-
You can change the Hazelcast dependency to any version of Hazelcast you need, e.g. you can change it to Hazelcast 5.2.x for Java 8 compatibilty, or to the latest.
92
+
Due to Java compatibility reasons, the Vert.x Hazelcast module does not depend on the latest version of Hazelcast.
93
+
You can change the Hazelcast dependency to any version of Hazelcast you need.
92
94
93
-
NOTE: The old versions may not be supported by Hazelcast anymore and don't receive any patches.
95
+
NOTE: Older versions may not be supported by Hazelcast or receive any patches.
94
96
95
97
There are multiple ways to replace the transitive dependency. The most reliable way is to exclude the `com.hazelcast:hazelcast` transitive dependency of the `vert-hazelcast` module and add a direct dependency on `com.hazelcast:hazelcast` to the pom.xml of your project.
0 commit comments