{"id":6036,"date":"2018-03-25T09:55:39","date_gmt":"2018-03-25T09:55:39","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6036"},"modified":"2018-03-25T09:55:39","modified_gmt":"2018-03-25T09:55:39","slug":"multiple-verticles","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/multiple-verticles\/","title":{"rendered":"Multiple Verticles"},"content":{"rendered":"<p>In my previous post I looked at creating my first Verticle\/Vert.x application. This had a single Verticle acting as a service and running up a server against port 8080. If we therefore assume that by default a Verticle represents a single service then to create multiple service we simply create multi-Verticles.<\/p>\n<p>Let&#8217;s create three Verticle&#8217;s. The first will act as the &#8220;main&#8221; verticle (or run as an application) and it will register\/deploy the Verticle&#8217;s that we&#8217;re using to host our two services, the HelloVerticle and WorldVerticle. <\/p>\n<p>Each service will itself create\/host an HttpServer. Traditionally this would tend to mean we&#8217;d have two ports exposed, one for each service, but Vert.x allows us to create multiple HttpServer&#8217;s on the same port and uses something like &#8220;round robin&#8221; to try to locate a valid route.<\/p>\n<p><em>Check my previous post for the required pom.xml.<\/em><\/p>\n<p>Here&#8217;s MainVerticle.java <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot;\r\n\r\nimport io.vertx.core.AbstractVerticle;\r\nimport io.vertx.ext.web.Router;\r\n\r\npublic class MainVerticle extends AbstractVerticle {\r\n    @Override\r\n    public void start() {\r\n        Router router  = Router.router(vertx);\r\n\r\n        vertx.deployVerticle(new HelloVerticle(router));\r\n        vertx.deployVerticle(new WorldVerticle(router));\r\n    }\r\n}\r\n<\/pre>\n<p>Notice that this Verticle&#8217;s job is to both register the other Verticle&#8217;s and acts as the main entry point to our application.<\/p>\n<p>You do <strong>not<\/strong> need to pass the Router around in this way unless we intend to have multiple routes on the same port. So in this case we&#8217;re creating the router that will be used by both services on the same port.<\/p>\n<p>Let&#8217;s look at the HelloVerticle.java service (both HelloVerticle and WorldVerticle, in this simple example, are basically the same, but I&#8217;ll reproduce all code here anyway).<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot;\r\n\r\nimport io.vertx.core.AbstractVerticle;\r\nimport io.vertx.ext.web.Router;\r\n\r\npublic class HelloVerticle extends AbstractVerticle {\r\n\r\n    private Router router;\r\n\r\n    public HelloVerticle(Router router) {\r\n        this.router = router;\r\n    }\r\n\r\n    @Override\r\n    public void start() {\r\n        router.route(&quot;\/hello&quot;).handler(ctx -&gt; {\r\n            ctx.response()\r\n                    .putHeader(&quot;content-type&quot;, &quot;text\/plain&quot;)\r\n                    .end(&quot;Hello &quot; + ctx.queryParam(&quot;name&quot;));\r\n        });\r\n\r\n        vertx.createHttpServer()\r\n                .requestHandler(router::accept)\r\n                .listen(8080);\r\n\r\n        System.out.println(&quot;HTTP Hello server started on port 8080&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p>There&#8217;s nothing particularly special or different here, compared to my previous posts code. So let&#8217;s look at the WorldVerticle.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot;\r\n\r\nimport io.vertx.core.AbstractVerticle;\r\nimport io.vertx.ext.web.Router;\r\n\r\npublic class WorldVerticle extends AbstractVerticle {\r\n\r\n    private Router router;\r\n\r\n    public WorldVerticle(Router router) {\r\n        this.router = router;\r\n    }\r\n\r\n    @Override\r\n    public void start() {\r\n        router.route(&quot;\/world&quot;).handler(ctx -&gt; {\r\n            ctx.response()\r\n                    .putHeader(&quot;content-type&quot;, &quot;text\/plain&quot;)\r\n                    .end(&quot;World &quot; + ctx.queryParam(&quot;name&quot;));\r\n        });\r\n\r\n        vertx.createHttpServer()\r\n                .requestHandler(router::accept)\r\n                .listen(8080);\r\n\r\n        System.out.println(&quot;HTTP World server started on port 8080&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p>What&#8217;s interesting is that both HelloVerticle and WorldVerticle create the HTTP server on the same port. As stated earlier Vert.x simply handles this for us without a port conflict and because we&#8217;re using a single router across these two Verticle&#8217;s Vert.x can correctly located the service for each of the following URL&#8217;s<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:8080\/hello?name=Hello\r\nhttp:\/\/localhost:8080\/world?name=World\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post I looked at creating my first Verticle\/Vert.x application. This had a single Verticle acting as a service and running up a server against port 8080. If we therefore assume that by default a Verticle represents a single service then to create multiple service we simply create multi-Verticles. Let&#8217;s create three Verticle&#8217;s. [&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-6036","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\/6036","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=6036"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6036\/revisions"}],"predecessor-version":[{"id":6040,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6036\/revisions\/6040"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}