Example #1
0
  @Override
  public Object execute(Context context) {
    KieSession kieSession = ((KnowledgeCommandContext) context).getKieSession();
    TimerManager tm = getTimerManager(kieSession);

    RuleFlowProcessInstance wfp =
        (RuleFlowProcessInstance) kieSession.getProcessInstance(processInstanceId);

    for (NodeInstance nodeInstance : wfp.getNodeInstances()) {
      if (nodeInstance instanceof TimerNodeInstance) {
        TimerNodeInstance tni = (TimerNodeInstance) nodeInstance;
        if (tni.getNodeName().equals(timerName)) {
          TimerInstance timer = tm.getTimerMap().get(tni.getTimerId());

          tm.cancelTimer(timer.getTimerId());
          TimerInstance newTimer = new TimerInstance();

          if (delay != 0) {
            long diff = System.currentTimeMillis() - timer.getActivated().getTime();
            newTimer.setDelay(delay * 1000 - diff);
          }
          newTimer.setPeriod(period);
          newTimer.setRepeatLimit(repeatLimit);
          newTimer.setTimerId(timer.getTimerId());
          tm.registerTimer(newTimer, wfp);

          tni.internalSetTimerId(newTimer.getId());

          break;
        }
      }
    }
    return null;
  }
  protected TimerInstance createTimerInstance(Timer timer) {
    TimerInstance timerInstance = new TimerInstance();
    KnowledgeRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
      BusinessCalendar businessCalendar =
          (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");

      String delay = resolveVariable(timer.getDelay());

      timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(delay));

      if (timer.getPeriod() == null) {
        timerInstance.setPeriod(0);
      } else {
        String period = resolveVariable(timer.getPeriod());
        timerInstance.setPeriod(businessCalendar.calculateBusinessTimeAsDuration(period));
      }
    } else {
      configureTimerInstance(timer, timerInstance);
    }
    timerInstance.setTimerId(timer.getId());
    return timerInstance;
  }
Example #3
0
  protected TimerInstance createTimerInstance(Timer timer) {
    TimerInstance timerInstance = new TimerInstance();
    KnowledgeRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
    if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null) {
      BusinessCalendar businessCalendar =
          (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar");
      String delay = null;
      switch (timer.getTimeType()) {
        case Timer.TIME_CYCLE:
          if (CronExpression.isValidExpression(timer.getDelay())) {
            timerInstance.setCronExpression(timer.getDelay());
          } else {

            String tempDelay = resolveVariable(timer.getDelay());
            String tempPeriod = resolveVariable(timer.getPeriod());
            if (DateTimeUtils.isRepeatable(tempDelay)) {
              String[] values = DateTimeUtils.parseISORepeatable(tempDelay);
              String tempRepeatLimit = values[0];
              tempDelay = values[1];
              tempPeriod = values[2];

              if (!tempRepeatLimit.isEmpty()) {
                try {
                  int repeatLimit = Integer.parseInt(tempRepeatLimit);
                  if (repeatLimit > -1) {
                    timerInstance.setRepeatLimit(repeatLimit + 1);
                  }
                } catch (NumberFormatException e) {
                  // ignore
                }
              }
            }

            timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(tempDelay));

            if (tempPeriod == null) {
              timerInstance.setPeriod(0);
            } else {
              timerInstance.setPeriod(businessCalendar.calculateBusinessTimeAsDuration(tempPeriod));
            }
          }
          break;
        case Timer.TIME_DURATION:
          delay = resolveVariable(timer.getDelay());

          timerInstance.setDelay(businessCalendar.calculateBusinessTimeAsDuration(delay));
          timerInstance.setPeriod(0);
          break;
        case Timer.TIME_DATE:
          // even though calendar is available concrete date was provided so it shall be used
          configureTimerInstance(timer, timerInstance);
        default:
          break;
      }

    } else {
      configureTimerInstance(timer, timerInstance);
    }
    timerInstance.setTimerId(timer.getId());
    return timerInstance;
  }