Search in sources :

Example 31 with TimerJobQuery

use of org.activiti.engine.runtime.TimerJobQuery in project Activiti by Activiti.

the class StartTimerEventTest method testVersionUpgradeShouldCancelJobs.

@Deployment
public void testVersionUpgradeShouldCancelJobs() throws Exception {
    processEngineConfiguration.getClock().setCurrentTime(new Date());
    // After process start, there should be timer created
    TimerJobQuery jobQuery = managementService.createTimerJobQuery();
    assertThat(jobQuery.count()).isEqualTo(1);
    // we deploy new process version, with some small change
    String process = new String(IoUtil.readInputStream(getClass().getResourceAsStream("StartTimerEventTest.testVersionUpgradeShouldCancelJobs.bpmn20.xml"), "")).replaceAll("beforeChange", "changed");
    String id = repositoryService.createDeployment().addInputStream("StartTimerEventTest.testVersionUpgradeShouldCancelJobs.bpmn20.xml", new ByteArrayInputStream(process.getBytes())).deploy().getId();
    assertThat(jobQuery.count()).isEqualTo(1);
    moveByMinutes(5);
    waitForJobExecutorOnCondition(10000, 500, new Callable<Boolean>() {

        public Boolean call() throws Exception {
            // we check that correct version was started
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey("startTimerEventExample").singleResult();
            if (processInstance != null) {
                String pi = processInstance.getId();
                List<Execution> executions = runtimeService.createExecutionQuery().processInstanceId(pi).list();
                Execution activityExecution = null;
                for (Execution execution : executions) {
                    if (!execution.getProcessInstanceId().equals(execution.getId())) {
                        activityExecution = execution;
                        break;
                    }
                }
                if (activityExecution != null) {
                    return "changed".equals(activityExecution.getActivityId());
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    });
    assertThat(jobQuery.count()).isEqualTo(1);
    cleanDB();
    repositoryService.deleteDeployment(id, true);
}
Also used : Execution(org.activiti.engine.runtime.Execution) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) List(java.util.List) Date(java.util.Date) TimerJobQuery(org.activiti.engine.runtime.TimerJobQuery) Deployment(org.activiti.engine.test.Deployment)

Example 32 with TimerJobQuery

use of org.activiti.engine.runtime.TimerJobQuery in project Activiti by Activiti.

the class MessageBoundaryEventTest method testSingleBoundaryMessageEventWithBoundaryTimerEvent.

@Deployment
public void testSingleBoundaryMessageEventWithBoundaryTimerEvent() {
    final Date startTime = new Date();
    processEngineConfiguration.getClock().setCurrentTime(startTime);
    runtimeService.startProcessInstanceByKey("process");
    assertThat(runtimeService.createExecutionQuery().count()).isEqualTo(3);
    Execution execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("messageName").singleResult();
    assertThat(execution).isNull();
    // ///////////////////////////////////
    // Verify the first task
    Task userTask = taskService.createTaskQuery().singleResult();
    assertThat(userTask).isNotNull();
    assertThat(userTask.getTaskDefinitionKey()).isEqualTo("task");
    // ///////////////////////////////////
    // Advance the clock to trigger the timer.
    final TimerJobQuery jobQuery = managementService.createTimerJobQuery().processInstanceId(userTask.getProcessInstanceId());
    assertThat(jobQuery.count()).isEqualTo(1);
    // After setting the clock to time '1 hour and 5 seconds', the timer should fire.
    processEngineConfiguration.getClock().setCurrentTime(new Date(startTime.getTime() + ((60 * 60 * 1000) + 5000)));
    waitForJobExecutorOnCondition(5000L, 100L, new Callable<Boolean>() {

        public Boolean call() throws Exception {
            return taskService.createTaskQuery().count() == 2;
        }
    });
    // It is a repeating job, so it will come back.
    assertThat(jobQuery.count()).isEqualTo(1L);
    // ///////////////////////////////////
    // Verify and complete the first task
    userTask = taskService.createTaskQuery().taskDefinitionKey("task").singleResult();
    assertThat(userTask).isNotNull();
    taskService.complete(userTask.getId());
    // ///////////////////////////////////
    // Complete the after timer task
    userTask = taskService.createTaskQuery().taskDefinitionKey("taskTimer").singleResult();
    assertThat(userTask).isNotNull();
    taskService.complete(userTask.getId());
    // Timer job of boundary event of task should be deleted and timer job
    // of task timer boundary event should be created.
    assertThat(jobQuery.count()).isEqualTo(1L);
    // ///////////////////////////////////
    // Verify that the message exists
    userTask = taskService.createTaskQuery().singleResult();
    assertThat(userTask.getTaskDefinitionKey()).isEqualTo("taskAfterTask");
    execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("messageName").singleResult();
    assertThat(execution).isNotNull();
    // ///////////////////////////////////
    // Send the message and verify that we went back to the right spot.
    runtimeService.messageEventReceived("messageName", execution.getId());
    userTask = taskService.createTaskQuery().singleResult();
    assertThat(userTask).isNotNull();
    assertThat(userTask.getTaskDefinitionKey()).isEqualTo("task");
    execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("messageName").singleResult();
    assertThat(execution).isNull();
    // ///////////////////////////////////
    // Complete the first task (again).
    taskService.complete(userTask.getId());
    userTask = taskService.createTaskQuery().singleResult();
    assertThat(userTask).isNotNull();
    assertThat(userTask.getTaskDefinitionKey()).isEqualTo("taskAfterTask");
    execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("messageName").singleResult();
    assertThat(execution).isNotNull();
    // ///////////////////////////////////
    // Verify timer firing.
    // After setting the clock to time '2 hours and 5 seconds', the timer
    // should fire.
    processEngineConfiguration.getClock().setCurrentTime(new Date(startTime.getTime() + ((2 * 60 * 60 * 1000) + 5000)));
    waitForJobExecutorOnCondition(2000L, 100L, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return taskService.createTaskQuery().count() == 2;
        }
    });
    // It is a repeating job, so it will come back.
    assertThat(jobQuery.count()).isEqualTo(1L);
    // After setting the clock to time '3 hours and 5 seconds', the timer should fire again.
    processEngineConfiguration.getClock().setCurrentTime(new Date(startTime.getTime() + ((3 * 60 * 60 * 1000) + 5000)));
    waitForJobExecutorOnCondition(2000L, 100L, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return taskService.createTaskQuery().list().size() == 3;
        }
    });
    // It is a repeating job, so it will come back.
    assertThat(jobQuery.count()).isEqualTo(1L);
    // ///////////////////////////////////
    // Complete the after timer tasks
    final List<Task> tasks = taskService.createTaskQuery().taskDefinitionKey("taskAfterTaskTimer").list();
    assertThat(tasks).hasSize(2);
    taskService.complete(tasks.get(0).getId());
    taskService.complete(tasks.get(1).getId());
    // ///////////////////////////////////
    // Complete the second task
    taskService.complete(userTask.getId());
    // ///////////////////////////////////
    // Complete the third task
    userTask = taskService.createTaskQuery().singleResult();
    assertThat(userTask).isNotNull();
    assertThat(userTask.getTaskDefinitionKey()).isEqualTo("taskAfterTaskAfterTask");
    taskService.complete(userTask.getId());
    // ///////////////////////////////////
    // We should be done at this point
    assertThat(runtimeService.createProcessInstanceQuery().count()).isEqualTo(0);
    execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("messageName").singleResult();
    assertThat(execution).isNull();
}
Also used : Task(org.activiti.engine.task.Task) Execution(org.activiti.engine.runtime.Execution) Date(java.util.Date) TimerJobQuery(org.activiti.engine.runtime.TimerJobQuery) Deployment(org.activiti.engine.test.Deployment)

Example 33 with TimerJobQuery

use of org.activiti.engine.runtime.TimerJobQuery in project Activiti by Activiti.

the class JobExecutorExceptionsTest method testQueryByExceptionWithRealJobExecutor.

@Test
@Deployment(resources = { "org/activiti/engine/test/api/mgmt/ManagementServiceTest.testGetJobExceptionStacktrace.bpmn20.xml" })
public void testQueryByExceptionWithRealJobExecutor() {
    TimerJobQuery query = managementService.createTimerJobQuery().withException();
    assertThat(query.count()).isEqualTo(0);
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("exceptionInJobExecution");
    // Timer is set for 4 hours, so move clock 5 hours
    processEngineConfiguration.getClock().setCurrentTime(new Date(new Date().getTime() + 5 * 60 * 60 * 1000));
    // The execution is waiting in the first usertask. This contains a
    // boundary timer event which we will execute manual for testing purposes.
    JobTestHelper.waitForJobExecutorOnCondition(processEngineConfiguration, 5000L, 100L, new Callable<Boolean>() {

        public Boolean call() throws Exception {
            return managementService.createTimerJobQuery().withException().count() == 1;
        }
    });
    query = managementService.createTimerJobQuery().processInstanceId(processInstance.getId()).withException();
    assertThat(query.count()).isEqualTo(1);
}
Also used : ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Date(java.util.Date) TimerJobQuery(org.activiti.engine.runtime.TimerJobQuery) Test(org.junit.Test) Deployment(org.activiti.engine.test.Deployment)

Example 34 with TimerJobQuery

use of org.activiti.engine.runtime.TimerJobQuery in project Activiti by Activiti.

the class HistoricProcessInstanceQueryAndWithExceptionTest method testQueryWithException.

public void testQueryWithException() throws InterruptedException {
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        ProcessInstance processNoException = runtimeService.startProcessInstanceByKey(PROCESS_DEFINITION_KEY_NO_EXCEPTION);
        HistoricProcessInstanceQuery queryNoException = historyService.createHistoricProcessInstanceQuery();
        assertThat(queryNoException.count()).isEqualTo(1);
        assertThat(queryNoException.list()).hasSize(1);
        assertThat(queryNoException.list().get(0).getId()).isEqualTo(processNoException.getId());
        HistoricProcessInstanceQuery queryWithException = historyService.createHistoricProcessInstanceQuery();
        assertThat(queryWithException.withJobException().count()).isEqualTo(0);
        assertThat(queryWithException.withJobException().list()).hasSize(0);
        ProcessInstance processWithException1 = startProcessInstanceWithFailingJob(PROCESS_DEFINITION_KEY_WITH_EXCEPTION_1);
        TimerJobQuery jobQuery1 = managementService.createTimerJobQuery().processInstanceId(processWithException1.getId());
        assertThat(jobQuery1.withException().count()).isEqualTo(1);
        assertThat(jobQuery1.withException().list()).hasSize(1);
        assertThat(queryWithException.withJobException().count()).isEqualTo(1);
        assertThat(queryWithException.withJobException().list()).hasSize(1);
        assertThat(queryWithException.withJobException().list().get(0).getId()).isEqualTo(processWithException1.getId());
        ProcessInstance processWithException2 = startProcessInstanceWithFailingJob(PROCESS_DEFINITION_KEY_WITH_EXCEPTION_2);
        TimerJobQuery jobQuery2 = managementService.createTimerJobQuery().processInstanceId(processWithException2.getId());
        assertThat(jobQuery2.withException().count()).isEqualTo(2);
        assertThat(jobQuery2.withException().list()).hasSize(2);
        assertThat(queryWithException.withJobException().count()).isEqualTo(2);
        assertThat(queryWithException.withJobException().list()).hasSize(2);
        assertThat(queryWithException.withJobException().processDefinitionKey(PROCESS_DEFINITION_KEY_WITH_EXCEPTION_1).list().get(0).getId()).isEqualTo(processWithException1.getId());
        assertThat(queryWithException.withJobException().processDefinitionKey(PROCESS_DEFINITION_KEY_WITH_EXCEPTION_2).list().get(0).getId()).isEqualTo(processWithException2.getId());
    }
}
Also used : HistoricProcessInstanceQuery(org.activiti.engine.history.HistoricProcessInstanceQuery) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) TimerJobQuery(org.activiti.engine.runtime.TimerJobQuery)

Example 35 with TimerJobQuery

use of org.activiti.engine.runtime.TimerJobQuery in project Activiti by Activiti.

the class JobQueryEscapeClauseTest method testQueryByTenantIdLike.

public void testQueryByTenantIdLike() {
    TimerJobQuery query = managementService.createTimerJobQuery().jobTenantIdLike("%\\%%");
    assertThat(query.singleResult().getTenantId()).isEqualTo("tenant%");
    assertThat(query.list()).hasSize(1);
    assertThat(query.count()).isEqualTo(1);
    query = managementService.createTimerJobQuery().jobTenantIdLike("%\\_%");
    assertThat(query.singleResult().getTenantId()).isEqualTo("tenant_");
    assertThat(query.list()).hasSize(1);
    assertThat(query.count()).isEqualTo(1);
    query = managementService.createTimerJobQuery().jobTenantIdLike("%test%");
    assertThat(query.singleResult().getTenantId()).isEqualTo("test");
    assertThat(query.list()).hasSize(1);
    assertThat(query.count()).isEqualTo(1);
}
Also used : TimerJobQuery(org.activiti.engine.runtime.TimerJobQuery)

Aggregations

TimerJobQuery (org.activiti.engine.runtime.TimerJobQuery)35 Deployment (org.activiti.engine.test.Deployment)20 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)18 Date (java.util.Date)16 Job (org.activiti.engine.runtime.Job)10 JobQuery (org.activiti.engine.runtime.JobQuery)7 Task (org.activiti.engine.task.Task)5 HashMap (java.util.HashMap)3 Execution (org.activiti.engine.runtime.Execution)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)2 ProcessInstanceQuery (org.activiti.engine.runtime.ProcessInstanceQuery)2 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 HistoricProcessInstanceQuery (org.activiti.engine.history.HistoricProcessInstanceQuery)1 Test (org.junit.Test)1