Search in sources :

Example 6 with TimerEntity

use of org.activiti.engine.impl.persistence.entity.TimerEntity in project Activiti by Activiti.

the class AbstractSetProcessDefinitionStateCmd method createTimerForDelayedExecution.

protected void createTimerForDelayedExecution(CommandContext commandContext, List<ProcessDefinitionEntity> processDefinitions) {
    for (ProcessDefinitionEntity processDefinition : processDefinitions) {
        TimerEntity timer = new TimerEntity();
        timer.setProcessDefinitionId(processDefinition.getId());
        // Inherit tenant identifier (if applicable)
        if (processDefinition.getTenantId() != null) {
            timer.setTenantId(processDefinition.getTenantId());
        }
        timer.setDuedate(executionDate);
        timer.setJobHandlerType(getDelayedExecutionJobHandlerType());
        timer.setJobHandlerConfiguration(TimerChangeProcessDefinitionSuspensionStateJobHandler.createJobHandlerConfiguration(includeProcessInstances));
        commandContext.getJobEntityManager().schedule(timer);
    }
}
Also used : TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) ProcessDefinitionEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)

Example 7 with TimerEntity

use of org.activiti.engine.impl.persistence.entity.TimerEntity in project Activiti by Activiti.

the class TimerDeclarationImpl method prepareTimerEntity.

public TimerEntity prepareTimerEntity(ExecutionEntity executionEntity) {
    // ACT-1415: timer-declaration on start-event may contain expressions NOT
    // evaluating variables but other context, evaluating should happen nevertheless
    VariableScope scopeForExpression = executionEntity;
    if (scopeForExpression == null) {
        scopeForExpression = NoExecutionVariableScope.getSharedInstance();
    }
    String calendarNameValue = type.calendarName;
    if (this.calendarNameExpression != null) {
        calendarNameValue = (String) this.calendarNameExpression.getValue(scopeForExpression);
    }
    BusinessCalendar businessCalendar = Context.getProcessEngineConfiguration().getBusinessCalendarManager().getBusinessCalendar(calendarNameValue);
    if (description == null) {
        // Prevent NPE from happening in the next line
        throw new ActivitiIllegalArgumentException("Timer '" + executionEntity.getActivityId() + "' was not configured with a valid duration/time");
    }
    String endDateString = null;
    String dueDateString = null;
    Date duedate = null;
    Date endDate = null;
    if (endDateExpression != null && !(scopeForExpression instanceof NoExecutionVariableScope)) {
        Object endDateValue = endDateExpression.getValue(scopeForExpression);
        if (endDateValue instanceof String) {
            endDateString = (String) endDateValue;
        } else if (endDateValue instanceof Date) {
            endDate = (Date) endDateValue;
        } else if (endDateValue instanceof DateTime) {
            // Joda DateTime support
            duedate = ((DateTime) endDateValue).toDate();
        } else {
            throw new ActivitiException("Timer '" + executionEntity.getActivityId() + "' was not configured with a valid duration/time, either hand in a java.util.Date or a String in format 'yyyy-MM-dd'T'hh:mm:ss'");
        }
        if (endDate == null) {
            endDate = businessCalendar.resolveEndDate(endDateString);
        }
    }
    Object dueDateValue = description.getValue(scopeForExpression);
    if (dueDateValue instanceof String) {
        dueDateString = (String) dueDateValue;
    } else if (dueDateValue instanceof Date) {
        duedate = (Date) dueDateValue;
    } else if (dueDateValue instanceof DateTime) {
        // Joda DateTime support
        duedate = ((DateTime) dueDateValue).toDate();
    } else if (dueDateValue != null) {
        //dueDateValue==null is OK - but unexpected class type must throw an error.
        throw new ActivitiException("Timer '" + executionEntity.getActivityId() + "' was not configured with a valid duration/time, either hand in a java.util.Date or a String in format 'yyyy-MM-dd'T'hh:mm:ss'");
    }
    if (duedate == null && dueDateString != null) {
        duedate = businessCalendar.resolveDuedate(dueDateString);
    }
    TimerEntity timer = null;
    // if dueDateValue is null -> this is OK - timer will be null and job not scheduled
    if (duedate != null) {
        timer = new TimerEntity(this);
        timer.setDuedate(duedate);
        timer.setEndDate(endDate);
        if (executionEntity != null) {
            timer.setExecution(executionEntity);
            timer.setProcessDefinitionId(executionEntity.getProcessDefinitionId());
            timer.setProcessInstanceId(executionEntity.getProcessInstanceId());
            // Inherit tenant identifier (if applicable)
            if (executionEntity != null && executionEntity.getTenantId() != null) {
                timer.setTenantId(executionEntity.getTenantId());
            }
        }
        if (type == TimerDeclarationType.CYCLE) {
            // See ACT-1427: A boundary timer with a cancelActivity='true', doesn't need to repeat itself
            boolean repeat = !isInterruptingTimer;
            // ACT-1951: intermediate catching timer events shouldn't repeat according to spec
            if (TimerCatchIntermediateEventJobHandler.TYPE.equals(jobHandlerType)) {
                repeat = false;
                if (endDate != null) {
                    long endDateMiliss = endDate.getTime();
                    long dueDateMiliss = duedate.getTime();
                    long dueDate = Math.min(endDateMiliss, dueDateMiliss);
                    timer.setDuedate(new Date(dueDate));
                }
            }
            if (repeat) {
                String prepared = prepareRepeat(dueDateString);
                timer.setRepeat(prepared);
            }
        }
    }
    return timer;
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope) BusinessCalendar(org.activiti.engine.impl.calendar.BusinessCalendar) VariableScope(org.activiti.engine.delegate.VariableScope) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope) Date(java.util.Date) DateTime(org.joda.time.DateTime)

Example 8 with TimerEntity

use of org.activiti.engine.impl.persistence.entity.TimerEntity in project Activiti by Activiti.

the class JobRetryCmd method getCurrentActivity.

private ActivityImpl getCurrentActivity(CommandContext commandContext, JobEntity job) {
    String type = job.getJobHandlerType();
    ActivityImpl activity = null;
    if (TimerExecuteNestedActivityJobHandler.TYPE.equals(type) || TimerCatchIntermediateEventJobHandler.TYPE.equals(type)) {
        ExecutionEntity execution = fetchExecutionEntity(commandContext, job.getExecutionId());
        if (execution != null) {
            activity = execution.getProcessDefinition().findActivity(job.getJobHandlerConfiguration());
        }
    } else if (TimerStartEventJobHandler.TYPE.equals(type)) {
        DeploymentManager deploymentManager = commandContext.getProcessEngineConfiguration().getDeploymentManager();
        if (TimerEventHandler.hasRealActivityId(job.getJobHandlerConfiguration())) {
            ProcessDefinitionEntity processDefinition = deploymentManager.findDeployedProcessDefinitionById(job.getProcessDefinitionId());
            String activityId = TimerEventHandler.getActivityIdFromConfiguration(job.getJobHandlerConfiguration());
            activity = processDefinition.findActivity(activityId);
        } else {
            String processId = job.getJobHandlerConfiguration();
            if (job instanceof TimerEntity) {
                processId = TimerEventHandler.getActivityIdFromConfiguration(job.getJobHandlerConfiguration());
            }
            ProcessDefinitionEntity processDefinition = null;
            if (job.getTenantId() != null && job.getTenantId().length() > 0) {
                processDefinition = deploymentManager.findDeployedLatestProcessDefinitionByKeyAndTenantId(processId, job.getTenantId());
            } else {
                processDefinition = deploymentManager.findDeployedLatestProcessDefinitionByKey(processId);
            }
            if (processDefinition != null) {
                activity = processDefinition.getInitial();
            }
        }
    } else if (AsyncContinuationJobHandler.TYPE.equals(type)) {
        ExecutionEntity execution = fetchExecutionEntity(commandContext, job.getExecutionId());
        if (execution != null) {
            activity = execution.getActivity();
        }
    } else {
    // nop, because activity type is not supported
    }
    return activity;
}
Also used : ActivityImpl(org.activiti.engine.impl.pvm.process.ActivityImpl) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) DeploymentManager(org.activiti.engine.impl.persistence.deploy.DeploymentManager) TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) ProcessDefinitionEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)

Example 9 with TimerEntity

use of org.activiti.engine.impl.persistence.entity.TimerEntity in project Activiti by Activiti.

the class ActivityEventsTest method testActivityTimeOutEventInSubProcess.

@Deployment(resources = "org/activiti/engine/test/bpmn/event/timer/BoundaryTimerEventTest.testTimerOnNestingOfSubprocesses.bpmn20.xml")
public void testActivityTimeOutEventInSubProcess() {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("timerOnNestedSubprocesses");
    Job theJob = managementService.createJobQuery().processInstanceId(processInstance.getId()).singleResult();
    assertNotNull(theJob);
    // Force timer to fire
    Calendar timeToFire = Calendar.getInstance();
    timeToFire.add(Calendar.HOUR, 2);
    timeToFire.add(Calendar.SECOND, 5);
    processEngineConfiguration.getClock().setCurrentTime(timeToFire.getTime());
    waitForJobExecutorToProcessAllJobs(2000, 200);
    // Check timeout-events have been dispatched
    assertEquals(3, listener.getEventsReceived().size());
    List<String> eventIdList = new ArrayList<String>();
    for (ActivitiEvent event : listener.getEventsReceived()) {
        assertEquals(ActivitiEventType.ACTIVITY_CANCELLED, event.getType());
        assertTrue("TIMER is the cause of the cancellation", ((ActivitiActivityCancelledEvent) event).getCause() instanceof TimerEntity);
        eventIdList.add(((ActivitiActivityEventImpl) event).getActivityId());
    }
    assertTrue(eventIdList.indexOf("innerTask1") >= 0);
    assertTrue(eventIdList.indexOf("innerTask2") >= 0);
    assertTrue(eventIdList.indexOf("innerFork") >= 0);
}
Also used : ActivitiActivityCancelledEvent(org.activiti.engine.delegate.event.ActivitiActivityCancelledEvent) TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Job(org.activiti.engine.runtime.Job) Deployment(org.activiti.engine.test.Deployment)

Example 10 with TimerEntity

use of org.activiti.engine.impl.persistence.entity.TimerEntity in project Activiti by Activiti.

the class ActivityEventsTest method testActivityTimeOutEvent.

@Deployment(resources = "org/activiti/engine/test/api/event/JobEventsTest.testJobEntityEvents.bpmn20.xml")
public void testActivityTimeOutEvent() {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testJobEvents");
    Job theJob = managementService.createJobQuery().processInstanceId(processInstance.getId()).singleResult();
    assertNotNull(theJob);
    // Force timer to fire
    Calendar tomorrow = Calendar.getInstance();
    tomorrow.add(Calendar.DAY_OF_YEAR, 1);
    processEngineConfiguration.getClock().setCurrentTime(tomorrow.getTime());
    waitForJobExecutorToProcessAllJobs(2000, 100);
    // Check timeout has been dispatched
    assertEquals(1, listener.getEventsReceived().size());
    ActivitiEvent activitiEvent = listener.getEventsReceived().get(0);
    assertEquals("ACTIVITY_CANCELLED event expected", ActivitiEventType.ACTIVITY_CANCELLED, activitiEvent.getType());
    ActivitiActivityCancelledEvent cancelledEvent = (ActivitiActivityCancelledEvent) activitiEvent;
    assertTrue("TIMER is the cause of the cancellation", cancelledEvent.getCause() instanceof TimerEntity);
}
Also used : ActivitiActivityCancelledEvent(org.activiti.engine.delegate.event.ActivitiActivityCancelledEvent) TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) Calendar(java.util.Calendar) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) ActivitiEvent(org.activiti.engine.delegate.event.ActivitiEvent) Job(org.activiti.engine.runtime.Job) Deployment(org.activiti.engine.test.Deployment)

Aggregations

TimerEntity (org.activiti.engine.impl.persistence.entity.TimerEntity)15 Date (java.util.Date)5 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)5 Deployment (org.activiti.engine.test.Deployment)5 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)4 ProcessDefinitionEntity (org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)4 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)4 ActivitiActivityCancelledEvent (org.activiti.engine.delegate.event.ActivitiActivityCancelledEvent)3 ActivitiEvent (org.activiti.engine.delegate.event.ActivitiEvent)3 Job (org.activiti.engine.runtime.Job)3 ActivitiException (org.activiti.engine.ActivitiException)2 Page (org.activiti.engine.impl.Page)2 DeploymentManager (org.activiti.engine.impl.persistence.deploy.DeploymentManager)2 JobEntityManager (org.activiti.engine.impl.persistence.entity.JobEntityManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1