{"id":1232,"date":"2012-08-04T20:29:18","date_gmt":"2012-08-04T18:29:18","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=1232"},"modified":"2015-01-31T13:11:31","modified_gmt":"2015-01-31T13:11:31","slug":"design-pattern-the-asynchronous-dispatcher","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=1232","title":{"rendered":"Design Pattern: the Asynchronous Dispatcher"},"content":{"rendered":"<p>Today we&#8217;ll revisit a common design pattern, the <strong>Observer<\/strong>, and one of its derivative for multi-threaded processing: the <strong>Asynchronous Dispatcher<\/strong>.<\/p>\n<p><!--more--><\/p>\n<h1>I Observer pattern<\/h1>\n<p>The classical <strong>Observer<\/strong> pattern features 2 actors: the subject, called <strong>Observable<\/strong> and one or more <strong>Observers<\/strong><\/p>\n<p>The classical UML diagram for such a pattern is:<\/p>\n<p><a href=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/observer.png\"><img loading=\"lazy\" src=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/observer.png\" alt=\"Observer Pattern Diagram\" title=\"Observer Pattern Diagram\" width=\"500\" height=\"207\" class=\"aligncenter size-full wp-image-1580\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/observer.png 500w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/observer-300x124.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><em>Original picture credits: http:\/\/en.wikipedia.org\/wiki\/File:Observer.svg<\/em><\/p>\n<p> This pattern is quite simple and powerfull, the only requirement is that the subject (<strong>Observable<\/strong>) being observed should:<\/p>\n<ul>\n<li>manage the registration\/removal of its <strong>Observers<\/strong><\/li>\n<li>be able to detect any state change and notify its <strong>Observers<\/strong><\/li>\n<\/ul>\n<p>We are here in a <strong>push model<\/strong> where state changes are pushed to the <strong>Observers<\/strong>. The latter are just passive event listeners.<\/p>\n<p>This pattern is suitable for one subject having many <strong>Observers<\/strong>. If there are more than one subjecs that should be monitored, the <strong>Observers<\/strong> registration should be done on each of them.<\/p>\n<p>&nbsp;<\/p>\n<h1>II The Dispatcher<\/h1>\n<p>To relieve the limitations of the <strong>Observer<\/strong> pattern. We can look at the <strong>Dispatcher<\/strong> pattern. <\/p>\n<p><a href=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/dispatcher1.png\"><img loading=\"lazy\" src=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/dispatcher1.png\" alt=\"Dispatcher\" title=\"Dispatcher\" width=\"435\" height=\"375\" class=\"aligncenter size-full wp-image-1585\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/dispatcher1.png 435w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/dispatcher1-300x259.png 300w\" sizes=\"(max-width: 435px) 100vw, 435px\" \/><\/a><\/p>\n<p>This pattern introduces a third actor, the <strong>Dispatcher<\/strong>. It is responsible for:<\/p>\n<ul>\n<li>managing registration of <strong>EventManagers<\/strong> (equivalent to the <strong>Observers<\/strong>)<\/li>\n<li>dispatching the event to all <strong>EventManagers<\/strong> <\/li>\n<\/ul>\n<p>Please notice that we define an enum for the event type (<strong>EventTypeEnum<\/strong>). Indeed there is one list of <strong>EventManagers<\/strong> for each distinct <strong>EventTypeEnum<\/strong>.<\/p>\n<p> The routing based on the <strong>EventTypeEnum<\/strong> is done in the <em>dispatch(EventTypeEnum eventType, Subject object)<\/em> method body.<\/p>\n<pre class=\"brush: java; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic void dispatch(EventTypeEnum event, Subject object)\r\n{\r\n\tswitch (event) {\r\n\t\tcase SAVE:\r\n\t\t\tfor(EventManager manager: this.saveEventManagers)\r\n\t\t\t{\r\n\t\t\t\tmanager.executeEvent(event,object);\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\tcase UPDATE:\r\n\t\t\tfor(EventManager manager: this.updateEventManagers)\r\n\t\t\t{\r\n\t\t\t\tmanager.executeEvent(event,object);\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\tcase DELETE:\r\n\t\t\tfor(EventManager manager: this.deleteEventManagers)\r\n\t\t\t{\r\n\t\t\t\tmanager.executeEvent(event,object);\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\tdefault:\r\n\t\t\tlogger.error(&quot;Unknow EventType of type &quot; + event.name());\r\n        }\r\n\r\n}\r\n<\/pre>\n<p> The <em>executeEvent(EventTypeEnum event, Subject object)<\/em> method body in <strong>EventManager<\/strong> is quite straightforward<\/p>\n<pre class=\"brush: java; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic void executeEvent(EventTypeEnum event, Subject object) {\r\n\t...\r\n\t\/\/ Processing here\r\n\t...\r\n}\r\n<\/pre>\n<p>Alternatively it is possible to delegate the routing of <strong>EventTypeEnum<\/strong> processing to each <strong>EventManager<\/strong>.<\/p>\n<p>In this case, the <strong>Dispatcher<\/strong> only needs a single list of all <strong>EventManagers<\/strong>.<\/p>\n<p><a href=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/dispatcher_alternative.png\"><img loading=\"lazy\" src=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/dispatcher_alternative.png\" alt=\"Dispatcher_Alternative\" title=\"Dispatcher_Alternative\" width=\"435\" height=\"336\" class=\"aligncenter size-full wp-image-1590\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/dispatcher_alternative.png 435w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/dispatcher_alternative-300x232.png 300w\" sizes=\"(max-width: 435px) 100vw, 435px\" \/><\/a><\/p>\n<p>Now, in each <strong>EventManager<\/strong> <em>executeEvent(EventTypeEnum event, Subject object)<\/em> method body, the routing is done:<\/p>\n<pre class=\"brush: java; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic void executeEvent(EventTypeEnum event, Subject object) {\r\n\tswitch (event) {\r\n\t\tcase SAVE:\r\n\t\t\t\/\/ Processing here\r\n\t\t\tbreak;\r\n\t\tcase UPDATE, DELETE:\r\n\t\t\t\/\/ Do nothing\r\n\t\t\tbreak;\r\n\t\tdefault:\r\n\t\t\tlogger.error(&quot;Unknow EventType of type &quot; + event.name());\r\n        }\r\n}\r\n<\/pre>\n<p>The only drawback of this pattern is that if you introduces a new <strong>EventType<\/strong>, you will need to change the <strong>Dispatcher<\/strong> code (for the routing) or each existing <strong>EventManager<\/strong> code (if routing is done at <strong>EventManager<\/strong> level).<\/p>\n<p>&nbsp;<\/p>\n<h1>III Asynchronous Dispatcher<\/h1>\n<p>In real projects, some processing can take time. With the <strong>Dispatcher<\/strong> pattern, the main thread is blocked until all <strong>EventManagers<\/strong> finish their task.<\/p>\n<p>If would be interesting if we could delegate the processing to a separate thread. There comes the <strong>Asynchronous Dispatcher<\/strong> pattern.<\/p>\n<p><a href=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/asyncdispatcher1.png\"><img loading=\"lazy\" src=\"http:\/\/doanduyhai.files.wordpress.com\/2012\/08\/asyncdispatcher1.png\" alt=\"AsyncDispatcher\" title=\"AsyncDispatcher\" width=\"521\" height=\"467\" class=\"aligncenter size-full wp-image-1596\" srcset=\"https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/asyncdispatcher1.png 521w, https:\/\/www.doanduyhai.com\/blog\/wp-content\/uploads\/2012\/08\/asyncdispatcher1-300x269.png 300w\" sizes=\"(max-width: 521px) 100vw, 521px\" \/><\/a><\/p>\n<p> Here we rely on a <strong>ThreadPoolTaskExecutor<\/strong> in each <strong>EventManager<\/strong> to delegate the work to a new thread. Please notice that the dispatching task is done in the main thread. The new thread delegation is done at the last step in each <strong>EventManager<\/strong>.<\/p>\n<p> <strong>One important point to take care when working in a multi-threaded context is concurrency issues<\/strong>. Indeed when we pass the <strong>Subject<\/strong> object to a new thread for processing, the main thread resumes its processing flow and the same <strong>Subject<\/strong> object may be changed in this main thread.<\/p>\n<p> Either you ensure that the processing flow in the main thread never change the Subject object, or you make a deep copy. It is quite expensive but there is no other way around to guard agains concurrent modifications.<\/p>\n<p> Below is the modified <em>dispatch(EventTypeEnum eventType, Subject object, Object&#8230;args)<\/em> method<\/p>\n<pre class=\"brush: java; highlight: [3]; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic void dispatch(EventTypeEnum event, Subject object, Object...args)\r\n{\r\n\tSubject copy = ObjectCloner.deepCopy(object);\r\n\tfor(EventManager manager: this.eventManagers)\r\n\t{\r\n\t\tmanager.executeEvent(event,copy);\r\n\t}\r\n}\r\n<\/pre>\n<p>At <strong>line 3<\/strong>, the Subject object is copied and passed to the manager instead of the original.<\/p>\n<blockquote><p>Notice: <font color=\"red\"><strong>it is strongly recommended not to use JPA entities as Subject object<\/strong><\/font>. Indeed making a deep copy of an JPA entity with its proxy and maybe its un-initialized collections is clearly not a good idea. It is better to pass the technical ID of the object as parameter and reload entirely the object in the new thread.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p> You have probably noticed that existing methods now have an additional <strong>Object &#8230; args<\/strong> vararg. These extra arguments are usefull to pass extra parameters for the processing. It also serves to pass technical ID of JPA entities to be reloaded in the new thread, if any.<\/p>\n<p> Now let&#8217;s look at the new <em>executeEvent(EventTypeEnum event, Subject object, Object&#8230;args)<\/em> method body.<\/p>\n<pre class=\"brush: java; highlight: [23,28]; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic class AsyncTaskManager {\r\n\r\n    private ThreadPoolTaskExecutor taskExecutor;\r\n\r\n    private MyService myService;\r\n\r\n    @Override\r\n    public void executeEvent(EventTypeEnum event, Subject object, Object... args) {\r\n        switch (event) {\r\n            case SAVE:\r\n                this.saveSubject(object,args);\r\n                break;\r\n            case UPDATE,DELETE:\r\n                \/\/ Do nothing\r\n                break;\r\n            default:\r\n                logger.error(&quot;Unknow EventTypeEnum of type &quot; + event.name());\r\n        }\r\n    }\r\n\r\n private void executeEvent(Subject object, Object... args) {\r\n\r\n\tthis.taskExecutor.execute(new Runnable() {\r\n\r\n\t\t@Override\r\n\t\tpublic void run() {\r\n\t\t\ttry {\r\n\t\t\t\tAsyncTaskManager.this.myService.save(object);\r\n\t\t\t} catch (Exception ex) {\r\n\t\t\t\tlogger.error(ex.getMessage(), ex);\r\n\t\t\t}\r\n\t\t}\r\n\t});\r\n<\/pre>\n<p>At <strong>line 23<\/strong>, we create a new <strong>Runnable<\/strong> object and pass it to the thread executor from the thread pool. At <strong>line 28<\/strong>, please notice the special reference to the enclosing class (<em>AsyncTaskManager<\/em>.<strong>this<\/strong>) to access the <strong>MyService<\/strong> service.<\/p>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;ll revisit a common design pattern, the Observer, and one of its derivative for multi-threaded processing: the Asynchronous Dispatcher.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1232"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1232"}],"version-history":[{"count":2,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1232\/revisions"}],"predecessor-version":[{"id":1752,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1232\/revisions\/1752"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}