Sharing objects with all verticles instances
up vote
0
down vote
favorite
My application, an API server, is thought to be organized as follows:
MainVerticle
is called on startup and should create all necessary objects for the application to work. Mainly a mongoDB pool of connections (MongoClient.createShared(...)
) and a global configuration object available instance-wide. It also starts the HTTP Listener, several instances of aHttpVerticle
.HttpVerticle
is in charge of receiving requests and, based the commandxxx
in the payload, execute theXxxHandler.handle(...)
method.Most of the
XxxHandler.handle(...)
methods will need to access the database. In addition, some others will also deploy additional verticles with parameters from the global conf. For exampleLoginHandler.handle(...)
will deploy a verticle to keep user state while he's connected and this verticle will be undeployed when the user logs out.
I can't figure out how to get the global configuration object while being in XxxHandler.handle(...)
or in a "sub"-verticle. Same for the mongo client.
Q1: For configuration data, I tried to use SharedData
. In `MainVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
lm.put("var", "val");
and in `HttpVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
log.debug("var={}", lm.get("var"));
but the log output is var=null
.... What am I doing wrong ?
Q2: Besides this basic example with a <String, String>
map type, what if the value is a mutable Object like JsonObject
which actually is what I would need ?
Q3: Finally how to make the instance of the mongo client available to all verticles?
vert.x vertx-verticle
add a comment |
up vote
0
down vote
favorite
My application, an API server, is thought to be organized as follows:
MainVerticle
is called on startup and should create all necessary objects for the application to work. Mainly a mongoDB pool of connections (MongoClient.createShared(...)
) and a global configuration object available instance-wide. It also starts the HTTP Listener, several instances of aHttpVerticle
.HttpVerticle
is in charge of receiving requests and, based the commandxxx
in the payload, execute theXxxHandler.handle(...)
method.Most of the
XxxHandler.handle(...)
methods will need to access the database. In addition, some others will also deploy additional verticles with parameters from the global conf. For exampleLoginHandler.handle(...)
will deploy a verticle to keep user state while he's connected and this verticle will be undeployed when the user logs out.
I can't figure out how to get the global configuration object while being in XxxHandler.handle(...)
or in a "sub"-verticle. Same for the mongo client.
Q1: For configuration data, I tried to use SharedData
. In `MainVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
lm.put("var", "val");
and in `HttpVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
log.debug("var={}", lm.get("var"));
but the log output is var=null
.... What am I doing wrong ?
Q2: Besides this basic example with a <String, String>
map type, what if the value is a mutable Object like JsonObject
which actually is what I would need ?
Q3: Finally how to make the instance of the mongo client available to all verticles?
vert.x vertx-verticle
1
LocalMap is the way to share objects between verticles in the same Vert.x instance. These objects should be immutable (but JsonObject will be copied). Not sure what doesn't work for you without more snippets.
– tsegismont
Nov 17 at 8:44
As for Mongo, if all your verticles usecreateShared
with the same pool name, they will all use the same pool (created just once).
– tsegismont
Nov 17 at 8:44
Myvar
had becomevqr
and I spent a couple of hours trying to figure out what was going on... And thanks for the advice on MongoClient.
– mszmurlo
Nov 18 at 4:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My application, an API server, is thought to be organized as follows:
MainVerticle
is called on startup and should create all necessary objects for the application to work. Mainly a mongoDB pool of connections (MongoClient.createShared(...)
) and a global configuration object available instance-wide. It also starts the HTTP Listener, several instances of aHttpVerticle
.HttpVerticle
is in charge of receiving requests and, based the commandxxx
in the payload, execute theXxxHandler.handle(...)
method.Most of the
XxxHandler.handle(...)
methods will need to access the database. In addition, some others will also deploy additional verticles with parameters from the global conf. For exampleLoginHandler.handle(...)
will deploy a verticle to keep user state while he's connected and this verticle will be undeployed when the user logs out.
I can't figure out how to get the global configuration object while being in XxxHandler.handle(...)
or in a "sub"-verticle. Same for the mongo client.
Q1: For configuration data, I tried to use SharedData
. In `MainVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
lm.put("var", "val");
and in `HttpVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
log.debug("var={}", lm.get("var"));
but the log output is var=null
.... What am I doing wrong ?
Q2: Besides this basic example with a <String, String>
map type, what if the value is a mutable Object like JsonObject
which actually is what I would need ?
Q3: Finally how to make the instance of the mongo client available to all verticles?
vert.x vertx-verticle
My application, an API server, is thought to be organized as follows:
MainVerticle
is called on startup and should create all necessary objects for the application to work. Mainly a mongoDB pool of connections (MongoClient.createShared(...)
) and a global configuration object available instance-wide. It also starts the HTTP Listener, several instances of aHttpVerticle
.HttpVerticle
is in charge of receiving requests and, based the commandxxx
in the payload, execute theXxxHandler.handle(...)
method.Most of the
XxxHandler.handle(...)
methods will need to access the database. In addition, some others will also deploy additional verticles with parameters from the global conf. For exampleLoginHandler.handle(...)
will deploy a verticle to keep user state while he's connected and this verticle will be undeployed when the user logs out.
I can't figure out how to get the global configuration object while being in XxxHandler.handle(...)
or in a "sub"-verticle. Same for the mongo client.
Q1: For configuration data, I tried to use SharedData
. In `MainVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
lm.put("var", "val");
and in `HttpVerticle.start() I have:
LocalMap<String, String> lm = vertx.sharedData().getLocalMap("conf");
log.debug("var={}", lm.get("var"));
but the log output is var=null
.... What am I doing wrong ?
Q2: Besides this basic example with a <String, String>
map type, what if the value is a mutable Object like JsonObject
which actually is what I would need ?
Q3: Finally how to make the instance of the mongo client available to all verticles?
vert.x vertx-verticle
vert.x vertx-verticle
asked Nov 11 at 5:18
mszmurlo
450412
450412
1
LocalMap is the way to share objects between verticles in the same Vert.x instance. These objects should be immutable (but JsonObject will be copied). Not sure what doesn't work for you without more snippets.
– tsegismont
Nov 17 at 8:44
As for Mongo, if all your verticles usecreateShared
with the same pool name, they will all use the same pool (created just once).
– tsegismont
Nov 17 at 8:44
Myvar
had becomevqr
and I spent a couple of hours trying to figure out what was going on... And thanks for the advice on MongoClient.
– mszmurlo
Nov 18 at 4:00
add a comment |
1
LocalMap is the way to share objects between verticles in the same Vert.x instance. These objects should be immutable (but JsonObject will be copied). Not sure what doesn't work for you without more snippets.
– tsegismont
Nov 17 at 8:44
As for Mongo, if all your verticles usecreateShared
with the same pool name, they will all use the same pool (created just once).
– tsegismont
Nov 17 at 8:44
Myvar
had becomevqr
and I spent a couple of hours trying to figure out what was going on... And thanks for the advice on MongoClient.
– mszmurlo
Nov 18 at 4:00
1
1
LocalMap is the way to share objects between verticles in the same Vert.x instance. These objects should be immutable (but JsonObject will be copied). Not sure what doesn't work for you without more snippets.
– tsegismont
Nov 17 at 8:44
LocalMap is the way to share objects between verticles in the same Vert.x instance. These objects should be immutable (but JsonObject will be copied). Not sure what doesn't work for you without more snippets.
– tsegismont
Nov 17 at 8:44
As for Mongo, if all your verticles use
createShared
with the same pool name, they will all use the same pool (created just once).– tsegismont
Nov 17 at 8:44
As for Mongo, if all your verticles use
createShared
with the same pool name, they will all use the same pool (created just once).– tsegismont
Nov 17 at 8:44
My
var
had become vqr
and I spent a couple of hours trying to figure out what was going on... And thanks for the advice on MongoClient.– mszmurlo
Nov 18 at 4:00
My
var
had become vqr
and I spent a couple of hours trying to figure out what was going on... And thanks for the advice on MongoClient.– mszmurlo
Nov 18 at 4:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Instead of getLocalMap()
you should be using getClusterWideMap()
. Then you should be able to operate on shared data accross the whole cluster and not just in one verticle.
Be aware that the shared operations are async and the code might look like (code in Groovy):
vertx.sharedData().getClusterWideMap( 'your-name' ){ AsyncResult<AsyncMap<String,String>> res ->
if( res.succeeded() )
res.result().put( 'var', 'val', { log.info "put succeeded: ${it.succeeded()}" } )
}
You should be able to use any Serializable
objects in your map.
I don't really know what I did wrong (misspelling ?) but I actually rewrote the same lines and theLocalMap
works fine across all verticles running in the samevertx
instance. I actually don't want to have this data shared across the cluster: its configurations which may not be the sames on different nodes.
– mszmurlo
Nov 12 at 16:55
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Instead of getLocalMap()
you should be using getClusterWideMap()
. Then you should be able to operate on shared data accross the whole cluster and not just in one verticle.
Be aware that the shared operations are async and the code might look like (code in Groovy):
vertx.sharedData().getClusterWideMap( 'your-name' ){ AsyncResult<AsyncMap<String,String>> res ->
if( res.succeeded() )
res.result().put( 'var', 'val', { log.info "put succeeded: ${it.succeeded()}" } )
}
You should be able to use any Serializable
objects in your map.
I don't really know what I did wrong (misspelling ?) but I actually rewrote the same lines and theLocalMap
works fine across all verticles running in the samevertx
instance. I actually don't want to have this data shared across the cluster: its configurations which may not be the sames on different nodes.
– mszmurlo
Nov 12 at 16:55
add a comment |
up vote
0
down vote
Instead of getLocalMap()
you should be using getClusterWideMap()
. Then you should be able to operate on shared data accross the whole cluster and not just in one verticle.
Be aware that the shared operations are async and the code might look like (code in Groovy):
vertx.sharedData().getClusterWideMap( 'your-name' ){ AsyncResult<AsyncMap<String,String>> res ->
if( res.succeeded() )
res.result().put( 'var', 'val', { log.info "put succeeded: ${it.succeeded()}" } )
}
You should be able to use any Serializable
objects in your map.
I don't really know what I did wrong (misspelling ?) but I actually rewrote the same lines and theLocalMap
works fine across all verticles running in the samevertx
instance. I actually don't want to have this data shared across the cluster: its configurations which may not be the sames on different nodes.
– mszmurlo
Nov 12 at 16:55
add a comment |
up vote
0
down vote
up vote
0
down vote
Instead of getLocalMap()
you should be using getClusterWideMap()
. Then you should be able to operate on shared data accross the whole cluster and not just in one verticle.
Be aware that the shared operations are async and the code might look like (code in Groovy):
vertx.sharedData().getClusterWideMap( 'your-name' ){ AsyncResult<AsyncMap<String,String>> res ->
if( res.succeeded() )
res.result().put( 'var', 'val', { log.info "put succeeded: ${it.succeeded()}" } )
}
You should be able to use any Serializable
objects in your map.
Instead of getLocalMap()
you should be using getClusterWideMap()
. Then you should be able to operate on shared data accross the whole cluster and not just in one verticle.
Be aware that the shared operations are async and the code might look like (code in Groovy):
vertx.sharedData().getClusterWideMap( 'your-name' ){ AsyncResult<AsyncMap<String,String>> res ->
if( res.succeeded() )
res.result().put( 'var', 'val', { log.info "put succeeded: ${it.succeeded()}" } )
}
You should be able to use any Serializable
objects in your map.
answered Nov 11 at 13:12
injecteer
10.3k22254
10.3k22254
I don't really know what I did wrong (misspelling ?) but I actually rewrote the same lines and theLocalMap
works fine across all verticles running in the samevertx
instance. I actually don't want to have this data shared across the cluster: its configurations which may not be the sames on different nodes.
– mszmurlo
Nov 12 at 16:55
add a comment |
I don't really know what I did wrong (misspelling ?) but I actually rewrote the same lines and theLocalMap
works fine across all verticles running in the samevertx
instance. I actually don't want to have this data shared across the cluster: its configurations which may not be the sames on different nodes.
– mszmurlo
Nov 12 at 16:55
I don't really know what I did wrong (misspelling ?) but I actually rewrote the same lines and the
LocalMap
works fine across all verticles running in the same vertx
instance. I actually don't want to have this data shared across the cluster: its configurations which may not be the sames on different nodes.– mszmurlo
Nov 12 at 16:55
I don't really know what I did wrong (misspelling ?) but I actually rewrote the same lines and the
LocalMap
works fine across all verticles running in the same vertx
instance. I actually don't want to have this data shared across the cluster: its configurations which may not be the sames on different nodes.– mszmurlo
Nov 12 at 16:55
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246053%2fsharing-objects-with-all-verticles-instances%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
LocalMap is the way to share objects between verticles in the same Vert.x instance. These objects should be immutable (but JsonObject will be copied). Not sure what doesn't work for you without more snippets.
– tsegismont
Nov 17 at 8:44
As for Mongo, if all your verticles use
createShared
with the same pool name, they will all use the same pool (created just once).– tsegismont
Nov 17 at 8:44
My
var
had becomevqr
and I spent a couple of hours trying to figure out what was going on... And thanks for the advice on MongoClient.– mszmurlo
Nov 18 at 4:00