Skip to content

Commit e0a29f4

Browse files
amandalindsayRob-HazelcastJackPGreen
authored
DOC-278 vertx (#1623)
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>
1 parent dc5fa9b commit e0a29f4

File tree

2 files changed

+74
-53
lines changed

2 files changed

+74
-53
lines changed

docs/modules/integrate/pages/get-started-with-vertx.adoc

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
This tutorial helps you integrate Vert.x with Hazelcast and use Hazelcast for distributed session management and other distributed data structures.
44

5-
In this tutorial, you will
5+
== Overview
66

7-
- start with a simple Vert.x Hello World application
8-
- add vertx-hazelcast module and enable distributed session management
9-
- use `io.vertx.core.shareddata.Counter` data structure to implement a unique id generator
7+
In this tutorial, you will learn how to:
8+
9+
- Start with a simple Vert.x Hello World application.
10+
- Add the vertx-hazelcast module and enable distributed session management.
11+
- Use the `io.vertx.core.shareddata.Counter` data structure to implement a unique ID generator.
1012

1113
== Prerequisites
1214

@@ -25,7 +27,7 @@ In this tutorial, you will
2527
$ mvn clean package
2628
----
2729

28-
and start the application using:
30+
Then start the application using:
2931

3032
[source,bash]
3133
----
@@ -41,14 +43,15 @@ Aug 29, 2024 2:22:38 PM io.vertx.launcher.application.VertxApplication
4143
INFO: Succeeded in deploying verticle
4244
----
4345

44-
== Storing Data in Session
46+
== Store data in session
4547

4648
Go to the `MainVerticle.java` file and replace the contents of the start method with the following:
4749

48-
NOTE: This tutorial uses 2-space indentation, which is customary for Vertx projects due to the high number of nested callbacks.
50+
NOTE: This tutorial uses 2-space indentation, which is customary for Vert.x projects due to the high number of nested callbacks.
4951

5052
[source,java]
5153
----
54+
// include::ROOT:example$/vertx/vertxMainVerticleStart.java[] Save for later; keep code sample inline for now
5255
public void start() {
5356
// Create a Router
5457
Router router = router(vertx);
@@ -105,6 +108,8 @@ NOTE: This tutorial uses 2-space indentation, which is customary for Vertx proje
105108
}
106109
----
107110

111+
Next, test the server response:
112+
108113
[source,bash]
109114
----
110115
$ http put localhost:8888 message=Hello\ World!
@@ -139,15 +144,15 @@ content-type: application/json
139144
140145
----
141146

142-
== Distributed Sessions
147+
== Distributed sessions
143148

144-
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:
145150

146151
Add the following method to the `MainVerticle.java` class:
147152

148153
[source,java]
149154
----
150-
private int findFreePort(int from) {
155+
private int findFreePort(int from) {
151156
for (int port = from; port < from + 100; port++) {
152157
try {
153158
new ServerSocket(port).close();
@@ -160,20 +165,20 @@ Add the following method to the `MainVerticle.java` class:
160165
}
161166
----
162167

163-
and use it in the `start` method:
168+
And then use it in the `start` method:
164169

165170
[source,java]
166171
----
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+
...
177182
----
178183

179184
Now, we can start two instances:
@@ -193,7 +198,7 @@ Aug 30, 2024 9:09:47 AM io.vertx.launcher.application.VertxApplication
193198
INFO: Succeeded in deploying verticle
194199
----
195200

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:
197202

198203
[source, bash]
199204
----
@@ -210,7 +215,7 @@ set-cookie: vertx-web.session=00f219c166ca50727d23eaaf9fe54229; Path=/
210215
}
211216
----
212217

213-
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.
214219

215220
[source, bash]
216221
----
@@ -229,12 +234,12 @@ set-cookie: vertx-web.session=a1486c5ed6416972fdc356e4d91d2397; Path=/
229234

230235
We will fix that by using a Hazelcast Cluster Manager. There are two modules that provide Hazelcast Cluster Manager:
231236

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.
233238
- `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.
234239

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.
236241

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.
238243

239244
Add the following dependency to the `pom.xml`:
240245

@@ -263,7 +268,7 @@ to the following:
263268
SessionStore store = ClusteredSessionStore.create(vertx);
264269
----
265270

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.
267272

268273
We also need to provide a Hazelcast configuration file, and create a file cluster.xml in the `src/main/resources` directory:
269274

@@ -325,7 +330,7 @@ Members {size:2, ver:2} [
325330
...
326331
----
327332

328-
and
333+
And:
329334

330335
[source,bash]
331336
----
@@ -375,6 +380,8 @@ content-type: application/json
375380

376381
== Using Counter
377382

383+
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+
378385
Replace this part of the code at the end of the `start()` method:
379386

380387
[source,java]
@@ -404,7 +411,7 @@ context.vertx()
404411
});
405412
----
406413

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.
408415

409416
[source,bash]
410417
----
@@ -422,11 +429,11 @@ set-cookie: vertx-web.session=d9fb4cada5c0fc625089a38f3de13e3c; Path=/
422429
}
423430
----
424431

425-
== CP Subsystem backed Lock and Counter
432+
== CP Subsystem-backed Lock and Counter
426433

427-
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.
428435

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.
430437

431438
NOTE: For the rest of this tutorial you need to have an {enterprise-product-name} license.
432439

@@ -441,7 +448,7 @@ Make sure you have the following dependency:
441448
</dependency>
442449
----
443450

444-
and your XML config contains a valid license key:
451+
And your XML config contains a valid license key:
445452

446453
[source,xml]
447454
----
@@ -459,3 +466,15 @@ Enable the CP subsystem, and in cluster.xml change the value of the `` property
459466

460467
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.
461468

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]

docs/modules/integrate/pages/integrate-with-vertx.adoc

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
Vert.x is a reactive application toolkit for creating resource-efficient, concurrent, asynchronous and flexible applications on the JVM.
44

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].
66

7+
In Vert.x, a cluster manager is used for various functions including:
78

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.
1514
1615
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.
1816

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].
2022

21-
== Using Vert.x Hazelcast Enterprise Cluster Manager
23+
== Use Vert.x Hazelcast Enterprise Cluster Manager
2224
[.enterprise]*Enterprise*
2325

2426
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
4749
</repositories>
4850
----
4951

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,
5153
but it might be useful to test if an issue has been fixed):
5254

5355
[source,xml]
@@ -73,24 +75,24 @@ For other configuration methods see the link:https://vertx.io/docs/vertx-hazelca
7375

7476
=== Versioning and Hazelcast compatibility
7577

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`.
7779

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.
7981

8082
While older versions may work, such configurations are not supported.
8183

82-
== Using Vert.x Hazelcast Cluster Manager
84+
== Use Vert.x Hazelcast Cluster Manager
8385

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.
8587

8688
You can also follow our xref:get-started-with-vertx.adoc[Get started with Vert.x guide].
8789

88-
== Using a different Hazelcast version
90+
== Use a different Hazelcast version
8991

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.
9294

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.
9496

9597
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.
9698

0 commit comments

Comments
 (0)