Clustering and Shared Data in Vert.x

Afaik you can't share data between different instances of vert.x -- from the documentation

"[...] Such a use case is better solved by providing a shared map structure that can be accessed directly by different verticle instances in the same vert.x instance."

Since "vert.x instance" means "jvm instance" you can't use sharedmap/set between different jvm. You can use the event bus for this.

Edit before others people downvotes: my answer is from 2012, 6 years ago, when this was not possible. Now it is possible as others people already said


With Vert.x 3 - if you configure your Vert.x instances into "clustered mode" (which can be as simple as adding -cluster to the command line of the Vert.x launcher, see here for details), then you can use the SharedData interface to get access to "distributed maps" which allows cluster members to read and write data across the cluster transparently.

Example:

if (vertx.isClustered()) {
    log.info("Using clustered data store");
    vertx.sharedData().<String, MyEntity>getClusterWideMap("entities", 
            res -> {
                AsyncMap<String, MyEntity> dataMap = res.result();
                setDataStore(dataMap);
            });
}

There are options for sharing data among vertx instances on different machines

Option 1.

You could use the Vert.x ClusterManager and it's maps:

ClusterManager clusterManager = ((VertxInternal)vertx).clusterManager();
Map map = clusterManager.getSyncMap("mapName"); // shared distributed map

That map is backed by a Hazelcast IMap and is distributed. This assumes you're running vertx with the -cluster parameter and have configured clustering.

However note that this is internal API and is not generally recommended for production. If you are doing a one time experiment then it could be useful.

Option 2.

You can get access to Hazelcast once vertx is started in clustered mode:

Set<HazelcastInstance> instances = Hazelcast.getAllHazelcastInstances();
HazelcastInstance hz = instances.stream().findFirst().get();
Map map = hz.getMap("mapName"); // shared distributed map