Skip to content

Commit

Permalink
[JBPM-10172] Same id used for timers when using per request (#2285) (#…
Browse files Browse the repository at this point in the history
…2286)

* [JBPM-10172] Same id used for timers when using per request

* [JBPM-10172] Fix test without timer name

* [KOGITO-10172] Kris proposal

* [JBPM-10172] Avoiding null pointer if manager is null

* [JBPM-10172] Sonar warnings

* [JBPM-10172] Fixing tests

---------

Co-authored-by: Francisco Javier Tirado Sarti <[email protected]>
Co-authored-by: Gonzalo Muñoz <[email protected]>
  • Loading branch information
3 people authored Jun 2, 2023
1 parent e410a50 commit 3320022
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import org.drools.core.common.InternalKnowledgeRuntime;
Expand Down Expand Up @@ -49,6 +51,8 @@
import org.jbpm.process.instance.ProcessRuntimeImpl;
import org.kie.api.runtime.KieSession;
import org.kie.api.time.SessionClock;
import org.jbpm.workflow.instance.NodeInstanceContainer;
import org.jbpm.workflow.instance.node.TimerNodeInstance;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -61,8 +65,8 @@ public class TimerManager {

private static final Logger logger = LoggerFactory.getLogger(TimerManager.class);

private long timerId = 0;

private long timerId;
private InternalKnowledgeRuntime kruntime;
private TimerService timerService;
private Map<Long, TimerInstance> timers = new ConcurrentHashMap<Long, TimerInstance>();
Expand All @@ -78,7 +82,7 @@ public void registerTimer(final TimerInstance timer, ProcessInstance processInst
try {
kruntime.startOperation();

timer.setId(++timerId);
timer.setId(getTimerId(processInstance));
timer.setProcessInstanceId(processInstance.getId());
timer.setSessionId(((KieSession) kruntime).getIdentifier());
timer.setActivated(new Date());
Expand All @@ -105,6 +109,15 @@ public void registerTimer(final TimerInstance timer, ProcessInstance processInst
}
}

private long getTimerId(ProcessInstance processInstance) {
Object manager = kruntime.getEnvironment().get("RuntimeManager");
if (processInstance instanceof NodeInstanceContainer && manager != null && !manager.getClass().getSimpleName().equals("PerProcessInstanceRuntimeManager")) {
((NodeInstanceContainer)processInstance).getNodeInstances(true).stream().filter(TimerNodeInstance.class::isInstance).map(TimerNodeInstance.class::cast)
.map(TimerNodeInstance::getTimerId).max(Comparator.comparingLong(Long::longValue)).filter(l -> l > timerId).ifPresent(l -> timerId=l);
}
return ++timerId;
}

public void registerTimer(final TimerInstance timer, String processId, Map<String, Object> params) {
try {
kruntime.startOperation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
package org.jbpm.test.functional.timer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.drools.core.command.SingleSessionCommandService;
import org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession;
import org.drools.core.impl.StatefulKnowledgeSessionImpl;
import org.jbpm.process.instance.InternalProcessRuntime;
import org.jbpm.process.instance.command.UpdateTimerCommand;
import org.jbpm.process.instance.timer.TimerInstance;
import org.jbpm.process.instance.timer.TimerManager;
import org.jbpm.test.JbpmTestCase;
import org.jbpm.test.listener.process.DefaultCountDownProcessEventListener;
import org.jbpm.test.listener.process.NodeLeftCountDownProcessEventListener;
Expand Down Expand Up @@ -168,8 +175,14 @@ public void beforeProcessStarted(ProcessStartedEvent event) {
long id = kieSession.startProcess(PROCESS_NAME).getId();
long startTime = System.currentTimeMillis();
Assertions.assertThat(list).isNotEmpty();

//Get the timerId (there is only one)
Collection<TimerInstance> timers = getTimerManager(kieSession).getTimers();
Assertions.assertThat(timers).hasSize(1);
long timerId = timers.iterator().next().getId();

//set delay to 3s
kieSession.execute(new UpdateTimerCommand(id, 1, 3));
kieSession.execute(new UpdateTimerCommand(id, timerId, 3));

listener.waitTillCompleted();
Assertions.assertThat(timerHasFired()).isTrue();
Expand Down Expand Up @@ -321,4 +334,13 @@ public void emptyISOTimerTest() throws Exception {
Assertions.assertThatThrownBy(() -> kieSession.startProcess(ISO_TIMER_NAME, parameters)).hasCauseInstanceOf(IllegalArgumentException.class);

}

private TimerManager getTimerManager(KieSession ksession) {
KieSession internal = ksession;
if (ksession instanceof CommandBasedStatefulKnowledgeSession) {
internal = ((SingleSessionCommandService)((CommandBasedStatefulKnowledgeSession)ksession ).getRunner()).getKieSession();
}

return ((InternalProcessRuntime)((StatefulKnowledgeSessionImpl)internal).getProcessRuntime()).getTimerManager();
}
}

0 comments on commit 3320022

Please sign in to comment.