Flowable流程监听器配置
1、监听器概述
任务监听器:顾名思义是监听任务的。任务监听器的生命周期如下图所示,会经历assignment、create、complete、delete。当流程引擎触发这四种事件类型时,对应的任务监听器会捕获其事件类型,再按照监听器的处理逻辑进行处理。
执行监听器:监听流程的所有节点和连线。主要有start、end、take事件。其中节点有start、end两种事件,而连线则有take事件。
全局监听器:监听流程各种事件
2、监听器配置及使用
(1)任务监听器
实现TaskListener接口(org.flowable.task.service.delegate.TaskListener):
@Component
public class TestTaskListener implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
}
}
demo:
@Component(value = "taskBusinessCallListener")
public class TaskBusinessCallListener extends BusinessCallListener implements TaskListener {
/**
* rest接口
*/
private FixedValue restUrl;
/**
* 参数 多个的话用分号隔开 实例 userCode:1;status:1
*/
private FixedValue params;
@Override
public void notify(DelegateTask delegateTask) {
String processInstanceId = delegateTask.getProcessInstanceId();
String restUrlStr = null, paramsStr = null;
if (restUrl != null) {
restUrlStr = restUrl.getExpressionText();
}
if (params != null) {
paramsStr = params.getExpressionText();
}
//执行回调
this.callBack(processInstanceId, restUrlStr, paramsStr);
}
public void callBack(String pocessInstanceId, String restUrl, String params) {
String paramsJson = null;
try {
//Map<String, Object> paramMap = new HashMap<String, Object>();
//获取变量
Map<String, Object> paramMap = runtimeService.getVariables(pocessInstanceId);
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(pocessInstanceId).singleResult();
paramMap.put("businessKey", processInstance.getBusinessKey());
this.setParams(params, paramMap);
paramsJson = JSON.toJSONString(paramMap);
//设置超时时间,否则会报超时错误
HttpComponentsClientHttpRequestFactory requestFactory=new HttpComponentsClientHttpRequestFactory();
requestFactory.setReadTimeout(30000);
requestFactory.setConnectionRequestTimeout(30000);
requestFactory.setConnectTimeout(30000);
restTemplate=new RestTemplate(requestFactory);
//调用业务方法
logger.info("开始调用业务系统接口" + restUrl + ",业务参数:" + paramsJson);
restTemplate.postForObject(restUrl, paramsJson, String.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在流程xml文件中的配置表示为:
<userTask id="Activity_1j2um3f" flowable:dataType="fixed" flowable:userType="candidateUsers" flowable:candidateUsers="43">
<extensionElements>
<flowable:taskListener delegateExpression="${taskBusinessCallListener}" event="complete">
<flowable:field name="restUrl" stringValue="http://localhost:8080/user/update"/>
<flowable:field name="params" stringValue="userId:1;status:0"/>
</flowable:taskListener>
</extensionElements>
</userTask>
(2)全局监听器
实现FlowableEventListener接口(org.flowable.common.engine.api.delegate.event.FlowableEventListener):
@Component
public class TestGlobalListener implements FlowableEventListener {
@Override
public void onEvent(FlowableEvent flowableEvent) {
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}
demo:
@Component
public class FlowableBaseEventListenerImpl implements FlowableEventListener {
@Override
public void onEvent(FlowableEvent event) {
FlowableEventType type = event.getType(); // 事件类型
if (type == FlowableEngineEventType.TASK_CREATED) {
// 用户任务创建完成
if (event instanceof org.flowable.common.engine.impl.event.FlowableEntityEventImpl) {
org.flowable.common.engine.impl.event.FlowableEntityEventImpl eventImpl = (org.flowable.common.engine.impl.event.FlowableEntityEventImpl) event;
TaskEntityImpl taskEntity = (TaskEntityImpl) eventImpl.getEntity();
}
} else if (type == FlowableEngineEventType.TASK_COMPLETED) {
// 用户审批任务
if (event instanceof org.flowable.engine.delegate.event.impl.FlowableEntityWithVariablesEventImpl) {
org.flowable.engine.delegate.event.impl.FlowableEntityWithVariablesEventImpl eventImpl = (org.flowable.engine.delegate.event.impl.FlowableEntityWithVariablesEventImpl) event;
TaskEntityImpl taskEntity = (TaskEntityImpl) eventImpl.getEntity();
}
} else if (type == FlowableEngineEventType.PROCESS_COMPLETED) {
// 流程完成
if (event instanceof org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl) {
org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl eventImpl = (org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl) event;
ExecutionEntityImpl taskEntity = (ExecutionEntityImpl) eventImpl.getEntity();
}
} else if (type == FlowableEngineEventType.PROCESS_CANCELLED) {
// 流程删除
if (event instanceof org.flowable.engine.delegate.event.impl.FlowableProcessCancelledEventImpl) {
org.flowable.engine.delegate.event.impl.FlowableProcessCancelledEventImpl eventImpl = (org.flowable.engine.delegate.event.impl.FlowableProcessCancelledEventImpl) event;
}
}
//...
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}
添加全局监听器:
@Configuration
public class InitConfig implements CommandLineRunner {
@Autowired
private RuntimeService runtimeService;
@Autowired
private FlowableBaseEventListenerImpl flowableBaseEventListener;
@Override
public void run(String... args) {
runtimeService.addEventListener(flowableBaseEventListener);
}
}
@DependsOn("flowableBaseEventListenerImpl")
@Configuration
public class FlowableGlobalListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
@Resource
private SpringProcessEngineConfiguration configuration;
@Resource
private FlowableBaseEventListenerImpl flowableBaseEventListener;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
// 添加事件监听器,监听流程结束、任务创建、任务完成事件
dispatcher.addEventListener(flowableBaseEventListener, FlowableEngineEventType.TASK_CREATED, FlowableEngineEventType.TASK_COMPLETED, FlowableEngineEventType.PROCESS_COMPLETED);
}
}
(3)执行监听器
实现ExecutionListener接口(org.flowable.engine.delegate.ExecutionListener):
@Component
public class TestExecutionListener implements ExecutionListener {
@Override
public void notify(DelegateExecution delegateExecution) {
}
}
在流程xml文件中的配置表示为:
<userTask id="Activity_0jrxma4" flowable:dataType="fixed" flowable:userType="candidateUsers" flowable:candidateUsers="43">
<extensionElements>
<flowable:executionListener delegateExpression="${testExecutionListener}" event="start" />
</extensionElements>
</userTask>