{"id":24552994,"url":"https://github.com/npdeehan/multi-instance-messages","last_synced_at":"2025-04-15T23:14:20.545Z","repository":{"id":53767379,"uuid":"279576115","full_name":"NPDeehan/multi-instance-messages","owner":"NPDeehan","description":"Example on how to use multi-instance messaging with Camunda BPM","archived":false,"fork":false,"pushed_at":"2021-03-15T11:22:16.000Z","size":451,"stargazers_count":5,"open_issues_count":1,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T23:13:59.109Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NPDeehan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-14T12:15:05.000Z","updated_at":"2023-08-15T13:02:49.000Z","dependencies_parsed_at":"2022-09-14T03:30:34.634Z","dependency_job_id":null,"html_url":"https://github.com/NPDeehan/multi-instance-messages","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NPDeehan%2Fmulti-instance-messages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NPDeehan%2Fmulti-instance-messages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NPDeehan%2Fmulti-instance-messages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NPDeehan%2Fmulti-instance-messages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NPDeehan","download_url":"https://codeload.github.com/NPDeehan/multi-instance-messages/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249167448,"owners_count":21223506,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-23T01:33:47.162Z","updated_at":"2025-04-15T23:14:20.527Z","avatar_url":"https://github.com/NPDeehan.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Multi Instance messages between processes (Example)\n\nThis is a simple example which shows how you can start multiple processes with BPMN messages and then waiting for multiple responses back from each of the instances that have been started.\nThis is a Spring Boot project using the Camunda engine in which the logic written in Java and two independent BPMN processes are uses to progress state and facilitate messaging.\n\n\n\n## Doctor Process\n\nThe use case here is where we have a list of doctors, each calling their own TARDIS. This results in a new process being called for each doctor and then the process waits for all TARDIS's to respond for each doctor before continuing.\n\n![DoctorProcess](./src/main/resources/image/DoctorProcess.png)\n\n\nFirst a list is created with all of the doctors and the list is put into the process context.\n\n```java\nList\u003cString\u003e doctors = new ArrayList\u003cString\u003e();\n\ndoctors.add(\"Tennant\");\ndoctors.add(\"Jodie\");\ndoctors.add(\"Capaldi\");\ndoctors.add(\"Smith\");\n\ndelegateExecution.setVariable(\"doctors\", doctors);\n```\n\nThen for each element of the list a message is sent which starts and instance of the TARDIS process.\nEach element of the list is created as a local variable - so it must be taken out of the local context\n\n```java\nString doctor = (String) delegateExecution.getVariableLocal(\"doctor\");\n```\n\nThe message is sent with a payload of ``BussinessKey`` and ``doctor`` in order to ensure we can communicate back to the right message event.\n\n```java\ndelegateExecution.getProcessEngineServices()\n         .getRuntimeService()\n         .createMessageCorrelation(\"NEED_TARDIS\")\n         .setVariable(\"BusKey\", delegateExecution.getBusinessKey())\n         .setVariable(\"doctor\", doctor)\n         .correlate();\n```\n\nAfter all of the messages are sent and the processes have started, the same list of ``doctors`` is used to created a multi sub-process in which we can wait for the responses.\n\nA very important thing to note is how the ``Receive Task`` works. It takes the ``doctor`` variable as an input parameter on the task to ensure that each of the waiting messages are waiting for their own unique return message.\n\n![response Model](./src/main/resources/image/ResponseMessage.png)\n\n## TARDIS process\n\nThis process begins with a message event from the doctor process - it then runs some Java classes before eventually sending a response back to the process that called it.\n\n![Tardis Process](./src/main/resources/image/TardisProcess.png)\n\nThe process runs some code which throws a BPMN error (just for flavor :) ) and then goes on to calculate a time in which it will wait before responding. it'll calculate a time between 5 and 35 seconds.\n\n```Java\n\nRandom timeLord = new Random();\nint waitTime = timeLord.nextInt(30) + 5;\n\ndelegateExecution.setVariable(\"timeyWimey\", \"PT\"+waitTime+\"S\");\n```\n\nAfter the time expires it will use the payload received from the doctor processes i.e. ``BusKey`` and ``doctor`` to send back a message to waiting message catch event.\n\n```Java\nString BusKey = (String) delegateExecution.getVariable(\"BusKey\");\nString doctor = (String) delegateExecution.getVariable(\"doctor\");\n\ndelegateExecution.getProcessEngineServices()\n     .getRuntimeService()\n     .createMessageCorrelation(\"TARDIS\")\n     .processInstanceBusinessKey(BusKey)\n     .localVariableEquals(\"doctor\", doctor)\n     .correlate();\n```\n\nAfter the message is sent successfully, the process ends.\n\n## Message Flow\n\nDespite the two processes being completely independent, we can visualize Communicaiton between the two in this BPMN diagram.\n\n![message flow](./src/main/resources/image/MessageFlow.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnpdeehan%2Fmulti-instance-messages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnpdeehan%2Fmulti-instance-messages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnpdeehan%2Fmulti-instance-messages/lists"}