Question

I have some processes in jBPM which I test with unit tests (sending events, checking if nodes are triggered, etc).

KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(ResourceFactory.newClassPathResource("process.bpmn"), ResourceType.BPMN2);
knowledgeBase = knowledgeBuilder.newKnowledgeBase();
session = knowledgeBase.newStatefulKnowledgeSession();
....

In some of the processes there are fixed timers (for example 3 weeks). Is there a possibility to manipulate the time jbpm is using so that I can simulate that this period of time is already over?

Btw. I don't want to trigger these notes manually or modify the times in it.

I'm using jbpm 5.4.

Was it helpful?

Solution

One possibility is to use the clock of the session and iterate over all actual running timers.

This will not actually shift time, but can be used to cancel timers, which should be fired in a time span.

Example:

public void shiftTime(long timeToShiftInMs) {
    long targetTime = System.currentTimeMillis() + timeToShiftInMs + 10; // 10ms ahead ...
    JDKTimerService clock = getSession().getSessionClock();

    List<TimerJobInstance> jobs = new ArrayList<>();

    for (TimerJobInstance job : clock.getTimerJobInstances()) { // go through all jobs
        // He keeps already executed timer without nextFirTime
        Date nextFireTime = job.getTrigger().hasNextFireTime();
        if (nextFireTime != null) {
            long jobTime = nextFireTime.getTime();
            if (targetTime > jobTime) { // look if it should be fired after this time
                jobs.add(job);
            }
        }
    }

    for (TimerJobInstance job : jobs) {
        job.getJob().execute(job.getJobContext());
        clock.removeJob(job.getJobHandle());
    }
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top