
Global event listener on Process Engine Server
@Slf4j
@Component
public class CamundaEventListeners {
@EventListener
public void onTaskEvent(DelegateTask taskDelegate) {
log.info("Handling mutable DelegateTask:{}", taskDelegate.getTaskDefinitionKey());
}
@EventListener
public void onTaskEvent(TaskEvent taskEvent) {
log.info("Handling immutable TaskEvent:{}", taskEvent.getTaskDefinitionKey());
}
@EventListener
public void onExecutionEvent(DelegateExecution executionDelegate) {
log.info("Handling mutable DelegateExecution:{}", executionDelegate.getCurrentActivityName());
}
@EventListener
public void onExecutionEvent(ExecutionEvent executionEvent) {
log.info("Handling immutable ExecutionEvent:{}", executionEvent.getProcessDefinitionId());
}
@EventListener
public void onHistoryEvent(HistoryEvent historyEvent) {
log.info("Handling mutable HistoryEvent:{}", historyEvent.getEventType());
}
}
camunda:
bpm:
# event
eventing:
execution: true
task: true
skippable: true
history: true
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.impl.history.event.HistoryEvent;
import org.camunda.bpm.spring.boot.starter.event.ExecutionEvent;
import org.camunda.bpm.spring.boot.starter.event.TaskEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class CamundaEventListeners {
@EventListener
public void onTaskEvent(TaskEvent taskEvent) {
String eventName = taskEvent.getEventName();
String taskId = taskEvent.getId();
String taskName = taskEvent.getName();
String taskDefinitionKey = taskEvent.getTaskDefinitionKey();
switch (eventName) {
case "create":
log.info("用户任务开始 - 任务ID: {}, 任务定义Key: {}, 任务名称: {}", taskId, taskDefinitionKey, taskName);
break;
case "complete":
log.info("用户任务结束 - 任务ID: {}, 任务定义Key: {}, 任务名称: {}", taskId, taskDefinitionKey, taskName);
break;
default:
break;
}
}
@EventListener
public void onExecutionEvent(ExecutionEvent executionEvent) {
String eventName = executionEvent.getEventName();
String processInstanceId = executionEvent.getProcessInstanceId();
String activityId = executionEvent.getCurrentActivityId();
String activityName = executionEvent.getCurrentActivityName();
switch (eventName) {
case "start":
log.info("执行开始 - 流程实例ID: {}, 活动ID: {}, 活动名称:{}, 类型:{}",
processInstanceId, activityId, activityName, eventName);
break;
case "end":
log.info("执行结束 - 流程实例ID: {}, 活动ID: {}, 活动名称:{}, 类型:{}",
processInstanceId, activityId, activityName, eventName);
break;
default:
break;
}
}
}
可以给每一个任务类型都加上对应的监听处理
# 流程引擎插件注入
@Configuration
public class ProgressLoggingSupportParseListenerPlugin extends AbstractProcessEnginePlugin {
@Override
public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration {
// get all existing preParseListeners
List<BpmnParseListener> preParseListeners = processEngineConfiguration.getCustomPreBPMNParseListeners();
if(preParseListeners == null) {
// if no preParseListener exists, create new list
preParseListeners = new ArrayList<BpmnParseListener>();
processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
}
// add new BPMN Parse Listener
preParseListeners.add(new ProgressLoggingSupportParseListener());
}
}
# 流程监听注入
public class ProgressLoggingSupportParseListener extends AbstractBpmnParseListener {
// parse given service task to get the attributes of the property extension elements
@Override
public void parseServiceTask(Element serviceTaskElement, ScopeImpl scope, ActivityImpl activity) {
// get the <extensionElements ...> element from the service task
Element extensionElement = serviceTaskElement.element("extensionElements");
if (extensionElement != null) {
// get the <camunda:properties ...> element from the service task
Element propertiesElement = extensionElement.element("properties");
if (propertiesElement != null) {
// get list of <camunda:property ...> elements from the service task
List<Element> propertyList = propertiesElement.elements("property");
for (Element property : propertyList) {
// get the name and the value of the extension property element
String name = property.attribute("name");
String value = property.attribute("value");
// check if name attribute has the expected value
if("progress".equals(name)) {
// add execution listener to the given service task element
// to execute it when the end event of the service task fired
ProgressLoggingExecutionListener progressLoggingExecutionListener = new ProgressLoggingExecutionListener(value);
activity.addExecutionListener(ExecutionListener.EVENTNAME_END, progressLoggingExecutionListener);
}
}
}
}
}
}
# 具体监听实现
public class ProgressLoggingExecutionListener implements ExecutionListener {
private final Logger LOGGER = Logger.getLogger(this.getClass().getName());
// static value list to see in the UNIT test if the execution listener was executed
public static List<String> progressValueList = new ArrayList<String>();
private String propertyValue;
// constructor with extension property value as parameter
public ProgressLoggingExecutionListener(String value) {
this.propertyValue = value;
}
// notify method is executed when Execution Listener is called
public void notify(DelegateExecution execution) throws Exception {
progressValueList.add(propertyValue);
// logging statement to see which value have the property 'progress'
LOGGER.info("value of service task extension property 'progress': " + propertyValue);
}
}
比如:
