private void waitForTimersToBeExecuted(String workflowInstanceId) throws Exception { String processInstanceId = BPMEngineRegistry.getLocalId(workflowInstanceId); // Job-executor should finish the job, no timers should be available for WF List<Job> timers = null; int numberOfRetries = 5; for (int i=0; i< numberOfRetries; i++) { Thread.sleep(1500); timers = activitiProcessEngine.getManagementService().createJobQuery() .timers() .processInstanceId(processInstanceId) .list(); if (timers.size() == 0) { break; } } if(timers.size() > 0) { fail("There are still timers available for the process: " + processInstanceId); } }
List<Job> timerJobs = managementService.createJobQuery() .processInstanceId(processInstanceId) .timers() .list();
List<Job> timerJobs = managementService.createJobQuery() .processInstanceId(processInstanceId) .timers() .list();
Job timerJob = managementService.createJobQuery().timers().processInstanceId(processInstance.getId()).singleResult(); String globalJobId = BPMEngineRegistry.createGlobalId(ActivitiConstants.ENGINE_ID, timerJob.getId());
/** * This method tests the scenario when an error that is not workflow related is throws while workflow is executing an Async type task like Timer. This error * is logged as ERROR. */ @Test(expected = ActivitiException.class) public void testActivitiReportableError() throws Exception { BpmnModel bpmnModel = getBpmnModelForXmlResource(ACTIVITI_XML_HERD_TIMER_WITH_CLASSPATH); ServiceTask serviceTask = (ServiceTask) bpmnModel.getProcesses().get(0).getFlowElement("servicetask1"); FieldExtension exceptionField = new FieldExtension(); exceptionField.setFieldName("exceptionToThrow"); exceptionField.setExpression("${exceptionToThrow}"); serviceTask.getFieldExtensions().add(exceptionField); jobDefinitionServiceTestHelper.createJobDefinitionForActivitiXml(getActivitiXmlFromBpmnModel(bpmnModel)); List<Parameter> parameters = new ArrayList<>(); Parameter parameter = new Parameter("exceptionToThrow", MockJavaDelegate.EXCEPTION_BPMN_ERROR); parameters.add(parameter); Job job = jobService.createAndStartJob(jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME, parameters)); org.activiti.engine.runtime.Job timer = activitiManagementService.createJobQuery().processInstanceId(job.getId()).timers().singleResult(); if (timer != null) { executeWithoutLogging(Arrays.asList(ActivitiRuntimeHelper.class, TimerExecuteNestedActivityJobHandler.class), () -> { activitiManagementService.executeJob(timer.getId()); }); } }
@Test public void testGetJobIntermediateTimer() throws Exception { jobDefinitionServiceTestHelper.createJobDefinition(ACTIVITI_XML_HERD_INTERMEDIATE_TIMER_WITH_CLASSPATH); Job job = jobService.createAndStartJob(jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME)); String activitiXml = IOUtils.toString(resourceLoader.getResource(ACTIVITI_XML_HERD_INTERMEDIATE_TIMER_WITH_CLASSPATH).getInputStream()); // Job should be waiting at User task. // Get job status Job jobGet = jobService.getJob(job.getId(), true); assertEquals(JobStatusEnum.RUNNING, jobGet.getStatus()); assertNotNull(jobGet.getActivitiJobXml()); assertEquals(activitiXml, jobGet.getActivitiJobXml()); assertTrue(jobGet.getCompletedWorkflowSteps().size() > 0); // Current workflow step will be null assertNull(jobGet.getCurrentWorkflowStep()); org.activiti.engine.runtime.Job timer = activitiManagementService.createJobQuery().processInstanceId(job.getId()).timers().singleResult(); if (timer != null) { activitiManagementService.executeJob(timer.getId()); } // Get the job status again. job should have completed now. jobGet = jobService.getJob(job.getId(), false); assertEquals(JobStatusEnum.COMPLETED, jobGet.getStatus()); assertNull(jobGet.getCurrentWorkflowStep()); }
/** * This method tests the timer execution in a workflow. */ @Test public void testTimerJob() throws Exception { // Create and start the workflow. jobDefinitionServiceTestHelper.createJobDefinition(ACTIVITI_XML_HERD_TIMER_WITH_CLASSPATH); Job job = jobService.createAndStartJob(jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME)); assertNotNull(job); // This workflow would normally automatically start a timer which would eventually complete the workflow, however, since // this test method is running within our own transaction, the timer would never go off since it is run in a different thread which is outside of // our transaction which didn't commit yet. As a result, we need to manually run the timer job to simulate what would happen if the timer // went off by itself. org.activiti.engine.runtime.Job timer = activitiManagementService.createJobQuery().processInstanceId(job.getId()).timers().singleResult(); if (timer != null) { activitiManagementService.executeJob(timer.getId()); } }
/** * This method tests the scenario when an workflow related error is throws while workflow is executing an Async type task like Timer. This error is logged * as WARN. */ @Test(expected = ActivitiException.class) public void testActivitiUnReportableError() throws Exception { BpmnModel bpmnModel = getBpmnModelForXmlResource(ACTIVITI_XML_HERD_TIMER_WITH_CLASSPATH); ServiceTask serviceTask = (ServiceTask) bpmnModel.getProcesses().get(0).getFlowElement("servicetask1"); serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_EXPRESSION); serviceTask.setImplementation("${BeanNotAvailable}"); jobDefinitionServiceTestHelper.createJobDefinitionForActivitiXml(getActivitiXmlFromBpmnModel(bpmnModel)); Job job = jobService.createAndStartJob(jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME, null)); org.activiti.engine.runtime.Job timer = activitiManagementService.createJobQuery().processInstanceId(job.getId()).timers().singleResult(); if (timer != null) { executeWithoutLogging(TimerExecuteNestedActivityJobHandler.class, () -> { activitiManagementService.executeJob(timer.getId()); }); } }