Sharing objects with all verticles instances











up vote
0
down vote

favorite












My application, an API server, is thought to be organized as follows:




  1. 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 a HttpVerticle.


  2. HttpVerticle is in charge of receiving requests and, based the command xxx in the payload, execute the XxxHandler.handle(...) method.


  3. 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 example LoginHandler.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?










share|improve this question


















  • 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 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















up vote
0
down vote

favorite












My application, an API server, is thought to be organized as follows:




  1. 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 a HttpVerticle.


  2. HttpVerticle is in charge of receiving requests and, based the command xxx in the payload, execute the XxxHandler.handle(...) method.


  3. 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 example LoginHandler.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?










share|improve this question


















  • 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 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













up vote
0
down vote

favorite









up vote
0
down vote

favorite











My application, an API server, is thought to be organized as follows:




  1. 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 a HttpVerticle.


  2. HttpVerticle is in charge of receiving requests and, based the command xxx in the payload, execute the XxxHandler.handle(...) method.


  3. 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 example LoginHandler.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?










share|improve this question













My application, an API server, is thought to be organized as follows:




  1. 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 a HttpVerticle.


  2. HttpVerticle is in charge of receiving requests and, based the command xxx in the payload, execute the XxxHandler.handle(...) method.


  3. 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 example LoginHandler.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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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 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








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












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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python