{"action":"created","comment":{"url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/comments/46552082","id":46552082,"diff_hunk":"@@ -0,0 +1,288 @@\n+/*\n+ * Copyright 2015, TeamDev Ltd. All rights reserved.\n+ *\n+ * Redistribution and use in source and/or binary forms, with or without\n+ * modification, must retain the above copyright notice and the following\n+ * disclaimer.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+ * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+package org.spine3.server.procman;\n+\n+import com.google.common.base.Function;\n+import com.google.common.collect.ImmutableSet;\n+import com.google.common.collect.Lists;\n+import com.google.protobuf.Message;\n+import com.google.protobuf.Timestamp;\n+import org.spine3.base.*;\n+import org.spine3.internal.EventHandlerMethod;\n+import org.spine3.server.Entity;\n+import org.spine3.server.internal.CommandHandlerMethod;\n+import org.spine3.util.Classes;\n+import org.spine3.util.Events;\n+import org.spine3.util.MethodMap;\n+\n+import javax.annotation.CheckReturnValue;\n+import javax.annotation.Nullable;\n+import java.lang.reflect.InvocationTargetException;\n+import java.lang.reflect.Method;\n+import java.util.List;\n+import java.util.Set;\n+\n+import static com.google.common.base.Preconditions.checkNotNull;\n+\n+/**\n+ * A central processing unit used to maintain the state of the sequence and determine the next processing step\n+ * based on intermediate results.\n+ * <p>\n+ * An independent component that reacts to domain events in a cross-aggregate, eventually consistent manner.\n+ * Used for externalizing the decisions on the logic flow from the business logic.\n+ * <p>\n+ * Event/command handlers are invoked by the {@link ProcessManagerRepository}\n+ * that manages instances of a process manager class.\n+ * <p>\n+ * There is a common confusion between Process Managers and Sagas.\n+ * See <a href=\"http://kellabyte.com/2012/05/30/clarifying-the-saga-pattern/\">this</a> and\n+ * <a href=\"https://dzone.com/articles/are-sagas-and-workflows-same-t\">this topic</a>\n+ * to understand the difference between them.\n+ *\n+ * @param <I> the type of the process manager IDs\n+ * @param <M> the type of the process manager state\n+ * @see <a href=\"http://www.enterpriseintegrationpatterns.com/patterns/messaging/ProcessManager.html\">Process Manager Pattern</a>\n+ * @see <a href=\"https://msdn.microsoft.com/en-us/library/jj591569.aspx\">CQRS Journey Guide: A Saga on Sagas</a>\n+ * @author Alexander Litus\n+ */\n+public abstract class ProcessManager<I, M extends Message> extends Entity<I, M> {\n+\n+    /**\n+     * Keeps initialization state of the process manager.\n+     */\n+    private volatile boolean initialized = false;\n+\n+    /**\n+     * The map of command handler methods for this process manager.\n+     *\n+     * @see Registry\n+     */\n+    private MethodMap commandHandlers;\n+\n+    /**\n+     * The map of event handlers for this process manager.\n+     *\n+     * @see Registry\n+     */\n+    private MethodMap eventHandlers;\n+\n+    protected ProcessManager(I id) {\n+        super(id);\n+    }\n+\n+    /**\n+     * Performs initialization of the instance and registers this class of process managers\n+     * in the {@link Registry} if it is not registered yet.\n+     */\n+    private void init() {\n+        if (!this.initialized) {\n+            final Registry registry = Registry.getInstance();\n+            final Class<? extends ProcessManager> pmClass = getClass();\n+            if (!registry.contains(pmClass)) {\n+                registry.register(pmClass);\n+            }\n+            commandHandlers = registry.getCommandHandlers(pmClass);\n+            eventHandlers = registry.getEventHandlers(pmClass);\n+            this.initialized = true;\n+        }\n+    }\n+\n+    /**\n+     * Dispatches a command to the command handler method of the process manager.\n+     *\n+     * @param command the command to be executed on the process manager\n+     * @param context of the command\n+     * @throws InvocationTargetException if an exception occurs during command dispatching\n+     */\n+    protected List<EventRecord> dispatchCommand(Message command, CommandContext context) throws InvocationTargetException {\n+        checkNotNull(command, \"command is null\");","path":"core-java/src/main/java/org/spine3/server/procman/ProcessManager.java","position":116,"original_position":116,"commit_id":"046f0765f30571b20cb880fcb5feab7ea10fc362","original_commit_id":"046f0765f30571b20cb880fcb5feab7ea10fc362","user":{"login":"AlexanderLitus","id":8298183,"avatar_url":"https://avatars.githubusercontent.com/u/8298183?v=3","gravatar_id":"","url":"https://api.github.com/users/AlexanderLitus","html_url":"https://github.com/AlexanderLitus","followers_url":"https://api.github.com/users/AlexanderLitus/followers","following_url":"https://api.github.com/users/AlexanderLitus/following{/other_user}","gists_url":"https://api.github.com/users/AlexanderLitus/gists{/gist_id}","starred_url":"https://api.github.com/users/AlexanderLitus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlexanderLitus/subscriptions","organizations_url":"https://api.github.com/users/AlexanderLitus/orgs","repos_url":"https://api.github.com/users/AlexanderLitus/repos","events_url":"https://api.github.com/users/AlexanderLitus/events{/privacy}","received_events_url":"https://api.github.com/users/AlexanderLitus/received_events","type":"User","site_admin":false},"body":"OK, I've added this only to avoid duplicate string literal inspection warning.","created_at":"2015-12-03T13:46:02Z","updated_at":"2015-12-03T13:46:02Z","html_url":"https://github.com/SpineEventEngine/core-java/pull/54#discussion_r46552082","pull_request_url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54","_links":{"self":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/comments/46552082"},"html":{"href":"https://github.com/SpineEventEngine/core-java/pull/54#discussion_r46552082"},"pull_request":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54"}}},"pull_request":{"url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54","id":52246957,"html_url":"https://github.com/SpineEventEngine/core-java/pull/54","diff_url":"https://github.com/SpineEventEngine/core-java/pull/54.diff","patch_url":"https://github.com/SpineEventEngine/core-java/pull/54.patch","issue_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues/54","number":54,"state":"open","locked":false,"title":"Abstract base classes for process managers and process manager repositories, sample, tests","user":{"login":"AlexanderLitus","id":8298183,"avatar_url":"https://avatars.githubusercontent.com/u/8298183?v=3","gravatar_id":"","url":"https://api.github.com/users/AlexanderLitus","html_url":"https://github.com/AlexanderLitus","followers_url":"https://api.github.com/users/AlexanderLitus/followers","following_url":"https://api.github.com/users/AlexanderLitus/following{/other_user}","gists_url":"https://api.github.com/users/AlexanderLitus/gists{/gist_id}","starred_url":"https://api.github.com/users/AlexanderLitus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlexanderLitus/subscriptions","organizations_url":"https://api.github.com/users/AlexanderLitus/orgs","repos_url":"https://api.github.com/users/AlexanderLitus/repos","events_url":"https://api.github.com/users/AlexanderLitus/events{/privacy}","received_events_url":"https://api.github.com/users/AlexanderLitus/received_events","type":"User","site_admin":false},"body":"I decided to cover sagas with tests after the code review (tested via sample).","created_at":"2015-12-01T11:35:45Z","updated_at":"2015-12-03T13:46:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"47bdd40afea68e24da3ab0bd9158a11b89c6fe66","assignee":{"login":"alexander-yevsyukov","id":3116444,"avatar_url":"https://avatars.githubusercontent.com/u/3116444?v=3","gravatar_id":"","url":"https://api.github.com/users/alexander-yevsyukov","html_url":"https://github.com/alexander-yevsyukov","followers_url":"https://api.github.com/users/alexander-yevsyukov/followers","following_url":"https://api.github.com/users/alexander-yevsyukov/following{/other_user}","gists_url":"https://api.github.com/users/alexander-yevsyukov/gists{/gist_id}","starred_url":"https://api.github.com/users/alexander-yevsyukov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexander-yevsyukov/subscriptions","organizations_url":"https://api.github.com/users/alexander-yevsyukov/orgs","repos_url":"https://api.github.com/users/alexander-yevsyukov/repos","events_url":"https://api.github.com/users/alexander-yevsyukov/events{/privacy}","received_events_url":"https://api.github.com/users/alexander-yevsyukov/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54/commits","review_comments_url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54/comments","review_comment_url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues/54/comments","statuses_url":"https://api.github.com/repos/SpineEventEngine/core-java/statuses/046f0765f30571b20cb880fcb5feab7ea10fc362","head":{"label":"SpineEventEngine:process-manager-registry","ref":"process-manager-registry","sha":"046f0765f30571b20cb880fcb5feab7ea10fc362","user":{"login":"SpineEventEngine","id":12821626,"avatar_url":"https://avatars.githubusercontent.com/u/12821626?v=3","gravatar_id":"","url":"https://api.github.com/users/SpineEventEngine","html_url":"https://github.com/SpineEventEngine","followers_url":"https://api.github.com/users/SpineEventEngine/followers","following_url":"https://api.github.com/users/SpineEventEngine/following{/other_user}","gists_url":"https://api.github.com/users/SpineEventEngine/gists{/gist_id}","starred_url":"https://api.github.com/users/SpineEventEngine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpineEventEngine/subscriptions","organizations_url":"https://api.github.com/users/SpineEventEngine/orgs","repos_url":"https://api.github.com/users/SpineEventEngine/repos","events_url":"https://api.github.com/users/SpineEventEngine/events{/privacy}","received_events_url":"https://api.github.com/users/SpineEventEngine/received_events","type":"Organization","site_admin":false},"repo":{"id":38311906,"name":"core-java","full_name":"SpineEventEngine/core-java","owner":{"login":"SpineEventEngine","id":12821626,"avatar_url":"https://avatars.githubusercontent.com/u/12821626?v=3","gravatar_id":"","url":"https://api.github.com/users/SpineEventEngine","html_url":"https://github.com/SpineEventEngine","followers_url":"https://api.github.com/users/SpineEventEngine/followers","following_url":"https://api.github.com/users/SpineEventEngine/following{/other_user}","gists_url":"https://api.github.com/users/SpineEventEngine/gists{/gist_id}","starred_url":"https://api.github.com/users/SpineEventEngine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpineEventEngine/subscriptions","organizations_url":"https://api.github.com/users/SpineEventEngine/orgs","repos_url":"https://api.github.com/users/SpineEventEngine/repos","events_url":"https://api.github.com/users/SpineEventEngine/events{/privacy}","received_events_url":"https://api.github.com/users/SpineEventEngine/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SpineEventEngine/core-java","description":"The Java implementation of the framework core.","fork":false,"url":"https://api.github.com/repos/SpineEventEngine/core-java","forks_url":"https://api.github.com/repos/SpineEventEngine/core-java/forks","keys_url":"https://api.github.com/repos/SpineEventEngine/core-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SpineEventEngine/core-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SpineEventEngine/core-java/teams","hooks_url":"https://api.github.com/repos/SpineEventEngine/core-java/hooks","issue_events_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues/events{/number}","events_url":"https://api.github.com/repos/SpineEventEngine/core-java/events","assignees_url":"https://api.github.com/repos/SpineEventEngine/core-java/assignees{/user}","branches_url":"https://api.github.com/repos/SpineEventEngine/core-java/branches{/branch}","tags_url":"https://api.github.com/repos/SpineEventEngine/core-java/tags","blobs_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SpineEventEngine/core-java/statuses/{sha}","languages_url":"https://api.github.com/repos/SpineEventEngine/core-java/languages","stargazers_url":"https://api.github.com/repos/SpineEventEngine/core-java/stargazers","contributors_url":"https://api.github.com/repos/SpineEventEngine/core-java/contributors","subscribers_url":"https://api.github.com/repos/SpineEventEngine/core-java/subscribers","subscription_url":"https://api.github.com/repos/SpineEventEngine/core-java/subscription","commits_url":"https://api.github.com/repos/SpineEventEngine/core-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/SpineEventEngine/core-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/SpineEventEngine/core-java/contents/{+path}","compare_url":"https://api.github.com/repos/SpineEventEngine/core-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SpineEventEngine/core-java/merges","archive_url":"https://api.github.com/repos/SpineEventEngine/core-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SpineEventEngine/core-java/downloads","issues_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues{/number}","pulls_url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls{/number}","milestones_url":"https://api.github.com/repos/SpineEventEngine/core-java/milestones{/number}","notifications_url":"https://api.github.com/repos/SpineEventEngine/core-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SpineEventEngine/core-java/labels{/name}","releases_url":"https://api.github.com/repos/SpineEventEngine/core-java/releases{/id}","created_at":"2015-06-30T13:50:58Z","updated_at":"2015-06-30T14:51:16Z","pushed_at":"2015-12-03T13:42:29Z","git_url":"git://github.com/SpineEventEngine/core-java.git","ssh_url":"git@github.com:SpineEventEngine/core-java.git","clone_url":"https://github.com/SpineEventEngine/core-java.git","svn_url":"https://github.com/SpineEventEngine/core-java","homepage":null,"size":2133,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"SpineEventEngine:master","ref":"master","sha":"f0ad77d2267167cabd498ab3f32196c2482acaa6","user":{"login":"SpineEventEngine","id":12821626,"avatar_url":"https://avatars.githubusercontent.com/u/12821626?v=3","gravatar_id":"","url":"https://api.github.com/users/SpineEventEngine","html_url":"https://github.com/SpineEventEngine","followers_url":"https://api.github.com/users/SpineEventEngine/followers","following_url":"https://api.github.com/users/SpineEventEngine/following{/other_user}","gists_url":"https://api.github.com/users/SpineEventEngine/gists{/gist_id}","starred_url":"https://api.github.com/users/SpineEventEngine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpineEventEngine/subscriptions","organizations_url":"https://api.github.com/users/SpineEventEngine/orgs","repos_url":"https://api.github.com/users/SpineEventEngine/repos","events_url":"https://api.github.com/users/SpineEventEngine/events{/privacy}","received_events_url":"https://api.github.com/users/SpineEventEngine/received_events","type":"Organization","site_admin":false},"repo":{"id":38311906,"name":"core-java","full_name":"SpineEventEngine/core-java","owner":{"login":"SpineEventEngine","id":12821626,"avatar_url":"https://avatars.githubusercontent.com/u/12821626?v=3","gravatar_id":"","url":"https://api.github.com/users/SpineEventEngine","html_url":"https://github.com/SpineEventEngine","followers_url":"https://api.github.com/users/SpineEventEngine/followers","following_url":"https://api.github.com/users/SpineEventEngine/following{/other_user}","gists_url":"https://api.github.com/users/SpineEventEngine/gists{/gist_id}","starred_url":"https://api.github.com/users/SpineEventEngine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SpineEventEngine/subscriptions","organizations_url":"https://api.github.com/users/SpineEventEngine/orgs","repos_url":"https://api.github.com/users/SpineEventEngine/repos","events_url":"https://api.github.com/users/SpineEventEngine/events{/privacy}","received_events_url":"https://api.github.com/users/SpineEventEngine/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SpineEventEngine/core-java","description":"The Java implementation of the framework core.","fork":false,"url":"https://api.github.com/repos/SpineEventEngine/core-java","forks_url":"https://api.github.com/repos/SpineEventEngine/core-java/forks","keys_url":"https://api.github.com/repos/SpineEventEngine/core-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SpineEventEngine/core-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SpineEventEngine/core-java/teams","hooks_url":"https://api.github.com/repos/SpineEventEngine/core-java/hooks","issue_events_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues/events{/number}","events_url":"https://api.github.com/repos/SpineEventEngine/core-java/events","assignees_url":"https://api.github.com/repos/SpineEventEngine/core-java/assignees{/user}","branches_url":"https://api.github.com/repos/SpineEventEngine/core-java/branches{/branch}","tags_url":"https://api.github.com/repos/SpineEventEngine/core-java/tags","blobs_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SpineEventEngine/core-java/statuses/{sha}","languages_url":"https://api.github.com/repos/SpineEventEngine/core-java/languages","stargazers_url":"https://api.github.com/repos/SpineEventEngine/core-java/stargazers","contributors_url":"https://api.github.com/repos/SpineEventEngine/core-java/contributors","subscribers_url":"https://api.github.com/repos/SpineEventEngine/core-java/subscribers","subscription_url":"https://api.github.com/repos/SpineEventEngine/core-java/subscription","commits_url":"https://api.github.com/repos/SpineEventEngine/core-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/SpineEventEngine/core-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/SpineEventEngine/core-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/SpineEventEngine/core-java/contents/{+path}","compare_url":"https://api.github.com/repos/SpineEventEngine/core-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SpineEventEngine/core-java/merges","archive_url":"https://api.github.com/repos/SpineEventEngine/core-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SpineEventEngine/core-java/downloads","issues_url":"https://api.github.com/repos/SpineEventEngine/core-java/issues{/number}","pulls_url":"https://api.github.com/repos/SpineEventEngine/core-java/pulls{/number}","milestones_url":"https://api.github.com/repos/SpineEventEngine/core-java/milestones{/number}","notifications_url":"https://api.github.com/repos/SpineEventEngine/core-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SpineEventEngine/core-java/labels{/name}","releases_url":"https://api.github.com/repos/SpineEventEngine/core-java/releases{/id}","created_at":"2015-06-30T13:50:58Z","updated_at":"2015-06-30T14:51:16Z","pushed_at":"2015-12-03T13:42:29Z","git_url":"git://github.com/SpineEventEngine/core-java.git","ssh_url":"git@github.com:SpineEventEngine/core-java.git","clone_url":"https://github.com/SpineEventEngine/core-java.git","svn_url":"https://github.com/SpineEventEngine/core-java","homepage":null,"size":2133,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54"},"html":{"href":"https://github.com/SpineEventEngine/core-java/pull/54"},"issue":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/issues/54"},"comments":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/issues/54/comments"},"review_comments":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54/comments"},"review_comment":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/pulls/54/commits"},"statuses":{"href":"https://api.github.com/repos/SpineEventEngine/core-java/statuses/046f0765f30571b20cb880fcb5feab7ea10fc362"}}}}