Java Code Examples for org.flowable.engine.impl.util.CommandContextUtil#getEventDispatcher()

The following examples show how to use org.flowable.engine.impl.util.CommandContextUtil#getEventDispatcher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SuspensionStateUtil.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
protected static void dispatchStateChangeEvent(Object entity, SuspensionState state) {
    CommandContext commandContext = Context.getCommandContext();
    FlowableEventDispatcher eventDispatcher = null;
    if (commandContext != null) {
        eventDispatcher = CommandContextUtil.getEventDispatcher();
    }
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        FlowableEngineEventType eventType = null;
        if (state == SuspensionState.ACTIVE) {
            eventType = FlowableEngineEventType.ENTITY_ACTIVATED;
        } else {
            eventType = FlowableEngineEventType.ENTITY_SUSPENDED;
        }
        eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(eventType, entity));
    }
}
 
Example 2
Source File: TriggerTimerEventJobHandler.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(JobEntity job, String configuration, VariableScope variableScope, CommandContext commandContext) {
    ExecutionEntity executionEntity = (ExecutionEntity) variableScope;
    CommandContextUtil.getAgenda(commandContext).planTriggerExecutionOperation(executionEntity);
    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher();
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.TIMER_FIRED, job));
    }
}
 
Example 3
Source File: DispatchEventCommand.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    if (event == null) {
        throw new FlowableIllegalArgumentException("event is null");
    }

    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher();
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        eventDispatcher.dispatchEvent(event);
    } else {
        throw new FlowableException("Message dispatcher is disabled, cannot dispatch event");
    }

    return null;
}
 
Example 4
Source File: SaveTaskCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void handleAssigneeChange(CommandContext commandContext,
        ProcessEngineConfigurationImpl processEngineConfiguration) {
    processEngineConfiguration.getListenerNotificationHelper().executeTaskListeners(task, TaskListener.EVENTNAME_ASSIGNMENT);

    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher(commandContext);
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        CommandContextUtil.getEventDispatcher().dispatchEvent(FlowableTaskEventBuilder.createEntityEvent(FlowableEngineEventType.TASK_ASSIGNED, task));
    }
}
 
Example 5
Source File: SetProcessDefinitionCategoryCmd.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {

    if (processDefinitionId == null) {
        throw new FlowableIllegalArgumentException("Process definition id is null");
    }

    ProcessDefinitionEntity processDefinition = CommandContextUtil.getProcessDefinitionEntityManager(commandContext).findById(processDefinitionId);

    if (processDefinition == null) {
        throw new FlowableObjectNotFoundException("No process definition found for id = '" + processDefinitionId + "'", ProcessDefinition.class);
    }

    if (Flowable5Util.isFlowable5ProcessDefinition(processDefinition, commandContext)) {
        Flowable5CompatibilityHandler compatibilityHandler = Flowable5Util.getFlowable5CompatibilityHandler();
        compatibilityHandler.setProcessDefinitionCategory(processDefinitionId, category);
        return null;
    }

    // Update category
    processDefinition.setCategory(category);

    // Remove process definition from cache, it will be refetched later
    DeploymentCache<ProcessDefinitionCacheEntry> processDefinitionCache = CommandContextUtil.getProcessEngineConfiguration(commandContext).getProcessDefinitionCache();
    if (processDefinitionCache != null) {
        processDefinitionCache.remove(processDefinitionId);
    }

    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher();
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        eventDispatcher
            .dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_UPDATED, processDefinition));
    }

    return null;
}
 
Example 6
Source File: JobRetryCmd.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public Object execute(CommandContext commandContext) {
    JobService jobService = CommandContextUtil.getJobService(commandContext);
    TimerJobService timerJobService = CommandContextUtil.getTimerJobService(commandContext);
    
    JobEntity job = jobService.findJobById(jobId);
    if (job == null) {
        return null;
    }

    ProcessEngineConfiguration processEngineConfig = CommandContextUtil.getProcessEngineConfiguration(commandContext);

    ExecutionEntity executionEntity = fetchExecutionEntity(commandContext, job.getExecutionId());
    FlowElement currentFlowElement = executionEntity != null ? executionEntity.getCurrentFlowElement() : null;
    if (executionEntity != null) {
        executionEntity.setActive(false);
    }

    String failedJobRetryTimeCycleValue = null;
    if (currentFlowElement instanceof ServiceTask) {
        failedJobRetryTimeCycleValue = ((ServiceTask) currentFlowElement).getFailedJobRetryTimeCycleValue();
    }

    AbstractRuntimeJobEntity newJobEntity = null;
    if (currentFlowElement == null || failedJobRetryTimeCycleValue == null) {

        LOGGER.debug("activity or FailedJobRetryTimerCycleValue is null in job {}. Only decrementing retries.", jobId);

        if (job.getRetries() <= 1) {
            newJobEntity = jobService.moveJobToDeadLetterJob(job);
        } else {
            newJobEntity = timerJobService.moveJobToTimerJob(job);
        }

        newJobEntity.setRetries(job.getRetries() - 1);
        if (job.getDuedate() == null || JobEntity.JOB_TYPE_MESSAGE.equals(job.getJobType())) {
            // add wait time for failed async job
            newJobEntity.setDuedate(calculateDueDate(commandContext, processEngineConfig.getAsyncFailedJobWaitTime(), null));
        } else {
            // add default wait time for failed job
            newJobEntity.setDuedate(calculateDueDate(commandContext, processEngineConfig.getDefaultFailedJobWaitTime(), job.getDuedate()));
        }

    } else {
        try {
            DurationHelper durationHelper = new DurationHelper(failedJobRetryTimeCycleValue, processEngineConfig.getClock());
            int jobRetries = job.getRetries();
            if (job.getExceptionMessage() == null) {
                // change default retries to the ones configured
                jobRetries = durationHelper.getTimes();
            }

            if (jobRetries <= 1) {
                newJobEntity = jobService.moveJobToDeadLetterJob(job);
            } else {
                newJobEntity = timerJobService.moveJobToTimerJob(job);
            }

            newJobEntity.setDuedate(durationHelper.getDateAfter());

            if (job.getExceptionMessage() == null) { // is it the first exception
                LOGGER.debug("Applying JobRetryStrategy '{}' the first time for job {} with {} retries", failedJobRetryTimeCycleValue, job.getId(), durationHelper.getTimes());

            } else {
                LOGGER.debug("Decrementing retries of JobRetryStrategy '{}' for job {}", failedJobRetryTimeCycleValue, job.getId());
            }

            newJobEntity.setRetries(jobRetries - 1);

        } catch (Exception e) {
            throw new FlowableException("failedJobRetryTimeCycle has wrong format:" + failedJobRetryTimeCycleValue, exception);
        }
    }

    if (exception != null) {
        newJobEntity.setExceptionMessage(exception.getMessage());
        newJobEntity.setExceptionStacktrace(getExceptionStacktrace());
    }

    // Dispatch both an update and a retry-decrement event
    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher();
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_UPDATED, newJobEntity));
        eventDispatcher.dispatchEvent(FlowableEventBuilder.createEntityEvent(FlowableEngineEventType.JOB_RETRIES_DECREMENTED, newJobEntity));
    }

    return null;
}
 
Example 7
Source File: SaveTaskCmd.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public Void execute(CommandContext commandContext) {
    if (task == null) {
        throw new FlowableIllegalArgumentException("task is null");
    }

    if (task.getProcessDefinitionId() != null && Flowable5Util.isFlowable5ProcessDefinitionId(commandContext, task.getProcessDefinitionId())) {
        Flowable5CompatibilityHandler compatibilityHandler = Flowable5Util.getFlowable5CompatibilityHandler();
        compatibilityHandler.saveTask(task);
        return null;
    }
    
    TaskService taskService = CommandContextUtil.getTaskService(commandContext);

    if (task.getRevision() == 0) {
        TaskHelper.insertTask(task, null, true, false);

        FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher(commandContext);
        if (eventDispatcher != null && eventDispatcher.isEnabled()) {
            CommandContextUtil.getEventDispatcher().dispatchEvent(FlowableTaskEventBuilder.createEntityEvent(FlowableEngineEventType.TASK_CREATED, task));
        }
        
        handleSubTaskCount(taskService, null);

    } else {

        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);

        TaskInfo originalTaskEntity = taskService.getTask(task.getId());
        
        if (originalTaskEntity == null && processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.AUDIT)) {
            originalTaskEntity = CommandContextUtil.getHistoricTaskService().getHistoricTask(task.getId());
        }

        CommandContextUtil.getActivityInstanceEntityManager(commandContext)
            .recordTaskInfoChange(task, processEngineConfiguration.getClock().getCurrentTime());
        taskService.updateTask(task, true);

        // Special care needed to detect the assignee task has changed
        if (!StringUtils.equals(originalTaskEntity.getAssignee(), task.getAssignee())) {
            handleAssigneeChange(commandContext, processEngineConfiguration);
        }
        
        // Special care needed to detect the parent task has changed
        if (!StringUtils.equals(originalTaskEntity.getParentTaskId(), task.getParentTaskId())) {
            handleSubTaskCount(taskService, originalTaskEntity);
        }
        
    }

    return null;
}
 
Example 8
Source File: AbstractDynamicStateManager.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected ExecutionEntity createEmbeddedSubProcessHierarchy(SubProcess subProcess, ExecutionEntity defaultParentExecution, Map<String, SubProcess> subProcessesToCreate, Set<String> movingExecutionIds, ProcessInstanceChangeState processInstanceChangeState, CommandContext commandContext) {
    if (processInstanceChangeState.getProcessInstanceActiveEmbeddedExecutions().containsKey(subProcess.getId())) {
        return processInstanceChangeState.getProcessInstanceActiveEmbeddedExecutions().get(subProcess.getId()).get(0);
    }

    if (processInstanceChangeState.getCreatedEmbeddedSubProcesses().containsKey(subProcess.getId())) {
        return processInstanceChangeState.getCreatedEmbeddedSubProcesses().get(subProcess.getId());
    }

    //Create the parent, if needed
    ExecutionEntity parentSubProcess = defaultParentExecution;
    if (subProcess.getSubProcess() != null) {
        parentSubProcess = createEmbeddedSubProcessHierarchy(subProcess.getSubProcess(), defaultParentExecution, subProcessesToCreate, movingExecutionIds, processInstanceChangeState, commandContext);
        processInstanceChangeState.getCreatedEmbeddedSubProcesses().put(subProcess.getSubProcess().getId(), parentSubProcess);
    }
    ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);

    ExecutionEntity subProcessExecution = executionEntityManager.createChildExecution(parentSubProcess);
    subProcessExecution.setCurrentFlowElement(subProcess);
    subProcessExecution.setScope(true);

    FlowableEventDispatcher eventDispatcher = CommandContextUtil.getEventDispatcher();
    if (eventDispatcher != null && eventDispatcher.isEnabled()) {
        eventDispatcher.dispatchEvent(
            FlowableEventBuilder.createActivityEvent(FlowableEngineEventType.ACTIVITY_STARTED, subProcess.getId(), subProcess.getName(), subProcessExecution.getId(),
                subProcessExecution.getProcessInstanceId(), subProcessExecution.getProcessDefinitionId(), subProcess));
    }

    subProcessExecution.setVariablesLocal(processDataObjects(subProcess.getDataObjects()));

    CommandContextUtil.getActivityInstanceEntityManager(commandContext).recordActivityStart(subProcessExecution);

    List<BoundaryEvent> boundaryEvents = subProcess.getBoundaryEvents();
    if (CollectionUtil.isNotEmpty(boundaryEvents)) {
        executeBoundaryEvents(boundaryEvents, subProcessExecution);
    }

    if (subProcess instanceof EventSubProcess) {
        processCreatedEventSubProcess((EventSubProcess) subProcess, subProcessExecution, movingExecutionIds, commandContext);
    }

    ProcessInstanceHelper processInstanceHelper = CommandContextUtil.getProcessEngineConfiguration(commandContext).getProcessInstanceHelper();

    //Process containing child Event SubProcesses not contained in this creation hierarchy
    List<EventSubProcess> childEventSubProcesses = subProcess.findAllSubFlowElementInFlowMapOfType(EventSubProcess.class);
    childEventSubProcesses.stream()
        .filter(childEventSubProcess -> !subProcessesToCreate.containsKey(childEventSubProcess.getId()))
        .forEach(childEventSubProcess -> processInstanceHelper.processEventSubProcess(subProcessExecution, childEventSubProcess, commandContext));

    return subProcessExecution;
}