public void run() {
      try {
        // wait for amount of time timer expires and plus 1s initially
        Thread.sleep(wait * 1000 + 1000);
        long processInstanceId = counter + 1;

        for (int y = 0; y < wait; y++) {
          RuntimeEngine runtime =
              manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
          try {
            testCompleteTaskByProcessInstance(manager, runtime, processInstanceId);
          } catch (Throwable e) {
            if (checkOptimiticLockException(e)) {
              logger.debug("{} retrying for process instance {}", counter, processInstanceId);
              manager.disposeRuntimeEngine(runtime);
              runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
              testRetryCompleteTaskByProcessInstance(manager, runtime, processInstanceId);
            } else {
              throw e;
            }
          }
          manager.disposeRuntimeEngine(runtime);
        }

        completedTask++;
      } catch (Throwable t) {
        t.printStackTrace();
      }
    }
  @Test
  public void testSinglePerProcessInstanceManager() {
    assertNotNull(perProcessInstanceManager);

    RuntimeEngine runtime =
        perProcessInstanceManager.getRuntimeEngine(ProcessInstanceIdContext.get());
    assertNotNull(runtime);
    testProcessStartOnManager(runtime);
    perProcessInstanceManager.disposeRuntimeEngine(runtime);
  }
 public void run() {
   try {
     RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
     testStartProcess(runtime);
     manager.disposeRuntimeEngine(runtime);
     completedStart++;
   } catch (Throwable t) {
     t.printStackTrace();
   }
 }
  /**
   * Test that illustrates that jobs are persisted and survives server restart and as soon as
   * GlobalTimerService is active jobs are fired and it loads and aborts the process instance to
   * illustrate jobs are properly removed when isntance is aborted NOTE: this test is disabled by
   * default as it requires real db (not in memory) and test to be executed separately each with new
   * jvm process
   */
  @Test
  @Ignore
  public void testAbortGlobalTestService() throws Exception {
    SimpleRuntimeEnvironment environment = new DefaultRuntimeEnvironment();

    environment.addAsset(
        ResourceFactory.newClassPathResource("BPMN2-IntermediateCatchEventTimerCycle3.bpmn2"),
        ResourceType.BPMN2);
    environment.addToConfiguration(
        "drools.timerService", "org.jbpm.process.core.timer.impl.RegisteredTimerServiceDelegate");
    RuntimeManager manger =
        RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);

    // build GlobalTimerService instance

    TimerService globalTs = new GlobalTimerService(manger, globalScheduler);
    // and register it in the registry under 'default' key
    TimerServiceRegistry.getInstance().registerTimerService("default", globalTs);
    // prepare listener to assert results
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listener =
        new DefaultProcessEventListener() {

          @Override
          public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
              timerExporations.add(event.getProcessInstance().getId());
            }
          }
        };
    long id = -1;
    Thread.sleep(5000);
    Runtime runtime = manger.getRuntime(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    ksession.addEventListener(listener);

    ksession.abortProcessInstance(id);
    ProcessInstance processInstance = ksession.getProcessInstance(id);
    assertNull(processInstance);
    // let's wait to ensure no more timers are expired and triggered
    Thread.sleep(3000);
    ksession.dispose();
  }