4 Replies Latest reply on Oct 28, 2011 11:57 AM by rrpeterson

    Change a Timer length/period at runtime possible?


      Hi guys,


      I'm working on allowing updating/migrating of processes.  I've got things mostly working thanks to the examples given by the documentation.  Here's an example of my test:


      Old Process:



      New Process:


      As you can tell, the new process simply adds an additional email where only the timer existed before.  I disconnect the processInstance, update the node instance IDs, set the new process definition onto the old processInstance, then reconnect the processInstance.


      The new email gets sent, and execution continues to the new timer and eventually to the end state node.  The only trouble I'm having is the time defined for the original timer must elapse before execution continues to the next newly defined node. 


      Is it possible to change this timer's duration during the processInstance disconnect() (while I'm re-assigning the nodeInstance Ids)? 


      If not, is there a way to tell the engine to skip over the previous NodeInstance rather than continuing to fire it?


      Thanks for any suggestions!

        • 1. Re: Change a Timer length/period at runtime possible?

          I forgot to mention:  The first process has the timer as a placeholder of 90 days, with the idea that the process instance will be updated at a later time and more functionality added as needed.


          Would it make more sense to implement this differently?  Rather than completely replacing the timer with a new node for instance?

          • 2. Re: Change a Timer length/period at runtime possible?

            I think I've found how to programmatically change a timer's delay/period:


            With the NodeInstance reference, it's possible to check if it's an instanceof TimerNodeInstance, and if so it can be cast to a TimerNodeInstance that you can use .getTimer() to then get/set the delay/period/etc.


            Even though this would work, I'm still not convinced it's the "best" way.



            Nevermind, this doesn't seem to work.  Even though I can change the delay (make it 5s for example), it still seems to last the original duration.  Is there any way to "trigger" a timer so it thinks its done?

            • 3. Re: Change a Timer length/period at runtime possible?

              After getting some help from krisv (thanks again btw!) I think I'm closer, but still not quite able to get this to work.


              I've tried canceling the old timer (canceling portion works great), but setting up the new timer seems to give issues.

              The route I'm trying is:

              ((TimerNodeInstance) nodeInstance).cancel();


              // create timerInstance

              TimerInstance timerInstance = new TimerInstance();

              timerInstance.setDelay(TimeUtils.parseTimeString("5s")); // hard-code to X seconds

              timerInstance.setPeriod(0); // hard-code to a period of 0


              then I register the new timerInstance with the processInstance:

              ((InternalProcessRuntime)((WorkflowProcessInstance) nodeInstanceContainer).getKnowledgeRuntime().getProcessRuntime()).getTimerManager().registerTimer(timerInstance, (org.jbpm.process.instance.ProcessInstance) nodeInstanceContainer);


              Lastly I set the timerInstanceId into the TimerNodeInstance:

              ((TimerNodeInstance) nodeInstance).internalSetTimerId(timerInstance.getId());


              My trouble is the new timer event never fires.  The old one is definitely canceled, if I set it for a short value (like 5 seconds) it doesn't fire after cancellation.  But it's like the new timer is missing something, as it never fires either no matter what delay value is set. 



              I've also tried modifying the IntervalTrigger time of the original timer, but this change doesn't seem to get picked up either.


              Both of these I've done while the processInstance.disconnect() was called, and processInstance.reconnect() is called afterwards.


              Thanks for any help/suggestions!

              • 4. Re: Change a Timer length/period at runtime possible?

                A bit more advice from krisv and I was able to get this to work:


                After migrating to the new process, I cancel the old timer, then once the node ids are re-mapped (same as happens in the WorkflowProcessInstanceUpgrader), and the processInstance is reconnected, I call the "to" trigger for the original node (I keep a reference to the active timer when mapping the node ids from the old process to the new):


                originalTimerNodeInstance.trigger(null, instance.getTimerNode().getTo().getToType());


                This "clears" the original process timer, and allows execution to continue in the newly defined nodes in the new process.


                Thanks again krisv for your help!