camunda中activitieventlistener的替代品是什么?

时间:2019-10-14 08:35:52

标签: java activiti camunda

我从Activiti迁移到Camunda。在camunda中,我想在创建任何任务时执行某些操作,例如,在创建任何任务时,我都想为创建的任务设置变量。在活动中,我使用activitieventlistener来做,但是现在在camunda中我该怎么办? 我以前的代码是

import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.valz.framework.common.utility.StringUtility;

public class ValzActivitiEventListener implements ActivitiEventListener {

@Override
public boolean isFailOnException() {
return false;
  }

 @Override
 public void onEvent(ActivitiEvent event) {
switch (event.getType()) {

case TASK_CREATED: {
    ActivitiEntityEvent activityEntityEvent = (ActivitiEntityEvent) event;
    TaskEntity taskEntity = (TaskEntity) activityEntityEvent.getEntity();
    ExecutionEntity exEntity = taskEntity.getProcessInstance();
    String prevTaskId =(String) exEntity.getVariable("prevTaskId");
    if(StringUtility.isNullOrEmpty( prevTaskId ))
        prevTaskId=taskEntity.getId();
    taskEntity.setVariableLocal("prevTaskId", prevTaskId);
}
    break;
default:
}
}

}

1 个答案:

答案 0 :(得分:1)

我认为TaskListener应该为您服务

@Component
@Slf4j
public class HumanTaskListener implements TaskListener {

    @Override
    public void notify(final DelegateTask delegateTask) {
        LOGGER.debug("Notify... {}", delegateTask);
    }
}

并注册监听器

@Component
public class HumanTaskBpmnListener extends AbstractBpmnParseListener {

    @Autowired
    private HumanTaskListener humanTaskListener;

    @Override
    public void parseUserTask(final Element humanTaskElement, final ScopeImpl scope, final ActivityImpl activity) {
        final TaskDefinition taskDefinition = ((UserTaskActivityBehavior) activity.getActivityBehavior()).getTaskDefinition();
        taskDefinition.addBuiltInTaskListener(TaskListener.EVENTNAME_CREATE, humanTaskListener);
    }

}