{"id":6090,"date":"2018-03-31T08:02:23","date_gmt":"2018-03-31T08:02:23","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6090"},"modified":"2018-03-31T08:02:23","modified_gmt":"2018-03-31T08:02:23","slug":"configuration-with-vert-x","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/configuration-with-vert-x\/","title":{"rendered":"Configuration with Vert.x"},"content":{"rendered":"<p>Obviously you can use whatever configuration code you like to configure your Verticles and\/or Vert.x applications, you might use standard configuration files or a Redis store or whatever. <\/p>\n<p>Vert.x has a single library which allows us to abstract such things and offers different storage formats and code to access different storage mechanisms.<\/p>\n<p>Add the following dependency to your pom.xml<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependency&gt;\r\n   &lt;groupId&gt;io.vertx&lt;\/groupId&gt;\r\n   &lt;artifactId&gt;vertx-config&lt;\/artifactId&gt;\r\n   &lt;version&gt;${vertx.version}&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<p><em>vertx.version in my version is 3.5.0<\/em><\/p>\n<p>We can actually define multiple storage mechanisms for our configuration data and chain them. For example imagine we have some we have configuration on the localhost and other configuration on a remote HTTP server. We can set things up to get properties that are a combination of both locations.<\/p>\n<p><em>Note: If location A has property port set to 8080 but location B (which appears after location A in the chain) has port set to 80 then the last property located in the chain is the one we&#8217;ll see when querying the configuration code for this property. For other properties the result is a combination of those from A and those from B.<\/em><\/p>\n<p>We can also mark configuration sources as optional so, if they do not exist or are down, the chaining will not fail or exception.<\/p>\n<p>Let&#8217;s start with a simple config.properties file stored in \/src\/main\/resources. Here&#8217;s the file (nothing exciting here)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nport=8080\r\n<\/pre>\n<p>We&#8217;ll now create the ConfigStoreOptions for a &#8220;properties&#8221; file, then add it to the store (we&#8217;ll just use a single store for this example) and finally we&#8217;ll retrieve the port property from the configuration retriever&#8230;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ create the options for a properties file store\r\nConfigStoreOptions propertyFile = new ConfigStoreOptions()\r\n   .setType(&quot;file&quot;)\r\n   .setFormat(&quot;properties&quot;)\r\n   .setConfig(new JsonObject().put(&quot;path&quot;, &quot;config.properties&quot;));\r\n\r\n\/\/ add the options to the chain\r\nConfigRetrieverOptions options = new ConfigRetrieverOptions()\r\n   .addStore(propertyFile);\r\n    \/\/ .addStore for other stores here\r\n\r\nConfigRetriever retriever = ConfigRetriever.create(vertx, options);\r\nretriever.getConfig(ar -&gt;\r\n{\r\n   if(ar.succeeded()) {\r\n      JsonObject o  = ar.result();\r\n      int port = o.getInteger(&quot;port&quot;);\r\n      \/\/ use port config\r\n   }\r\n});\r\n<\/pre>\n<p><em>Note: Don&#8217;t forget to set the configuration store options &#8220;path&#8221; to the location and name of your file.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Obviously you can use whatever configuration code you like to configure your Verticles and\/or Vert.x applications, you might use standard configuration files or a Redis store or whatever. Vert.x has a single library which allows us to abstract such things and offers different storage formats and code to access different storage mechanisms. Add the following [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[161,210],"tags":[],"class_list":["post-6090","post","type-post","status-publish","format-standard","hentry","category-java","category-vert-x"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6090"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6090\/revisions"}],"predecessor-version":[{"id":6099,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6090\/revisions\/6099"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}