{"id":12514,"date":"2015-09-07T20:41:00","date_gmt":"2015-09-07T19:41:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=103ec56eef97c67fc5fbc013043b269f"},"modified":"2022-07-28T15:42:04","modified_gmt":"2022-07-28T13:42:04","slug":"groovy-callable-and-executorservice","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2015\/09\/07\/groovy-callable-and-executorservice\/","title":{"rendered":"Groovy, Callable and ExecutorService"},"content":{"rendered":"<p>Suppose you want submit job to <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/ExecutorService.html\">ExecutorService<\/a>.<\/p>\n<h2 id=\"the-baroque-version\">The Baroque version<\/h2>\n<p>You could create a class that implements <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/Callable.html\">Callable<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">class MyJob implements Callable&lt;Integer&gt;{\r\n    @Override\r\n    Integer call() throws Exception {\r\n        return 42\r\n    }\r\n}<\/pre>\n<p>and give it to the executor service:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">def 'submit callable as MyJob object'() {\r\n    expect:\r\n    executorService.submit(new MyJob()).get() == 42\r\n}<\/pre>\n<p>The response is, as expected, 42.<\/p>\n<h2 id=\"map-as-callable-version\">Map as Callable version<\/h2>\n<p>You want to use this job only in one place so why not inline this class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def 'submit callable as map'() {\r\n    expect:\r\n        executorService.submit([call: { 42 }] as Callable).get() == 42\r\n}<\/pre>\n<p>The response is again 42.<\/p>\n<h2 id=\"groovy-closure-version\">Groovy closure version<\/h2>\n<p>Why not use closure instead of map?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">def 'submit callable as closure'(){\r\n    expect:\r\n        executorService.submit { 42 }.get() == 42\r\n}<\/pre>\n<p>The response is &#8230; null.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">Condition not satisfied:\r\nexecutorService.submit { 42 }.get() == 42\r\n|               |             |      |\r\n|               |             |      false\r\n|               |             null\r\n|               java.util.concurrent.FutureTask@21de60b4\r\njava.util.concurrent.Executors$FinalizableDelegatedExecutorService@1700915<\/pre>\n<p>&nbsp;<\/p>\n<p>Why? It is because Groovy treats this closure as <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Runnable.html\">Runnable<\/a>, not <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/Callable.html\">Callable<\/a> and <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/Future.html#get--\">Future#get<\/a> returns null when task is complete.<\/p>\n<h2 id=\"groovy-closure-version-with-cast\">Groovy closure version with cast<\/h2>\n<p>We have to cast our closure before submiting to executor service:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">def 'submit callable as closure with cast'() {\r\n    when:\r\n        int result = executorService.submit({ return 42 } as Callable&lt;Integer&gt;).get()\r\n    then:\r\n        result == 42\r\n}<\/pre>\n<p>The response is, as expected, again 42.<\/p>\n<p>What interesting, the same test with inlined result variable fails&#8230; Strange&#8230; It could be <a href=\"https:\/\/code.google.com\/p\/spock\/\">Spock framework<\/a> error.<\/p>\n<p>Source code is available <a href=\"https:\/\/github.com\/alien11689\/CallableInGroovy\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"Suppose you want submit job to ExecutorService. The Baroque versionYou could create a class that implements Callable:class MyJob implements Callable&lt;Integer&gt;{    @Override    Integer call() throws Exception {        return 42    }}and give it to &#8230;\n","protected":false},"author":54,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[50,411,30],"class_list":{"0":"post-12514","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-groovy","8":"tag-spock","9":"tag-testing"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12514"}],"version-history":[{"count":11,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12514\/revisions"}],"predecessor-version":[{"id":14680,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12514\/revisions\/14680"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}