{"id":6100,"date":"2018-04-11T20:48:35","date_gmt":"2018-04-11T20:48:35","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6100"},"modified":"2018-04-11T20:48:35","modified_gmt":"2018-04-11T20:48:35","slug":"vert-x-futures","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/vert-x-futures\/","title":{"rendered":"Vert.x futures"},"content":{"rendered":"<p>In previous examples of implementations of AbstractVerticle classes I&#8217;ve used <em>start<\/em> and <em>stop<\/em> methods which take no arguments, there&#8217;s actually asynchronous versions of these methods which support the Vert.x Future class. <\/p>\n<p>For example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class FutureVerticle extends AbstractVerticle {\r\n   @Override\r\n   public void start(Future&lt;Void&gt; future) {\r\n   }\r\n\r\n   @Override\r\n   public void stop(Future&lt;Void&gt; future) {\r\n   }\r\n}\r\n<\/pre>\n<p>Let&#8217;s take a look at how our <em>start<\/em> method might change to use futures.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Override\r\npublic void start(Future&lt;Void&gt; future) {\r\n\r\n   \/\/ routing and\/or initialization code\r\n\r\n   vertx.createHttpServer()\r\n      .requestHandler(router::accept)\r\n      .listen(port, l -&gt;\r\n      {\r\n         if(l.succeeded()) {\r\n            future.succeeded();\r\n         }\r\n         else {\r\n            future.fail(l.cause());\r\n         }\r\n      });\r\n)\r\n<\/pre>\n<p>In this example we simply set the state of the future to success or failure and in the case of the failure supply a Throwable as the argument to the <em>fail<\/em> method.<\/p>\n<p><strong>Using the Future in our own code<\/strong><\/p>\n<p>Obviously the Future class may be used outside of the <em>start<\/em> and <em>stop<\/em> methods, so let&#8217;s take a look at creating and using a Future.<\/p>\n<p>To create a future simply use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nFuture&lt;Record&gt; f = Future.future();\r\n<\/pre>\n<p>in this case we&#8217;re creating a Future which takes a Record. We can now supply our own AsyncResult<T> handler to handle the future on completion, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nFuture&lt;Record&gt; f = Future.future();\r\n\r\nf.setHandler(ar -&gt;\r\n{\r\n   if(r.succeeded() {\r\n      \/\/ do something with result\r\n   }\r\n});\r\n<\/pre>\n<p>Many of the Vertx methods (like <em>listen<\/em> in the earlier code) supply overloads with an AsyncResult callback. We can pass a future as a callback using the method <em>completer <\/em> and supply a handler via the future. For example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nFuture&lt;HttpServer&gt; f = Future.future();\r\nf.setHandler(l -&gt;\r\n{\r\n   if(l.succeeded()) {\r\n      future.succeeded();\r\n   }\r\n   else {\r\n      future.fail(l.cause());\r\n   }\r\n});\r\n\r\nvertx.createHttpServer()\r\n   .requestHandler(router::accept)\r\n   .listen(port, f.completer());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In previous examples of implementations of AbstractVerticle classes I&#8217;ve used start and stop methods which take no arguments, there&#8217;s actually asynchronous versions of these methods which support the Vert.x Future class. For example public class FutureVerticle extends AbstractVerticle { @Override public void start(Future&lt;Void&gt; future) { } @Override public void stop(Future&lt;Void&gt; future) { } } Let&#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-6100","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\/6100","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=6100"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6100\/revisions"}],"predecessor-version":[{"id":6166,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6100\/revisions\/6166"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}