From 4568783e2eb92cd60906f7c6e42174ce8ff50f6d Mon Sep 17 00:00:00 2001 From: "459816669@qq.com" <459816669@qq.com> Date: Mon, 7 Dec 2020 22:18:39 +0800 Subject: [PATCH] =?UTF-8?q?flowable=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/snow/SnowApplication.java | 6 +- .../flowable/common/SpringContextUtil.java | 37 +++++++++++ .../listener/AbstractEventListener.java | 62 +++++++++++-------- 3 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 snow-flowable/src/main/java/com/snow/flowable/common/SpringContextUtil.java diff --git a/snow-admin/src/main/java/com/snow/SnowApplication.java b/snow-admin/src/main/java/com/snow/SnowApplication.java index ffa9281..6cc44c9 100644 --- a/snow-admin/src/main/java/com/snow/SnowApplication.java +++ b/snow-admin/src/main/java/com/snow/SnowApplication.java @@ -1,5 +1,6 @@ package com.snow; +import com.snow.flowable.common.SpringContextUtil; import org.flowable.ui.common.rest.idm.remote.RemoteAccountResource; import org.flowable.ui.modeler.properties.FlowableModelerAppProperties; import org.springframework.boot.SpringApplication; @@ -8,6 +9,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; @@ -37,7 +39,9 @@ public class SnowApplication public static void main(String[] args) { // System.setProperty("spring.devtools.restart.enabled", "false"); - SpringApplication.run(SnowApplication.class, args); + //SpringApplication.run(SnowApplication.class, args); + ApplicationContext context=SpringApplication.run(SnowApplication.class, args); + SpringContextUtil.setApplicationContext(context); } diff --git a/snow-flowable/src/main/java/com/snow/flowable/common/SpringContextUtil.java b/snow-flowable/src/main/java/com/snow/flowable/common/SpringContextUtil.java new file mode 100644 index 0000000..3585cff --- /dev/null +++ b/snow-flowable/src/main/java/com/snow/flowable/common/SpringContextUtil.java @@ -0,0 +1,37 @@ +package com.snow.flowable.common; + + +import org.springframework.context.ApplicationContext; + +/** + * @program: snow + * @description + * @author: 没用的阿吉 + * @create: 2020-12-07 21:56 + **/ +public class SpringContextUtil { + + + private static ApplicationContext applicationContext; + + //获取上下文 + public static ApplicationContext getApplicationContext() { + return applicationContext; + } + + //设置上下文 + public static void setApplicationContext(ApplicationContext applicationContext) { + SpringContextUtil.applicationContext = applicationContext; + } + + //通过名字获取上下文中的bean + public static Object getBean(String name){ + return applicationContext.getBean(name); + } + + //通过类型获取上下文中的bean + public static Object getBean(Class requiredType){ + return applicationContext.getBean(requiredType); + } + +} diff --git a/snow-flowable/src/main/java/com/snow/flowable/listener/AbstractEventListener.java b/snow-flowable/src/main/java/com/snow/flowable/listener/AbstractEventListener.java index 94a1536..4999348 100644 --- a/snow-flowable/src/main/java/com/snow/flowable/listener/AbstractEventListener.java +++ b/snow-flowable/src/main/java/com/snow/flowable/listener/AbstractEventListener.java @@ -1,21 +1,29 @@ 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.impl.event.FlowableEntityEventImpl; + 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.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl; + import java.util.Map; @@ -25,55 +33,59 @@ import java.util.Map; * @Description: 全局事件监听 * @date 2020/12/7 18:13 */ -@Component @Slf4j public class AbstractEventListener implements FlowableEventListener { - @Autowired - private FlowableServiceImpl flowableService; - @Autowired - private RepositoryService repositoryService; + @Override public void onEvent(FlowableEvent flowableEvent) { - if (!(flowableEvent instanceof FlowableEntityEventImpl)) { + RepositoryService repositoryService = (RepositoryService)SpringContextUtil.getBean(RepositoryService.class); + + FlowableServiceImpl flowableService = (FlowableServiceImpl)SpringContextUtil.getBean(FlowableServiceImpl.class); + + if (!(flowableEvent instanceof FlowableEngineEventImpl)) { return; } - FlowableEntityEventImpl entityEvent = (FlowableEntityEventImpl) flowableEvent; + FlowableEngineEventImpl entityEvent = (FlowableEngineEventImpl) flowableEvent; log.info("监听到的事件类型:{}",entityEvent.getType()); - Object entity = entityEvent.getEntity(); - //是否是任务实体类 - if (!(entity instanceof TaskEntity)) { - return; - } - TaskEntity taskEntity = (TaskEntity) entity; + FlowableEngineEventType type = entityEvent.getType(); - log.info("监听到的事件类型:{}",type.name()); + // log.info("监听到的事件类型:{}",type.name()); String processInstanceId = entityEvent.getProcessInstanceId(); - if(type.name().equals("TASK_CREATED")){ - Object variableLocal = taskEntity.getVariableLocal(FlowConstants.IS_PASS); - ProcessDefinition processDefinition = repositoryService.getProcessDefinition(processInstanceId); - if(processDefinition.getName().equals(FlowDefEnum.SNOW_OA_LEAVE.getCode())){ - HistoricProcessInstance historicProcessInstance = flowableService.getHistoricProcessInstanceById(processInstanceId); - Map processVariables = historicProcessInstance.getProcessVariables(); + 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 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_COMPLETED")){ + if(type.name().equals("TASK_CREATED")){ //todo - log.info("任务已完成,这个时候可以处理一些业务逻辑"); + log.info("任务创建,这个时候可以处理一些业务逻辑"); } }