Search in sources :

Example 76 with Job

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

the class TenancyTest method testSignalAsyncThroughApiTenancy.

public void testSignalAsyncThroughApiTenancy() {
    // Deploy process both with and without tenant
    repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/api/tenant/TenancyTest.testMultiTenancySignals.bpmn20.xml").deploy();
    repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/api/tenant/TenancyTest.testMultiTenancySignals.bpmn20.xml").tenantId(TEST_TENANT_ID).deploy();
    // Start 4 proc instances for the one with a tenant and 5 for the one without tenant
    runtimeService.startProcessInstanceByKeyAndTenantId("testMtSignalCatch", TEST_TENANT_ID);
    runtimeService.startProcessInstanceByKeyAndTenantId("testMtSignalCatch", TEST_TENANT_ID);
    runtimeService.startProcessInstanceByKeyAndTenantId("testMtSignalCatch", TEST_TENANT_ID);
    runtimeService.startProcessInstanceByKeyAndTenantId("testMtSignalCatch", TEST_TENANT_ID);
    runtimeService.startProcessInstanceByKey("testMtSignalCatch");
    runtimeService.startProcessInstanceByKey("testMtSignalCatch");
    runtimeService.startProcessInstanceByKey("testMtSignalCatch");
    runtimeService.startProcessInstanceByKey("testMtSignalCatch");
    runtimeService.startProcessInstanceByKey("testMtSignalCatch");
    // Verify
    assertEquals(4, taskService.createTaskQuery().taskName("My task").taskTenantId(TEST_TENANT_ID).count());
    assertEquals(5, taskService.createTaskQuery().taskName("My task").taskWithoutTenantId().count());
    // Signal through API (with tenant)
    runtimeService.signalEventReceivedAsyncWithTenantId("The Signal", TEST_TENANT_ID);
    assertEquals(0, taskService.createTaskQuery().taskName("Task after signal").taskTenantId(TEST_TENANT_ID).count());
    assertEquals(0, taskService.createTaskQuery().taskName("Task after signal").taskWithoutTenantId().count());
    for (Job job : managementService.createJobQuery().list()) {
        managementService.executeJob(job.getId());
    }
    assertEquals(4, taskService.createTaskQuery().taskName("Task after signal").taskTenantId(TEST_TENANT_ID).count());
    assertEquals(0, taskService.createTaskQuery().taskName("Task after signal").taskWithoutTenantId().count());
    // Signal through API (without tenant)
    runtimeService.signalEventReceivedAsync("The Signal");
    assertEquals(4, taskService.createTaskQuery().taskName("Task after signal").taskTenantId(TEST_TENANT_ID).count());
    assertEquals(0, taskService.createTaskQuery().taskName("Task after signal").taskWithoutTenantId().count());
    for (Job job : managementService.createJobQuery().list()) {
        managementService.executeJob(job.getId());
    }
    assertEquals(4, taskService.createTaskQuery().taskName("Task after signal").taskTenantId(TEST_TENANT_ID).count());
    assertEquals(5, taskService.createTaskQuery().taskName("Task after signal").taskWithoutTenantId().count());
    // Cleanup
    for (Deployment deployment : repositoryService.createDeploymentQuery().list()) {
        repositoryService.deleteDeployment(deployment.getId(), true);
    }
}
Also used : Deployment(org.activiti.engine.repository.Deployment) Job(org.activiti.engine.runtime.Job)

Example 77 with Job

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

the class AsyncTaskTest method testFailingAsyncServiceTimer.

@Deployment
public void testFailingAsyncServiceTimer() {
    // start process 
    runtimeService.startProcessInstanceByKey("asyncService");
    // now there should be one job in the database, and it is a message
    assertEquals(1, managementService.createJobQuery().count());
    Job job = managementService.createJobQuery().singleResult();
    if (!(job instanceof MessageEntity)) {
        fail("the job must be a message");
    }
    try {
        managementService.executeJob(job.getId());
        fail();
    } catch (Exception e) {
    // exception expected
    }
    // the service failed: the execution is still sitting in the service task:
    Execution execution = runtimeService.createExecutionQuery().singleResult();
    assertNotNull(execution);
    assertEquals("service", runtimeService.getActiveActivityIds(execution.getId()).get(0));
    // there is still a single job because the timer was created in the same transaction as the 
    // service was executed (which rolled back)
    assertEquals(1, managementService.createJobQuery().count());
    runtimeService.deleteProcessInstance(execution.getId(), "dead");
}
Also used : MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity) Execution(org.activiti.engine.runtime.Execution) Job(org.activiti.engine.runtime.Job) Deployment(org.activiti.engine.test.Deployment)

Example 78 with Job

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

the class TimerEventsAndNewVersionDeploymentsTest method testTimerRestoreOnDeploymentDelete2.

public void testTimerRestoreOnDeploymentDelete2() {
    String deploymentId1 = deployTimerProcess();
    // Process has same key
    String deploymentId2 = deployProcessWithoutTimers();
    String deploymentId3 = deployTimerProcess();
    String deploymentId4 = deployProcessWithoutTimers();
    assertTimerJobs(0);
    repositoryService.deleteDeployment(deploymentId3, true);
    assertTimerJobs(0);
    repositoryService.deleteDeployment(deploymentId2, true);
    assertTimerJobs(0);
    repositoryService.deleteDeployment(deploymentId4, true);
    assertTimerJobs(1);
    Job job = managementService.createJobQuery().singleResult();
    assertEquals(repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId1).singleResult().getId(), job.getProcessDefinitionId());
    cleanup(deploymentId1);
}
Also used : Job(org.activiti.engine.runtime.Job)

Example 79 with Job

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

the class DeleteProcessInstanceTest method testNoEndTimeSet.

@Deployment
public void testNoEndTimeSet() {
    //Note that the instance with a Task Type of "user" is being started.
    log.info("Starting an instance of \"Demo Partial Deletion\" with a Task Type of \"user\".");
    //Set the inputs for the first process instance, which we will be able to completely delete.
    Map<String, Object> inputParamsUser = new HashMap<String, Object>();
    inputParamsUser.put("taskType", "user");
    //Start the process instance & ensure it's started.
    ProcessInstance instanceUser = runtimeService.startProcessInstanceByKey("DemoPartialDeletion", inputParamsUser);
    assertNotNull(instanceUser);
    log.info("Process instance (of process model " + instanceUser.getProcessDefinitionId() + ") started with id: " + instanceUser.getId() + ".");
    //Assert that the process instance is active.
    Execution executionUser = runtimeService.createExecutionQuery().processInstanceId(instanceUser.getProcessInstanceId()).singleResult();
    assertFalse(executionUser.isEnded());
    //Assert that a user task is available for claiming.
    Task taskUser = taskService.createTaskQuery().processInstanceId(instanceUser.getProcessInstanceId()).singleResult();
    assertNotNull(taskUser);
    //Delete the process instance.
    runtimeService.deleteProcessInstance(instanceUser.getId(), null);
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        //Retrieve the HistoricProcessInstance and assert that there is an end time.
        HistoricProcessInstance hInstanceUser = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceUser.getId()).singleResult();
        assertNotNull(hInstanceUser.getEndTime());
        log.info("End time for the deleted instance of \"Demo Partial Deletion\" that was started with a Task Type of \"user\": " + hInstanceUser.getEndTime() + ".");
        log.info("Successfully deleted the instance of \"Demo Partial Deletion\" that was started with a Task Type of \"user\".");
    }
    //Note that the instance with a Task Type of "java" is being started.
    log.info("Starting an instance of \"Demo Partial Deletion\" with a Task Type of \"java\".");
    //Set the inputs for the second process instance, which we will NOT be able to completely delete.
    Map<String, Object> inputParamsJava = new HashMap<String, Object>();
    inputParamsJava.put("taskType", "java");
    //Start the process instance & ensure it's started.
    ProcessInstance instanceJava = runtimeService.startProcessInstanceByKey("DemoPartialDeletion", inputParamsJava);
    assertNotNull(instanceJava);
    log.info("Process instance (of process model " + instanceJava.getProcessDefinitionId() + ") started with id: " + instanceJava.getId() + ".");
    //Assert that the process instance is active.
    Execution executionJava = runtimeService.createExecutionQuery().processInstanceId(instanceJava.getProcessInstanceId()).singleResult();
    assertFalse(executionJava.isEnded());
    // Try to execute job 3 times
    Job jobJava = managementService.createJobQuery().processInstanceId(instanceJava.getId()).singleResult();
    assertNotNull(jobJava);
    try {
        managementService.executeJob(jobJava.getId());
        fail("Expected exception");
    } catch (Exception e) {
    // expected
    }
    try {
        managementService.executeJob(jobJava.getId());
        fail("Expected exception");
    } catch (Exception e) {
    // expected
    }
    try {
        managementService.executeJob(jobJava.getId());
        fail("Expected exception");
    } catch (Exception e) {
    // expected
    }
    //Assert that there is a failed job.
    jobJava = managementService.createJobQuery().processInstanceId(instanceJava.getId()).singleResult();
    assertNotNull(jobJava);
    assertEquals(0, jobJava.getRetries());
    //Delete the process instance.
    runtimeService.deleteProcessInstance(instanceJava.getId(), null);
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        //Retrieve the HistoricProcessInstance and assert that there is no end time.
        HistoricProcessInstance hInstanceJava = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceJava.getId()).singleResult();
        assertNotNull(hInstanceJava.getEndTime());
    }
}
Also used : Task(org.activiti.engine.task.Task) Execution(org.activiti.engine.runtime.Execution) HashMap(java.util.HashMap) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Job(org.activiti.engine.runtime.Job) Deployment(org.activiti.engine.test.Deployment)

Example 80 with Job

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

the class JobExecutorCmdExceptionTest method testJobCommandsWith3Exceptions.

public void testJobCommandsWith3Exceptions() {
    tweetExceptionHandler.setExceptionsRemaining(3);
    commandExecutor.execute(new Command<String>() {

        public String execute(CommandContext commandContext) {
            MessageEntity message = createTweetExceptionMessage();
            commandContext.getJobEntityManager().send(message);
            return message.getId();
        }
    });
    Job job = managementService.createJobQuery().singleResult();
    assertEquals(3, job.getRetries());
    try {
        managementService.executeJob(job.getId());
        fail("exception expected");
    } catch (Exception e) {
    // exception expected;
    }
    job = managementService.createJobQuery().singleResult();
    assertEquals(2, job.getRetries());
    try {
        managementService.executeJob(job.getId());
        fail("exception expected");
    } catch (Exception e) {
    // exception expected;
    }
    job = managementService.createJobQuery().singleResult();
    assertEquals(1, job.getRetries());
    try {
        managementService.executeJob(job.getId());
        fail("exception expected");
    } catch (Exception e) {
    // exception expected;
    }
    job = managementService.createJobQuery().singleResult();
    assertEquals(0, job.getRetries());
    managementService.deleteJob(job.getId());
}
Also used : MessageEntity(org.activiti.engine.impl.persistence.entity.MessageEntity) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) Job(org.activiti.engine.runtime.Job)

Aggregations

Job (org.activiti.engine.runtime.Job)110 Deployment (org.activiti.engine.test.Deployment)76 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)55 Task (org.activiti.engine.task.Task)39 Date (java.util.Date)23 Calendar (java.util.Calendar)16 DelegateTask (org.activiti.engine.delegate.DelegateTask)11 HashMap (java.util.HashMap)10 ActivitiEvent (org.activiti.engine.delegate.event.ActivitiEvent)10 JobQuery (org.activiti.engine.runtime.JobQuery)8 ActivitiException (org.activiti.engine.ActivitiException)6 SimpleDateFormat (java.text.SimpleDateFormat)5 ArrayList (java.util.ArrayList)5 DefaultClockImpl (org.activiti.engine.impl.util.DefaultClockImpl)5 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)5 Clock (org.activiti.engine.runtime.Clock)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)4 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)4