Search in sources :

Example 91 with Job

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

the class IntermediateTimerEventRepeatWithEndTest method testRepeatWithEnd.

@Deployment
public void testRepeatWithEnd() throws Throwable {
    Calendar calendar = Calendar.getInstance();
    Date baseTime = calendar.getTime();
    //expect to stop boundary jobs after 20 minutes
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    calendar.setTime(baseTime);
    calendar.add(Calendar.HOUR, 2);
    //expect to wait after competing task B for 1 hour even I set the end date for 2 hours (the expression will trigger the execution)
    DateTime dt = new DateTime(calendar.getTime());
    String dateStr1 = fmt.print(dt);
    calendar.setTime(baseTime);
    calendar.add(Calendar.HOUR, 1);
    calendar.add(Calendar.MINUTE, 30);
    //expect to wait after competing task B for 1 hour and 30 minutes (the end date will be reached, the expression will not be considered)
    dt = new DateTime(calendar.getTime());
    String dateStr2 = fmt.print(dt);
    //reset the timer
    Calendar nextTimeCal = Calendar.getInstance();
    nextTimeCal.setTime(baseTime);
    processEngineConfiguration.getClock().setCurrentTime(nextTimeCal.getTime());
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("repeatWithEnd");
    runtimeService.setVariable(processInstance.getId(), "EndDateForCatch1", dateStr1);
    runtimeService.setVariable(processInstance.getId(), "EndDateForCatch2", dateStr2);
    List<Task> tasks = taskService.createTaskQuery().list();
    assertEquals(1, tasks.size());
    tasks = taskService.createTaskQuery().list();
    assertEquals(1, tasks.size());
    Task task = tasks.get(0);
    assertEquals("Task A", task.getName());
    //Test Timer Catch Intermediate Events after completing Task B (endDate not reached but it will be executed accrding to the expression)
    taskService.complete(task.getId());
    try {
        waitForJobExecutorToProcessAllJobs(2000, 500);
        fail("Expected that job isn't executed because the timer is in t0");
    } catch (Exception e) {
    // expected
    }
    //after 1 hour the event must be triggered and the flow will go to the next step
    nextTimeCal.add(Calendar.HOUR, 1);
    processEngineConfiguration.getClock().setCurrentTime(nextTimeCal.getTime());
    waitForJobExecutorToProcessAllJobs(2000, 200);
    //expect to execute because the time is reached.
    List<Job> jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    tasks = taskService.createTaskQuery().list();
    assertEquals(1, tasks.size());
    task = tasks.get(0);
    assertEquals("Task C", task.getName());
    //Test Timer Catch Intermediate Events after completing Task C
    taskService.complete(task.getId());
    //after 1H 30 minutes from process start, the timer will trigger because of the endDate
    nextTimeCal.add(Calendar.MINUTE, 30);
    processEngineConfiguration.getClock().setCurrentTime(nextTimeCal.getTime());
    waitForJobExecutorToProcessAllJobs(2000, 500);
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
        assertNotNull(historicInstance.getEndTime());
    }
    //now all the process instances should be completed
    List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //no jobs
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    //no tasks
    tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
}
Also used : Task(org.activiti.engine.task.Task) Calendar(java.util.Calendar) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) Date(java.util.Date) DateTime(org.joda.time.DateTime) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Job(org.activiti.engine.runtime.Job) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Deployment(org.activiti.engine.test.Deployment)

Example 92 with Job

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

the class StartTimerEventRepeatWithEndExpressionTest method testCycleDateStartTimerEvent.

/**
   * Timer repetition
   */
public void testCycleDateStartTimerEvent() throws Exception {
    Clock previousClock = processEngineConfiguration.getClock();
    Clock testClock = new DefaultClockImpl();
    processEngineConfiguration.setClock(testClock);
    Calendar calendar = Calendar.getInstance();
    calendar.set(2025, Calendar.DECEMBER, 10, 0, 0, 0);
    testClock.setCurrentTime(calendar.getTime());
    //deploy the process
    repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/bpmn/event/timer/StartTimerEventRepeatWithEndExpressionTest.testCycleDateStartTimerEvent.bpmn20.xml").deploy();
    assertEquals(1, repositoryService.createProcessDefinitionQuery().count());
    //AFTER DEPLOYMENT
    //when the process is deployed there will be created a timerStartEvent job which will wait to be executed.
    List<Job> jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    //dueDate should be after 24 hours from the process deployment
    Calendar dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 11, 0, 0, 0);
    //check the due date is inside the 2 seconds range
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    //No process instances
    List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //No tasks
    List<Task> tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    // ADVANCE THE CLOCK
    // advance the clock to 11 dec -> the system will execute the pending job and will create a new one
    moveByMinutes(60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
        fail("there must be a pending job because the endDate is not reached yet");
    } catch (Exception e) {
    //expected failure
    }
    // After the first startEvent Execution should be one process instance started
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(1, processInstances.size());
    // one task to be executed (the userTask "Task A")
    tasks = taskService.createTaskQuery().list();
    assertEquals(1, tasks.size());
    // one new job will be created (and the old one will be deleted after execution)
    jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 12, 0, 0, 0);
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    // ADVANCE THE CLOCK SO THE END DATE WILL BE REACHED
    // 12 dec (last execution)
    moveByMinutes(60 * 24);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
    } catch (Exception e) {
        fail("Because the endDate is reached it will not be executed other jobs");
    }
    // After the second startEvent Execution should have 2 process instances started
    // (since the first one was not completed)
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(2, processInstances.size());
    // Because the endDate 12.dec.2025 is reached
    // the current job will be deleted after execution and a new one will not be created.
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    // 2 tasks to be executed (the userTask "Task A")
    // one task for each process instance
    tasks = taskService.createTaskQuery().list();
    assertEquals(2, tasks.size());
    // count "timer fired" events
    int timerFiredCount = 0;
    List<ActivitiEvent> eventsReceived = listener.getEventsReceived();
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.TIMER_FIRED.equals(eventReceived.getType())) {
            timerFiredCount++;
        }
    }
    //count "entity created" events
    int eventCreatedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_CREATED.equals(eventReceived.getType())) {
            eventCreatedCount++;
        }
    }
    // count "entity deleted" events
    int eventDeletedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_DELETED.equals(eventReceived.getType())) {
            eventDeletedCount++;
        }
    }
    //2 timers fired
    assertEquals(2, timerFiredCount);
    //2 jobs created
    assertEquals(2, eventCreatedCount);
    //2 jobs deleted
    assertEquals(2, eventDeletedCount);
    // let's complete the userTasks where the process is hanging in order to complete the processes.
    for (ProcessInstance processInstance : processInstances) {
        tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();
        Task task = tasks.get(0);
        assertEquals("Task A", task.getName());
        assertEquals(1, tasks.size());
        taskService.complete(task.getId());
    }
    //now All the process instances should be completed
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //no jobs
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    //no tasks
    tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    listener.clearEventsReceived();
    processEngineConfiguration.setClock(previousClock);
    repositoryService.deleteDeployment(repositoryService.createDeploymentQuery().singleResult().getId(), true);
}
Also used : Task(org.activiti.engine.task.Task) Calendar(java.util.Calendar) Clock(org.activiti.engine.runtime.Clock) DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Job(org.activiti.engine.runtime.Job)

Example 93 with Job

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

the class StartTimerEventRepeatWithEndTest method testCycleDateStartTimerEvent.

/**
   * Timer repetition
   */
public void testCycleDateStartTimerEvent() throws Exception {
    Clock previousClock = processEngineConfiguration.getClock();
    Clock testClock = new DefaultClockImpl();
    processEngineConfiguration.setClock(testClock);
    Calendar calendar = Calendar.getInstance();
    calendar.set(2025, Calendar.DECEMBER, 10, 0, 0, 0);
    testClock.setCurrentTime(calendar.getTime());
    //deploy the process
    repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/bpmn/event/timer/StartTimerEventRepeatWithEndTest.testCycleDateStartTimerEvent.bpmn20.xml").deploy();
    assertEquals(1, repositoryService.createProcessDefinitionQuery().count());
    //AFTER DEPLOYMENT
    //when the process is deployed there will be created a timerStartEvent job which will wait to be executed.
    List<Job> jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    //dueDate should be after 24 hours from the process deployment
    Calendar dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 11, 0, 0, 0);
    //check the due date is inside the 2 seconds range
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    //No process instances
    List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //No tasks
    List<Task> tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    // ADVANCE THE CLOCK
    // advance the clock to 11 dec -> the system will execute the pending job and will create a new one
    moveByMinutes(60 * 25);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
        fail("there must be a pending job because the endDate is not reached yet");
    } catch (Exception e) {
    //expected failure
    }
    jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    // After the first startEvent Execution should be one process instance started
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(1, processInstances.size());
    // one task to be executed (the userTask "Task A")
    tasks = taskService.createTaskQuery().list();
    assertEquals(1, tasks.size());
    // one new job will be created (and the old one will be deleted after execution)
    jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    dueDateCalendar = Calendar.getInstance();
    dueDateCalendar.set(2025, Calendar.DECEMBER, 12, 0, 0, 0);
    assertEquals(true, Math.abs(dueDateCalendar.getTime().getTime() - jobs.get(0).getDuedate().getTime()) < 2000);
    // ADVANCE THE CLOCK SO THE END DATE WILL BE REACHED
    // 12 dec (last execution)
    moveByMinutes(60 * 25);
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
    } catch (Exception e) {
        fail("Because the endDate is reached it will not be executed other jobs");
    }
    // After the second startEvent Execution should have 2 process instances started
    // (since the first one was not completed)
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(2, processInstances.size());
    // Because the endDate 12.dec.2025 is reached
    // the current job will be deleted after execution and a new one will not be created.
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    // 2 tasks to be executed (the userTask "Task A")
    // one task for each process instance
    tasks = taskService.createTaskQuery().list();
    assertEquals(2, tasks.size());
    // count "timer fired" events
    int timerFiredCount = 0;
    List<ActivitiEvent> eventsReceived = listener.getEventsReceived();
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.TIMER_FIRED.equals(eventReceived.getType())) {
            timerFiredCount++;
        }
    }
    //count "entity created" events
    int eventCreatedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_CREATED.equals(eventReceived.getType())) {
            eventCreatedCount++;
        }
    }
    // count "entity deleted" events
    int eventDeletedCount = 0;
    for (ActivitiEvent eventReceived : eventsReceived) {
        if (ActivitiEventType.ENTITY_DELETED.equals(eventReceived.getType())) {
            eventDeletedCount++;
        }
    }
    //2 timers fired
    assertEquals(2, timerFiredCount);
    //2 jobs created
    assertEquals(2, eventCreatedCount);
    //2 jobs deleted
    assertEquals(2, eventDeletedCount);
    // let's complete the userTasks where the process is hanging in order to complete the processes.
    for (ProcessInstance processInstance : processInstances) {
        tasks = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId()).list();
        Task task = tasks.get(0);
        assertEquals("Task A", task.getName());
        assertEquals(1, tasks.size());
        taskService.complete(task.getId());
    }
    //now All the process instances should be completed
    processInstances = runtimeService.createProcessInstanceQuery().list();
    assertEquals(0, processInstances.size());
    //no jobs
    jobs = managementService.createJobQuery().list();
    assertEquals(0, jobs.size());
    //no tasks
    tasks = taskService.createTaskQuery().list();
    assertEquals(0, tasks.size());
    listener.clearEventsReceived();
    processEngineConfiguration.setClock(previousClock);
    repositoryService.deleteDeployment(repositoryService.createDeploymentQuery().singleResult().getId(), true);
}
Also used : Task(org.activiti.engine.task.Task) Calendar(java.util.Calendar) Clock(org.activiti.engine.runtime.Clock) DefaultClockImpl(org.activiti.engine.impl.util.DefaultClockImpl) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Job(org.activiti.engine.runtime.Job)

Example 94 with Job

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

the class SignalEventTest method testSignalStartEventFromAPIAsync.

public void testSignalStartEventFromAPIAsync() {
    // Deploy test processes
    repositoryService.createDeployment().addClasspathResource("org/activiti/engine/test/bpmn/event/signal/SignalEventTest.testSignalStartEventAsync.bpmn20.xml").deploy();
    runtimeService.signalEventReceivedAsync("The Signal");
    assertEquals(3, managementService.createJobQuery().count());
    for (Job job : managementService.createJobQuery().list()) {
        managementService.executeJob(job.getId());
    }
    assertEquals(3, runtimeService.createProcessInstanceQuery().count());
    assertEquals(3, taskService.createTaskQuery().count());
    List<Task> tasks = taskService.createTaskQuery().orderByTaskName().asc().list();
    List<String> names = Arrays.asList("A", "B", "C");
    for (int i = 0; i < tasks.size(); i++) {
        assertEquals("Task in process " + names.get(i), tasks.get(i).getName());
    }
    // Start a process with a signal boundary event
    runtimeService.startProcessInstanceByKey("processWithSignalCatch");
    assertEquals(4, runtimeService.createProcessInstanceQuery().count());
    assertEquals(4, taskService.createTaskQuery().count());
    assertEquals(1, taskService.createTaskQuery().taskName("Task in process D").count());
    // Firing again
    runtimeService.signalEventReceivedAsync("The Signal");
    assertEquals(4, managementService.createJobQuery().count());
    for (Job job : managementService.createJobQuery().list()) {
        managementService.executeJob(job.getId());
    }
    assertEquals(7, runtimeService.createProcessInstanceQuery().count());
    assertEquals(7, taskService.createTaskQuery().count());
    assertEquals(1, taskService.createTaskQuery().taskName("Task after signal").count());
    // Cleanup
    for (org.activiti.engine.repository.Deployment deployment : repositoryService.createDeploymentQuery().list()) {
        repositoryService.deleteDeployment(deployment.getId(), true);
    }
}
Also used : Task(org.activiti.engine.task.Task) Job(org.activiti.engine.runtime.Job)

Example 95 with Job

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

the class BoundaryTimerEventRepeatWithEnd method testRepeatWithEnd.

@Deployment
public void testRepeatWithEnd() throws Throwable {
    Calendar calendar = Calendar.getInstance();
    Date baseTime = calendar.getTime();
    calendar.add(Calendar.MINUTE, 20);
    //expect to stop boundary jobs after 20 minutes
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    DateTime dt = new DateTime(calendar.getTime());
    String dateStr = fmt.print(dt);
    //reset the timer
    Calendar nextTimeCal = Calendar.getInstance();
    nextTimeCal.setTime(baseTime);
    processEngineConfiguration.getClock().setCurrentTime(nextTimeCal.getTime());
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("repeatWithEnd");
    runtimeService.setVariable(processInstance.getId(), "EndDateForBoundary", dateStr);
    List<Task> tasks = taskService.createTaskQuery().list();
    assertEquals(1, tasks.size());
    Task task = tasks.get(0);
    assertEquals("Task A", task.getName());
    //Test Boundary Events
    // complete will cause timer to be created
    taskService.complete(task.getId());
    List<Job> jobs = managementService.createJobQuery().list();
    assertEquals(1, jobs.size());
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
        fail("a new job must be prepared because there are 20 repeats 2 seconds interval");
    } catch (Exception ex) {
    //expected exception because a new job is prepared
    }
    //after 15 minutes
    nextTimeCal.add(Calendar.MINUTE, 15);
    processEngineConfiguration.getClock().setCurrentTime(nextTimeCal.getTime());
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
        fail("a new job must be prepared because there are 20 repeats 2 seconds interval");
    } catch (Exception ex) {
    //expected exception because a new job is prepared
    }
    //after another 5 minutes (20 minutes and 1 second from the baseTime) the BoundaryEndTime is reached
    nextTimeCal.add(Calendar.MINUTE, 5);
    nextTimeCal.add(Calendar.SECOND, 1);
    processEngineConfiguration.getClock().setCurrentTime(nextTimeCal.getTime());
    try {
        waitForJobExecutorToProcessAllJobs(2000, 200);
    } catch (Exception ex) {
        fail("Should not have any other jobs because the endDate is reached");
    }
    tasks = taskService.createTaskQuery().list();
    task = tasks.get(0);
    assertEquals("Task B", task.getName());
    assertEquals(1, tasks.size());
    taskService.complete(task.getId());
    try {
        waitForJobExecutorToProcessAllJobs(2000, 500);
    } catch (Exception e) {
        // expected
        fail("No jobs should be active here.");
    }
}
Also used : Task(org.activiti.engine.task.Task) Calendar(java.util.Calendar) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Job(org.activiti.engine.runtime.Job) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Date(java.util.Date) DateTime(org.joda.time.DateTime) Deployment(org.activiti.engine.test.Deployment)

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