完善监听器

This commit is contained in:
jinqiming 2020-12-08 20:01:04 +08:00
parent 4568783e2e
commit f542340577
5 changed files with 560 additions and 83 deletions

View File

@ -9,7 +9,7 @@ package com.snow.flowable.common.enums;
public enum FlowDefEnum {
SNOW_OA_LEAVE("snow_oa_leave", "请假申请流程"),
NO_FINISHED("", "");
NO_FINISHED("snow_oa_leave111", "请假申请流程111");
private final String code;
private final String info;

View File

@ -1,12 +1,13 @@
package com.snow.flowable.config;
import com.google.common.collect.Lists;
import com.snow.flowable.listener.AbstractEventListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.event.support.TypedEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.EngineDeployer;
import org.flowable.engine.impl.rules.RulesDeployer;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@ -14,7 +15,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author qimingjin
@ -23,12 +24,13 @@ import java.util.List;
* @date 2020/11/18 19:16
*/
@Configuration
public class FlowableConfig{
@Slf4j
public class FlowableConfig {
@Primary
@Bean(name = "processEngineConfiguration")
public SpringProcessEngineConfiguration getSpringProcessEngineConfiguration(DataSource dataSource, DataSourceTransactionManager transactionManager) {
public SpringProcessEngineConfiguration getSpringProcessEngineConfiguration(ApplicationContext applicationContext, DataSource dataSource, DataSourceTransactionManager transactionManager) {
SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
configuration.setDataSource(dataSource);
configuration.setTransactionManager(transactionManager);
@ -40,10 +42,11 @@ public class FlowableConfig{
add(new RulesDeployer());
}
});
//注入全局监听器
List<FlowableEventListener> flowableEventListenerList=Lists.newArrayList();
flowableEventListenerList.add(new AbstractEventListener());
configuration.setEventListeners(flowableEventListenerList);
//具体事件的监听器
Map<String, AbstractEventListener> beanListeners = applicationContext.getBeansOfType(AbstractEventListener.class);
ArrayList<FlowableEventListener> flowableEventListeners = new ArrayList(beanListeners.values());
flowableEventListeners.removeIf(eventListener -> eventListener instanceof TypedEventListener);
configuration.setEventListeners(flowableEventListeners);
//设置流程图显示乱码
configuration.setActivityFontName("宋体");
configuration.setLabelFontName("宋体");

View File

@ -1,99 +1,417 @@
package com.snow.flowable.listener;
import com.alibaba.fastjson.JSON;
import com.snow.flowable.common.SpringContextUtil;
import com.snow.flowable.common.constants.FlowConstants;
import com.snow.flowable.common.enums.FlowDefEnum;
import com.snow.flowable.service.FlowableService;
import com.snow.flowable.service.impl.FlowableServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.Task;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.event.FlowableEngineEventImpl;
import org.flowable.common.engine.api.delegate.event.*;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.event.*;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.variable.api.event.FlowableVariableEvent;
import org.springframework.stereotype.Service;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
* @author qimingjin
* @Title:
* @Description: 全局事件监听
* @Title: 全局事件监听
* @Description:
* 每一个类型对应于org.flowable.engine.common.api.delegate.event.FlowableEventType下的一个枚举值
* ENGINE_CREATED该监听器所附加的process engine已被创建且准备好接受API调用org.flowableFlowableEvent
* ENGINE_CLOSED该监听器所附加的process engine已被关闭且API调用将不可用org.flowableFlowableEvent
* ENTITY_CREATED一个新entity被创建这个新entity包含在事件中org.flowableFlowableEntityEvent
* ENTITY_INITIALIZED一个新entity被创建并被完全初始化如果该entity有子entity创建则该事件直到所有子entity完成创建后才会发出org.flowableFlowableEntityEvent
* ENTITY_UPDATED一个已存在的entity被升级被升级的entity包含在事件中org.flowableFlowableEntityEvent
* ENTITY_DELETED一个已存在的entity被删除被删除的entity包含在事件中org.flowableFlowableEntityEvent
* ENTITY_SUSPENDED一个已存在的entity被暂停被暂停的entity包含在事件中将被ProcessDefinitionsProcessInstancesTasks分派org.flowableFlowableEntityEvent
* ENTITY_ACTIVATED一个已存在的entity被激活被激活的entity包含在事件中将被ProcessDefinitionsProcessInstancesTasks分派org.flowableFlowableEntityEvent
* JOB_EXECUTION_SUCCESS一项作业已执行成功该事件包含被执行的作业org.flowableFlowableEntityEvent
* JOB_EXECUTION_FAILURE一项作业已执行失败该事件包含被执行的作业及异常org.flowableFlowableEntityEvent和org.flowableFlowableExceptionEvent
* JOB_RETRIES_DECREMENTED由于作业失败作业重试次数已减少该事件包含已更新的作业org.flowableFlowableEntityEvent
* TIMER_SCHEDULED一项记时作业被创建并计划在未来某个时间点执行org.flowableFlowableEntityEvent
* TIMER_FIRED计时器被触发该事件包含已被执行的作业org.flowableFlowableEntityEvent
* JOB_CANCELED作业被取消该事件包含已被取消的作业在新的流程定义部署中可以通过API调用取消作业完成任务并取消关联的边界计时器org.flowableFlowableEntityEvent
* ACTIVITY_STARTED一项活动开始执行org.flowableFlowableActivityEvent
* ACTIVITY_COMPLETED一项活动执行成功org.flowableFlowableActivityEvent
* ACTIVITY_CANCELLED一项活动将被取消可能有三个原因MessageEventSubscriptionEntity, SignalEventSubscriptionEntity, TimerEntityorg.flowableFlowableActivityCancelledEvent
* ACTIVITY_SIGNALED一项活动收到信号org.flowableFlowableSignalEvent
* ACTIVITY_MESSAGE_RECEIVED一项活动收到了消息在活动收到消息之前进行调度收到后将根据类型边界事件或事件子进程启动事件为此活动调度ACTIVITY_SIGNAL或ACTIVITY_STARTEDorg.flowableFlowableMessageEvent
* ACTIVITY_MESSAGE_WAITING一项活动创建了一个消息事件订阅且正在等待接收org.flowableFlowableMessageEvent
* ACTIVITY_MESSAGE_CANCELLED已经为其创建了消息事件订阅的活动被取消因此接收该消息将不再触发该特定消息org.flowableFlowableMessageEvent
* ACTIVITY_ERROR_RECEIVED一项活动收到错误事件在活动处理实际错误之前进行调度事件的activityId包含对错误处理活动的引用如果错误成功传递则此事件将跟随ACTIVITY_SIGNALLED或ACTIVITY_COMPLETE用于所涉及的活动org.flowableFlowableErrorEvent
* UNCAUGHT_BPMN_ERROR抛出未处理的BPMN错误该process没有针对该特定错误的任何处理程序此事件的activityId将为空org.flowableFlowableErrorEvent
* ACTIVITY_COMPENSATE一项活动即将得到补偿该事件包含将要执行以进行补偿的活动的IDorg.flowableFlowableActivityEvent
* MULTI_INSTANCE_ACTIVITY_STARTED一个多实例活动将被执行org.flowableFlowableMultiInstanceActivityEvent
* MULTI_INSTANCE_ACTIVITY_COMPLETED一个多实例活动已成功执行完毕org.flowableFlowableMultiInstanceActivityEvent
* MULTI_INSTANCE_ACTIVITY_CANCELLED一个多实例活动将被取消可能有三个原因MessageEventSubscriptionEntity, SignalEventSubscriptionEntity, TimerEntityorg.flowableFlowableMultiInstanceActivityCancelledEvent
* VARIABLE_CREATED创建变量该事件包含变量名称以及相关执行和任务org.flowableFlowableVariableEvent
* VARIABLE_UPDATED现有变量已更新该事件包含变量名称更新值以及相关执行和任务如果有org.flowableFlowableVariableEvent
* VARIABLE_DELETED现有变量已删除该事件包含变量名称最后的值以及相关执行和任务如果有org.flowableFlowableVariableEvent
* TASK_ASSIGNED一个任务被分配给用户该事件包含任务org.flowableFlowableEntityEvent
* TASK_CREATED一个任务被创建这是在ENTITY_CREATE事件之后调度的如果任务是process的一部分则在执行任务监听器之前将触发此事件org.flowableFlowableEntityEvent
* TASK_COMPLETED一个任务已完成这是在ENTITY_DELETE事件之前调度的如果该任务是process的一部分则在process继续之前将触发此事件之后将跟随触发ACTIVITY_COMPLETE事件该事件指向表示已完成任务的活动org.flowableFlowableEntityEvent
* PROCESS_CREATED一个流程实例已经被创建所有的基础属性被设置但是还没有变量org.flowableFlowableEntityEvent
* PROCESS_STARTED一个流程实例已启动在启动先前创建的流程实例时调度在关联事件ENTITY_INITIALIZED之后和设置变量之后调度事件PROCESS_STARTEDorg.flowableFlowableEntityEvent
* PROCESS_COMPLETED一个流程已完成表示流程实例已停止所有的执行在最后一个活动ACTIVITY_COMPLETED事件之后调度当流程达到流程实例没有任何转换的状态时流程就完成了org.flowableFlowableEntityEvent
* PROCESS_COMPLETED_WITH_TERMINATE_END_EVENT一个流程已经结束并达到最后的事件org.flowableFlowableProcessTerminatedEvent
* PROCESS_CANCELLED一个流程已被取消在从运行时删除流程实例之前调度例如可以通过API调用RuntimeService.deleteProcessInstance通过调用活动上的中断边界事件等方法来取消流程实例org.flowableFlowableCancelledEvent
* MEMBERSHIP_CREATED用户已添加到组中该事件包含所涉及的用户和组的IDorg.flowableFlowableMembershipEvent
* MEMBERSHIP_DELETED用户已从组中删除该事件包含所涉及的用户和组的IDorg.flowableFlowableMembershipEvent
* MEMBERSHIPS_DELETED所有成员都将从一个组中删除在删除成员之前抛出该事件因此仍可访问它们出于性能原因如果立即删除所有成员则不会抛出任何单独的MEMBERSHIP_DELETED事件org.flowableFlowableMembershipEvent
* 所有ENTITY_开头的事件都与引擎内entity相关下面的列表显示了为哪些实体分派了哪些实体事件的概述
*
* ENTITY_CREATED, ENTITY_INITIALIZED, ENTITY_DELETED: Attachment, Comment, Deployment, Execution, Group, IdentityLink, Job, Model, ProcessDefinition, ProcessInstance, Task, User.
* ENTITY_UPDATED: Attachment, Deployment, Execution, Group, IdentityLink, Job, Model, ProcessDefinition, ProcessInstance, Task, User.
* ENTITY_SUSPENDED, ENTITY_ACTIVATED: ProcessDefinition, ProcessInstance/Execution, Task.
* @date 2020/12/7 18:13
*/
@Slf4j
public class AbstractEventListener implements FlowableEventListener {
@Service
public abstract class AbstractEventListener extends AbstractFlowableEventListener {
/**
* 需要监听的类型集合
*/
protected Set<FlowableEngineEventType> types;
/**
* 监听的流程集合
*/
protected Set<FlowDefEnum> flowDefEnums;
public AbstractEventListener() {}
public AbstractEventListener(Set<FlowableEngineEventType> types,Set<FlowDefEnum> flowDefEnums) {
this.types = types;
this.flowDefEnums = flowDefEnums;
}
@Override
public void onEvent(FlowableEvent flowableEvent) {
RepositoryService repositoryService = (RepositoryService)SpringContextUtil.getBean(RepositoryService.class);
FlowableServiceImpl flowableService = (FlowableServiceImpl)SpringContextUtil.getBean(FlowableServiceImpl.class);
if (!(flowableEvent instanceof FlowableEngineEventImpl)) {
return;
}
FlowableEngineEventImpl entityEvent = (FlowableEngineEventImpl) flowableEvent;
log.info("监听到的事件类型:{}",entityEvent.getType());
FlowableEngineEventType type = entityEvent.getType();
// log.info("监听到的事件类型:{}",type.name());
String processInstanceId = entityEvent.getProcessInstanceId();
if(type.name().equals("TASK_COMPLETED")){
//todo
log.info("任务已完成,这个时候可以处理一些业务逻辑");
ProcessDefinition processDefinition = repositoryService.getProcessDefinition(entityEvent.getProcessDefinitionId());
if(processDefinition.getKey().equals(FlowDefEnum.SNOW_OA_LEAVE.getCode())){
ProcessInstance processInstance= flowableService.getProcessInstanceById(processInstanceId);
Map<String, Object> processVariables = processInstance.getProcessVariables();
processVariables.get(FlowConstants.IS_PASS);
log.info("processVariables{}",JSON.toJSONString(processVariables));
}
FlowableEntityEventImpl flowableEntityEvent = (FlowableEntityEventImpl) flowableEvent;
log.info("flowableEntityEvent:{}",JSON.toJSONString(flowableEntityEvent));
TaskEntity entity = (TaskEntity)flowableEntityEvent.getEntity();
Object variable = entity.getVariable(FlowConstants.IS_PASS);
log.info("entity:{}",JSON.toJSONString(entity));
}
if(type.name().equals("TASK_ASSIGNED")){
FlowableEntityEventImpl flowableEntityEvent = (FlowableEntityEventImpl) flowableEvent;
log.info("TASK_ASSIGNED-flowableEntityEvent:{}",JSON.toJSONString(flowableEntityEvent));
//todo
log.info("任务已分配,这个时候可以发送短信,邮件通知");
}
if(type.name().equals("TASK_CREATED")){
//todo
log.info("任务创建,这个时候可以处理一些业务逻辑");
if(flowableEvent instanceof FlowableEngineEvent) {
FlowableEngineEvent flowableEngineEvent = (FlowableEngineEvent) flowableEvent;
initProcessDefinition(flowableEngineEvent);
}
}
/**
* 初始化事件类型
* @param flowableEngineEvent
*/
public void initEngineEventType(FlowableEngineEvent flowableEngineEvent){
FlowableEngineEventType engineEventType = (FlowableEngineEventType) flowableEngineEvent.getType();
if(types == null || types.contains(engineEventType)) {
switch (engineEventType) {
case ENTITY_CREATED:
entityCreated((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case ENTITY_INITIALIZED:
entityInitialized((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case ENTITY_UPDATED:
entityUpdated((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case ENTITY_DELETED:
entityDeleted((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case ENTITY_SUSPENDED:
entitySuspended((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case ENTITY_ACTIVATED:
entityActivated((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case TIMER_SCHEDULED:
timerScheduled((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case TIMER_FIRED:
timerFired((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case JOB_CANCELED:
jobCancelled((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case JOB_EXECUTION_SUCCESS:
jobExecutionSuccess((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case JOB_EXECUTION_FAILURE:
jobExecutionFailure((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case JOB_RETRIES_DECREMENTED:
jobRetriesDecremented((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case JOB_RESCHEDULED:
jobRescheduled((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case CUSTOM:
custom(flowableEngineEvent);
break;
case ENGINE_CREATED:
engineCreated((FlowableProcessEngineEvent) flowableEngineEvent);
break;
case ENGINE_CLOSED:
engineClosed((FlowableProcessEngineEvent) flowableEngineEvent);
break;
case ACTIVITY_STARTED:
activityStarted((FlowableActivityEvent) flowableEngineEvent);
break;
case ACTIVITY_COMPLETED:
activityCompleted((FlowableActivityEvent) flowableEngineEvent);
break;
case ACTIVITY_CANCELLED:
activityCancelled((FlowableActivityCancelledEvent) flowableEngineEvent);
break;
case MULTI_INSTANCE_ACTIVITY_STARTED:
multiInstanceActivityStarted((FlowableMultiInstanceActivityEvent) flowableEngineEvent);
break;
case MULTI_INSTANCE_ACTIVITY_COMPLETED:
multiInstanceActivityCompleted((FlowableMultiInstanceActivityCompletedEvent) flowableEngineEvent);
break;
case MULTI_INSTANCE_ACTIVITY_COMPLETED_WITH_CONDITION:
multiInstanceActivityCompletedWithCondition((FlowableMultiInstanceActivityCompletedEvent) flowableEngineEvent);
break;
case MULTI_INSTANCE_ACTIVITY_CANCELLED:
multiInstanceActivityCancelled((FlowableMultiInstanceActivityCancelledEvent) flowableEngineEvent);
break;
case ACTIVITY_SIGNAL_WAITING:
activitySignalWaiting((FlowableSignalEvent) flowableEngineEvent);
break;
case ACTIVITY_SIGNALED:
activitySignaled((FlowableSignalEvent) flowableEngineEvent);
break;
case ACTIVITY_COMPENSATE:
activityCompensate((FlowableActivityEvent) flowableEngineEvent);
break;
case ACTIVITY_MESSAGE_WAITING:
activityMessageWaiting((FlowableMessageEvent) flowableEngineEvent);
break;
case ACTIVITY_MESSAGE_RECEIVED:
activityMessageReceived((FlowableMessageEvent) flowableEngineEvent);
break;
case ACTIVITY_MESSAGE_CANCELLED:
activityMessageCancelled((FlowableMessageEvent) flowableEngineEvent);
break;
case ACTIVITY_ERROR_RECEIVED:
activityErrorReceived((FlowableErrorEvent) flowableEngineEvent);
break;
case HISTORIC_ACTIVITY_INSTANCE_CREATED:
historicActivityInstanceCreated((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case HISTORIC_ACTIVITY_INSTANCE_ENDED:
historicActivityInstanceEnded((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case SEQUENCEFLOW_TAKEN:
sequenceFlowTaken((FlowableSequenceFlowTakenEvent) flowableEngineEvent);
break;
case VARIABLE_CREATED:
variableCreated((FlowableVariableEvent) flowableEngineEvent);
break;
case VARIABLE_UPDATED:
variableUpdatedEvent((FlowableVariableEvent) flowableEngineEvent);
break;
case VARIABLE_DELETED:
variableDeletedEvent((FlowableVariableEvent) flowableEngineEvent);
break;
case TASK_CREATED:
taskCreated((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case TASK_ASSIGNED:
taskAssigned((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case TASK_COMPLETED:
taskCompleted((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case PROCESS_CREATED:
processCreated((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case PROCESS_STARTED:
processStarted((FlowableProcessStartedEvent) flowableEngineEvent);
break;
case PROCESS_COMPLETED:
processCompleted((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case PROCESS_COMPLETED_WITH_TERMINATE_END_EVENT:
processCompletedWithTerminateEnd((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case PROCESS_COMPLETED_WITH_ERROR_END_EVENT:
processCompletedWithErrorEnd((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case PROCESS_CANCELLED:
processCancelled((FlowableCancelledEvent) flowableEngineEvent);
break;
case HISTORIC_PROCESS_INSTANCE_CREATED:
historicProcessInstanceCreated((FlowableEngineEntityEvent) flowableEngineEvent);
break;
case HISTORIC_PROCESS_INSTANCE_ENDED:
historicProcessInstanceEnded((FlowableEngineEntityEvent) flowableEngineEvent);
break;
}
}
}
public void initProcessDefinition(FlowableEngineEvent flowableEngineEvent){
ProcessDefinitionEntity processDefinition = getProcessDefinition(flowableEngineEvent);
if(flowDefEnums!=null&&processDefinition!=null){
String key = Optional.ofNullable(processDefinition.getKey()).orElse("");
for(FlowDefEnum flowDefEnum:flowDefEnums){
//在流程中存在的才监听
if(flowDefEnum.getCode().equals(key)){
initEngineEventType(flowableEngineEvent);
execute();
}
}
}
}
protected void execute() {
try {
process();
} catch (RuntimeException e) {
log.error("执行监听异常", e);
throw e;
}
}
/**
* 抽象需要执行的程序类
*/
protected abstract void process();
@Override
public boolean isFailOnException() {
return false;
return true;
}
protected void entityCreated(FlowableEngineEntityEvent event) {}
protected void entityInitialized(FlowableEngineEntityEvent event) {}
protected void entityUpdated(FlowableEngineEntityEvent event) {}
protected void entityDeleted(FlowableEngineEntityEvent event) {}
protected void entitySuspended(FlowableEngineEntityEvent event) {}
protected void entityActivated(FlowableEngineEntityEvent event) {}
protected void timerScheduled(FlowableEngineEntityEvent event) {}
protected void timerFired(FlowableEngineEntityEvent event) {}
protected void jobCancelled(FlowableEngineEntityEvent event) {}
protected void jobExecutionSuccess(FlowableEngineEntityEvent event) {}
protected void jobExecutionFailure(FlowableEngineEntityEvent event) {}
protected void jobRetriesDecremented(FlowableEngineEntityEvent event) {}
protected void jobRescheduled(FlowableEngineEntityEvent event) {}
protected void custom(FlowableEngineEvent event) {}
protected void engineCreated(FlowableProcessEngineEvent event) {}
protected void engineClosed(FlowableProcessEngineEvent flowableEngineEvent) {}
protected void activityStarted(FlowableActivityEvent event) {}
protected void activityCompleted(FlowableActivityEvent event) {}
protected void activityCancelled(FlowableActivityCancelledEvent event) {}
protected void multiInstanceActivityStarted(FlowableMultiInstanceActivityEvent event) {}
protected void multiInstanceActivityCompleted(FlowableMultiInstanceActivityCompletedEvent event) {}
protected void multiInstanceActivityCompletedWithCondition(FlowableMultiInstanceActivityCompletedEvent event) {}
protected void multiInstanceActivityCancelled(FlowableMultiInstanceActivityCancelledEvent event) {}
protected void activitySignalWaiting(FlowableSignalEvent event) {}
protected void activitySignaled(FlowableSignalEvent event) {}
protected void activityCompensate(FlowableActivityEvent event) {}
protected void activityMessageWaiting(FlowableMessageEvent event) {}
protected void activityMessageReceived(FlowableMessageEvent event) {}
protected void activityMessageCancelled(FlowableMessageEvent event) {}
protected void activityErrorReceived(FlowableErrorEvent event) {}
protected void historicActivityInstanceCreated(FlowableEngineEntityEvent event) {}
protected void historicActivityInstanceEnded(FlowableEngineEntityEvent event) {}
protected void sequenceFlowTaken(FlowableSequenceFlowTakenEvent event) {}
protected void variableCreated(FlowableVariableEvent event) {}
protected void variableUpdatedEvent(FlowableVariableEvent event) {}
protected void variableDeletedEvent(FlowableVariableEvent event) {}
protected void taskCreated(FlowableEngineEntityEvent event) {}
protected void taskAssigned(FlowableEngineEntityEvent event) {}
protected void taskCompleted(FlowableEngineEntityEvent event) {}
protected void processCreated(FlowableEngineEntityEvent event) {}
protected void processStarted(FlowableProcessStartedEvent event) {}
protected void processCompleted(FlowableEngineEntityEvent event) {}
protected void processCompletedWithTerminateEnd(FlowableEngineEntityEvent event) {}
protected void processCompletedWithErrorEnd(FlowableEngineEntityEvent event) {}
protected void processCancelled(FlowableCancelledEvent event) {}
protected void historicProcessInstanceCreated(FlowableEngineEntityEvent event) {}
protected void historicProcessInstanceEnded(FlowableEngineEntityEvent event) {}
/**
* 获取流程执行信息
* @param event
* @return
*/
protected DelegateExecution getExecution(FlowableEngineEvent event) {
String executionId = event.getExecutionId();
if (executionId != null) {
CommandContext commandContext = CommandContextUtil.getCommandContext();
if (commandContext != null) {
return CommandContextUtil.getExecutionEntityManager(commandContext).findById(executionId);
}
}
return null;
}
/**
* 获取流程定义信息
* @param event
* @return
*/
protected ProcessDefinitionEntity getProcessDefinition(FlowableEngineEvent event) {
String processDefinitionId = event.getProcessDefinitionId();
if (processDefinitionId != null) {
CommandContext commandContext = CommandContextUtil.getCommandContext();
if (commandContext != null) {
return CommandContextUtil.getProcessDefinitionEntityManager(commandContext).findById(processDefinitionId);
}
}
return null;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;

View File

@ -0,0 +1,70 @@
package com.snow.flowable.listener.common;
import com.alibaba.fastjson.JSON;
import com.snow.flowable.common.enums.FlowDefEnum;
import com.snow.flowable.listener.AbstractEventListener;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/12/8 14:38
*/
@Slf4j
@Service
public class SendMessageEventLister extends AbstractEventListener {
public SendMessageEventLister() {
super(
new HashSet<>(Arrays.asList(
FlowableEngineEventType.TASK_CREATED
)),
new HashSet<>(Arrays.asList(
FlowDefEnum.SNOW_OA_LEAVE
)));
}
@Override
protected void process() {
}
@Override
protected void taskCreated(FlowableEngineEntityEvent event) {
TaskEntity entity = (TaskEntity)event.getEntity();
ProcessDefinitionEntity processDefinition = getProcessDefinition(event);
//根据任务ID获取任务获选人人
Set<IdentityLink> candidates = entity.getCandidates();
if(!CollectionUtils.isEmpty(candidates)){
candidates.forEach(t->{
String userId = t.getUserId();
String groupId = t.getGroupId();
if(!StringUtils.isEmpty(userId)){
sendMessage();
} else if(!StringUtils.isEmpty(groupId)) {
sendMessage();
}
});
}
//任务创建可发送短信邮件通知接收人
log.info("ManagerTaskEventListener----taskCreated任务创建监听{}",JSON.toJSONString(event));
}
public void sendMessage(){
}
}

View File

@ -0,0 +1,86 @@
package com.snow.flowable.listener.leave;
import com.alibaba.fastjson.JSON;
import com.snow.common.enums.ProcessStatus;
import com.snow.flowable.common.SpringContextUtil;
import com.snow.flowable.common.constants.FlowConstants;
import com.snow.flowable.common.enums.FlowDefEnum;
import com.snow.flowable.listener.AbstractEventListener;
import com.snow.system.domain.SysOaLeave;
import com.snow.system.service.impl.SysOaLeaveServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/12/8 10:05
*/
@Slf4j
@Service
public class ManagerTaskEventListener extends AbstractEventListener {
public ManagerTaskEventListener() {
super(
new HashSet<>(Arrays.asList(
FlowableEngineEventType.TASK_ASSIGNED,
FlowableEngineEventType.TASK_COMPLETED
)),
new HashSet<>(Arrays.asList(
FlowDefEnum.SNOW_OA_LEAVE
)));
}
@Override
protected void process() {
}
@Override
protected void taskAssigned(FlowableEngineEntityEvent event) {
TaskEntity entity = (TaskEntity)event.getEntity();
ProcessDefinitionEntity processDefinition = getProcessDefinition(event);
//任务创建可发送短信邮件通知接收人
log.info("ManagerTaskEventListener----taskAssigned{}",JSON.toJSONString(event));
}
@Override
protected void taskCompleted(FlowableEngineEntityEvent event) {
SysOaLeaveServiceImpl sysOaLeaveService = (SysOaLeaveServiceImpl)SpringContextUtil.getBean(SysOaLeaveServiceImpl.class);
//任务完成做一些业务处理
log.info("ManagerTaskEventListener----taskCompleted任务完成监听{}",JSON.toJSONString(event));
TaskEntity entity = (TaskEntity)event.getEntity();
DelegateExecution execution = getExecution(event);
SysOaLeave sysOaLeave=new SysOaLeave();
sysOaLeave.setLeaveNo(execution.getProcessInstanceBusinessKey());
Optional.ofNullable(entity.getVariableLocal(FlowConstants.IS_PASS)).ifPresent(t->{
Boolean isPass = (Boolean)t;
if(!isPass){
sysOaLeave.setProcessStatus(ProcessStatus.REJECT.ordinal());
}
});
sysOaLeaveService.updateSysOaLeaveByLeaveNo(sysOaLeave);
}
@Override
public boolean isFailOnException() {
return false;
}
}