From 118fc9a2227133da8de45836ca1e481acb719018 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Mon, 18 Sep 2023 18:55:49 +0200 Subject: [PATCH] [JBPM-10187] Handling sessionnotfound in local thread --- .../PerProcessInstanceRuntimeManager.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java b/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java index 7ae3fe4456..02b562b90c 100644 --- a/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java +++ b/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java @@ -16,9 +16,12 @@ package org.jbpm.runtime.manager.impl; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; +import java.util.Set; import org.drools.core.command.SingleSessionCommandService; import org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession; @@ -191,15 +194,33 @@ public void signalEvent(String type, Object event) { // process currently active runtime engines Map currentlyActive = local.get(); + if (currentlyActive != null && !currentlyActive.isEmpty()) { - RuntimeEngine[] activeEngines = currentlyActive.values().toArray(new RuntimeEngine[currentlyActive.size()]); - for (RuntimeEngine engine : activeEngines) { - Context context = ((RuntimeEngineImpl) engine).getContext(); + @SuppressWarnings("unchecked") + Entry activeEngines[] = currentlyActive.entrySet() + .toArray(new Entry[currentlyActive.size()]); + Set enginesToDelete = new HashSet<>(); + for (Entry engine : activeEngines) { + RuntimeEngineImpl engineImpl = (RuntimeEngineImpl) engine.getValue(); + if (engineImpl.isDisposed() || engineImpl.isInvalid()) { + enginesToDelete.add(engine.getKey()); + continue; + } + Context context = engineImpl.getContext(); if (context != null && context instanceof ProcessInstanceIdContext && ((ProcessInstanceIdContext) context).getContextId() != null) { - engine.getKieSession().signalEvent(type, event, ((ProcessInstanceIdContext) context).getContextId()); + try { + engineImpl.getKieSession().signalEvent(type, event, + ((ProcessInstanceIdContext) context).getContextId()); + } catch (SessionNotFoundException ex) { + logger.warn( + "Signal event cannot proceed because of session not found exception {} for engine {}", + ex.getMessage(), engineImpl.getKieSessionId()); + enginesToDelete.add(engine.getKey()); + } } } + enginesToDelete.forEach(currentlyActive::remove); } }