pefernan commented on code in PR #4185:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4185#discussion_r2774093786


##########
api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/ProcessEventDispatcher.java:
##########
@@ -70,41 +70,44 @@ public ProcessInstance<M> dispatch(String trigger, 
DataEvent<D> event) {
             }
             return null;
         }
-
-        // now see if we have a particular one to check
-        Optional<ProcessInstance<M>> processInstance = null;
         // obtain data from the event
         Object data = dataResolver.apply(event);
-        // check correlation key
-        String processInstanceId = resolveCorrelationId(event).orElse(null);
-        processInstance = signalTargetProcessInstance(processInstanceId, 
trigger, data, this::findById);
-        if (processInstance.isPresent()) {
-            LOGGER.debug("sending event to process {} with correlation key {} 
with trigger {} and payload {}", process.id(), processInstanceId, trigger, 
data);
-            return processInstance.get();
+        // check correlation key, if an instance associated to that 
correlation key exist, notify the instance, if it does not exist, ignore the 
vent
+        Optional<String> correlationId = resolveCorrelationId(event);
+        if (correlationId.isPresent()) {
+            return signalTargetProcessInstance(correlationId.orElseThrow(), 
trigger, data, this::findById, "correlation");
         }
-
-        // check processInstanceId
-        processInstanceId = event.getKogitoReferenceId();
-        processInstance = signalTargetProcessInstance(processInstanceId, 
trigger, data, this::findById);
-        if (processInstance.isPresent()) {
-            LOGGER.debug("sending event to process {} with reference key {} 
with trigger {} and payload {}", process.id(), processInstanceId, trigger, 
data);
-            return processInstance.get();
+        // check process reference id, if the id exist, notify the instance, 
if it does not exist, ignore the event
+        String processInstanceId = event.getKogitoReferenceId();
+        if (processInstanceId != null) {
+            return signalTargetProcessInstance(processInstanceId, trigger, 
data, this::findById, "reference");
         }
-
         // check businessKey
         processInstanceId = event.getKogitoBusinessKey();
-        processInstance = signalTargetProcessInstance(processInstanceId, 
trigger, data, this::findByBusinessKey);
-        if (processInstance.isPresent()) {
-            LOGGER.debug("sending event to process {} with business key {} 
with trigger {} and payload {}", process.id(), processInstanceId, trigger, 
data);
-            return processInstance.get();
+        if (processInstanceId != null) {
+            Optional<ProcessInstance<M>> processInstance = 
signalTargetProcessInstance(processInstanceId, trigger, data, 
this::findByBusinessKey);
+            // business key is special case, since it might be used to notify 
a process instance identified by that business key or create a new one 
+            // using that business key
+            return processInstance.isPresent() ? processInstance.orElseThrow() 
: startNewInstance(trigger, event);
         }
-
+        // if we reach this point try to start a new instance if possible 
(this covers start events)
+        ProcessInstance<M> processInstance = startNewInstance(trigger, event);
         // we signal all the processes waiting for trigger (this covers 
intermediate catch events)
         LOGGER.debug("sending event to process {} with trigger {} and payload 
{}", process.id(), trigger, data);
         process.send(SignalFactory.of("Message-" + trigger, data));
+        return processInstance;
+    }
 
-        // try to start a new instance if possible (this covers start events)
-        return startNewInstance(trigger, event);
+    private ProcessInstance<M> signalTargetProcessInstance(String 
processInstanceId, String trigger, Object data, Function<String, 
Optional<ProcessInstance<M>>> findProcessInstance,
+            String messagePart) {
+        Optional<ProcessInstance<M>> processInstance = 
signalTargetProcessInstance(processInstanceId, trigger, data, 
findProcessInstance);
+        if (processInstance.isPresent()) {
+            LOGGER.debug("Event sent to process {} with {} key {} with trigger 
{} and payload {}", process.id(), messagePart, processInstanceId, trigger, 
data);
+            return processInstance.get();
+        } else {
+            LOGGER.warn(" process {} with {} key {} with trigger {} and 
payload {} does not exist, ignoring event", process.id(), messagePart, 
processInstanceId, trigger, data);

Review Comment:
   ```suggestion
               LOGGER.warn("Process {} with {} key {} with trigger {} and 
payload {} does not exist, ignoring event", process.id(), messagePart, 
processInstanceId, trigger, data);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to