@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;
  }
  @Test
  public void testPersistenceSubProcess() {
    KieSession service = (KieSession) ctx.getBean("jpaSingleSessionCommandService2");

    int sessionId = service.getId();

    RuleFlowProcessInstance processInstance =
        (RuleFlowProcessInstance) service.startProcess("org.drools.test.ProcessSubProcess");
    log.info("Started process instance {}", processInstance.getId());
    long processInstanceId = processInstance.getId();

    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    final Environment env = (Environment) ctx.getBean("env");
    /*Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set( EnvironmentName.ENTITY_MANAGER_FACTORY,
             ctx.getBean( "myEmf" ) );
    env.set( EnvironmentName.TRANSACTION_MANAGER,
             ctx.getBean( "txManager" ) );
    */
    KieStoreServices kstore = (KieStoreServices) ctx.getBean("kstore1");
    KieBase kbase1 = (KieBase) ctx.getBean("kb_persistence");
    service = kstore.loadKieSession(sessionId, kbase1, null, env);

    processInstance = (RuleFlowProcessInstance) service.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);

    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    SubProcessNodeInstance subProcessNodeInstance =
        (SubProcessNodeInstance) nodeInstances.iterator().next();
    long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    RuleFlowProcessInstance subProcessInstance =
        (RuleFlowProcessInstance) service.getProcessInstance(subProcessInstanceId);
    assertNotNull(subProcessInstance);
    service.dispose();

    service = kstore.loadKieSession(sessionId, kbase1, null, env);
    service.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    service.dispose();

    service = kstore.loadKieSession(sessionId, kbase1, null, env);
    subProcessInstance = (RuleFlowProcessInstance) service.getProcessInstance(subProcessInstanceId);
    assertNull(subProcessInstance);

    processInstance = (RuleFlowProcessInstance) service.getProcessInstance(processInstanceId);
    assertNull(processInstance);
    service.dispose();
  }
  @Test
  public void testPersistenceSubProcess() {
    setUp();

    Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = new SessionConfiguration(properties);

    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    Package pkg = getProcessSubProcess();
    ruleBase.addPackage(pkg);

    SingleSessionCommandService service = new SingleSessionCommandService(ruleBase, config, env);
    int sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    RuleFlowProcessInstance processInstance =
        (RuleFlowProcessInstance) service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());
    long processInstanceId = processInstance.getId();

    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);

    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    SubProcessNodeInstance subProcessNodeInstance =
        (SubProcessNodeInstance) nodeInstances.iterator().next();
    long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    RuleFlowProcessInstance subProcessInstance =
        (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(subProcessInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(subProcessInstance);

    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
  }