完善监听器

This commit is contained in:
jinqiming 2020-12-09 18:08:28 +08:00
parent f542340577
commit 41c192e28b
16 changed files with 387 additions and 115 deletions

View File

@ -170,7 +170,7 @@ public class SysOaLeaveController extends BaseController
BeanUtils.copyProperties(sysOaLeave,sysOaLeaveForm); BeanUtils.copyProperties(sysOaLeave,sysOaLeaveForm);
sysOaLeaveForm.setBusinessKey(newSysOaLeave.getLeaveNo()); sysOaLeaveForm.setBusinessKey(newSysOaLeave.getLeaveNo());
sysOaLeaveForm.setStartUserId(String.valueOf(sysUser.getUserId())); sysOaLeaveForm.setStartUserId(String.valueOf(sysUser.getUserId()));
sysOaLeaveForm.setClassInfoJson(com.alibaba.fastjson.JSON.toJSONString(newSysOaLeave)); sysOaLeaveForm.setBusVarJson(com.alibaba.fastjson.JSON.toJSONString(newSysOaLeave));
sysOaLeaveForm.setClassPackName(SysOaLeaveForm.class.getCanonicalName()); sysOaLeaveForm.setClassPackName(SysOaLeaveForm.class.getCanonicalName());
ProcessInstance processInstance = flowableService.startProcessInstanceByAppForm(sysOaLeaveForm); ProcessInstance processInstance = flowableService.startProcessInstanceByAppForm(sysOaLeaveForm);
//提交 //提交

View File

@ -24,6 +24,8 @@ public enum DingTalkListenerType {
CALL_BACK_DELETE(3,10, "回调删除"), CALL_BACK_DELETE(3,10, "回调删除"),
CALL_BACK_FAILED_RESULT(23,10, "获取回调失败结果"), CALL_BACK_FAILED_RESULT(23,10, "获取回调失败结果"),
WORK_RECODE_CREATE(1,20, "创建待办"),
; ;

View File

@ -28,6 +28,9 @@ public class SyncDingTalkInfoFactory {
else if(type.equals(DingTalkListenerType.CALL_BACK_REGISTER.getType())){ else if(type.equals(DingTalkListenerType.CALL_BACK_REGISTER.getType())){
return new CallBackService(); return new CallBackService();
} }
else if(type.equals(DingTalkListenerType.WORK_RECODE_CREATE.getType())){
return new WorkRecodeService();
}
else { else {

View File

@ -0,0 +1,32 @@
package com.snow.dingtalk.listener;
import com.alibaba.fastjson.JSON;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.common.utils.spring.SpringUtils;
import com.snow.dingtalk.model.WorkrecordAddRequest;
import com.snow.dingtalk.service.impl.WorkRecodeServiceImpl;
import com.snow.system.event.SyncEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author qimingjin
* @Title:
* @Description:
* @date 2020/12/9 15:03
*/
@Component
@Slf4j
public class WorkRecodeService implements ISyncDingTalkInfo {
private WorkRecodeServiceImpl workRecodeService=SpringUtils.getBean(WorkRecodeServiceImpl.class);
@Override
public void syncDingTalkInfoEvent(SyncEvent syncEvent) {
log.info("调用工作通知传入的原始参数:{}"+JSON.toJSONString(syncEvent));
DingTalkListenerType eventType =(DingTalkListenerType) syncEvent.getT();
Integer code = eventType.getCode();
if(code.equals(DingTalkListenerType.WORK_RECODE_CREATE.getCode())){
WorkrecordAddRequest workrecordAddRequest=(WorkrecordAddRequest)syncEvent.getSource();
workRecodeService.create(workrecordAddRequest);
}
}
}

View File

@ -26,61 +26,21 @@ import org.springframework.stereotype.Service;
* @date 2020/9/21 14:28 * @date 2020/9/21 14:28
*/ */
@Service @Service
public class WorkRecodeService extends BaseService { public interface WorkRecodeService {
@Autowired
private ISysConfigService isysConfigService;
/** /**
* 创建工作待办 * 创建工作待办
* @param workrecordAddRequest * @param workrecordAddRequest
* @return * @return
*/ */
public String create(WorkrecordAddRequest workrecordAddRequest){ String create(WorkrecordAddRequest workrecordAddRequest);
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.WORK_RECORD_CREATE);
OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
BeanUtils.copyProperties(workrecordAddRequest,req);
OapiWorkrecordAddResponse rsp = null;
try {
rsp = client.execute(req, getDingTalkToken());
if (rsp.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_CREATE,rsp.getMessage(),"WorkRecordAddRequest",JSON.toJSONString(req));
return rsp.getRecordId();
}else {
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_CREATE,rsp.getErrmsg(),"WorkRecordAddRequest",JSON.toJSONString(req));
}
} catch (ApiException e) {
e.printStackTrace();
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_CREATE,e.getMessage(),"WorkRecordAddRequest",JSON.toJSONString(req));
}
return null;
}
/** /**
* 根据用户ID获取待办 * 根据用户ID获取待办
* @param workrecordGetbyuseridRequest * @param workrecordGetbyuseridRequest
* @return * @return
*/ */
public OapiWorkrecordGetbyuseridResponse.PageResult getWorkRecordByUserId(WorkrecordGetbyuseridRequest workrecordGetbyuseridRequest){ OapiWorkrecordGetbyuseridResponse.PageResult getWorkRecordByUserId(WorkrecordGetbyuseridRequest workrecordGetbyuseridRequest);
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.GET_WORK_RECORD_USER_ID_);
OapiWorkrecordGetbyuseridRequest req = new OapiWorkrecordGetbyuseridRequest();
req.setUserid(workrecordGetbyuseridRequest.getUserid());
req.setOffset(workrecordGetbyuseridRequest.getOffset());
req.setLimit(workrecordGetbyuseridRequest.getLimit());
req.setStatus(workrecordGetbyuseridRequest.getStatus());
try {
OapiWorkrecordGetbyuseridResponse rsp = client.execute(req, getDingTalkToken());
if(rsp.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,rsp.getMessage(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req));
return rsp.getRecords();
}else {
syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,rsp.getErrmsg(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req));
}
} catch (ApiException e) {
e.printStackTrace();
syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,e.getMessage(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req));
}
return null;
}
/** /**
* 更新待办 * 更新待办
@ -88,24 +48,5 @@ public class WorkRecodeService extends BaseService {
* @param recordId * @param recordId
* @return * @return
*/ */
public Boolean update(String userId,String recordId){ Boolean update(String userId,String recordId);
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.WORK_RECORD_UPDATE);
OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest();
req.setUserid(userId);
req.setRecordId(recordId);
OapiWorkrecordUpdateResponse rsp = null;
try {
rsp = client.execute(req, getDingTalkToken());
if(rsp.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,rsp.getMessage(),"WorkrecordUpdateRequest",JSON.toJSONString(req));
return rsp.getResult();
}else {
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,rsp.getErrmsg(),"WorkrecordUpdateRequest",JSON.toJSONString(req));
}
} catch (ApiException e) {
e.printStackTrace();
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,e.getMessage(),"WorkrecordUpdateRequest",JSON.toJSONString(req));
}
return false;
}
} }

View File

@ -0,0 +1,114 @@
package com.snow.dingtalk.service.impl;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiWorkrecordAddRequest;
import com.dingtalk.api.request.OapiWorkrecordGetbyuseridRequest;
import com.dingtalk.api.request.OapiWorkrecordUpdateRequest;
import com.dingtalk.api.response.OapiWorkrecordAddResponse;
import com.dingtalk.api.response.OapiWorkrecordGetbyuseridResponse;
import com.dingtalk.api.response.OapiWorkrecordUpdateResponse;
import com.snow.common.annotation.SyncLog;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.common.exception.SyncDataException;
import com.snow.dingtalk.common.BaseConstantUrl;
import com.snow.dingtalk.common.BaseService;
import com.snow.dingtalk.model.WorkrecordAddRequest;
import com.snow.dingtalk.model.WorkrecordGetbyuseridRequest;
import com.snow.system.service.ISysConfigService;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author qimingjin
* @Title: 待办事项
* @Description:
* @date 2020/12/9 14:53
*/
@Slf4j
@Service
public class WorkRecodeServiceImpl extends BaseService {
/**
* 创建工作待办
* @param workrecordAddRequest
* @return
*/
@SyncLog(dingTalkListenerType = DingTalkListenerType.WORK_RECODE_CREATE,dingTalkUrl=BaseConstantUrl.WORK_RECORD_CREATE)
public String create(WorkrecordAddRequest workrecordAddRequest){
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.WORK_RECORD_CREATE);
OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
BeanUtils.copyProperties(workrecordAddRequest,req);
OapiWorkrecordAddResponse rsp = null;
try {
rsp = client.execute(req, getDingTalkToken());
if (rsp.getErrcode()==0) {
return rsp.getRecordId();
}else {
throw new SyncDataException(JSON.toJSONString(req),rsp.getErrmsg());
}
} catch (ApiException e) {
log.error("钉钉workRecordAddRequest异常{}",e.getErrMsg());
throw new SyncDataException(JSON.toJSONString(req),e.getErrMsg());
}
}
/**
* 根据用户ID获取待办
* @param workrecordGetbyuseridRequest
* @return
*/
public OapiWorkrecordGetbyuseridResponse.PageResult getWorkRecordByUserId(WorkrecordGetbyuseridRequest workrecordGetbyuseridRequest){
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.GET_WORK_RECORD_USER_ID_);
OapiWorkrecordGetbyuseridRequest req = new OapiWorkrecordGetbyuseridRequest();
req.setUserid(workrecordGetbyuseridRequest.getUserid());
req.setOffset(workrecordGetbyuseridRequest.getOffset());
req.setLimit(workrecordGetbyuseridRequest.getLimit());
req.setStatus(workrecordGetbyuseridRequest.getStatus());
try {
OapiWorkrecordGetbyuseridResponse rsp = client.execute(req, getDingTalkToken());
if(rsp.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,rsp.getMessage(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req));
return rsp.getRecords();
}else {
syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,rsp.getErrmsg(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req));
}
} catch (ApiException e) {
e.printStackTrace();
syncDingTalkErrorOperLog(BaseConstantUrl.GET_WORK_RECORD_USER_ID_,e.getMessage(),"WorkrecordGetbyuseridRequest",JSON.toJSONString(req));
}
return null;
}
/**
* 更新待办
* @param userId
* @param recordId
* @return
*/
public Boolean update(String userId,String recordId){
DingTalkClient client = new DefaultDingTalkClient(BaseConstantUrl.WORK_RECORD_UPDATE);
OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest();
req.setUserid(userId);
req.setRecordId(recordId);
OapiWorkrecordUpdateResponse rsp = null;
try {
rsp = client.execute(req, getDingTalkToken());
if(rsp.getErrcode()==0){
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,rsp.getMessage(),"WorkrecordUpdateRequest",JSON.toJSONString(req));
return rsp.getResult();
}else {
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,rsp.getErrmsg(),"WorkrecordUpdateRequest",JSON.toJSONString(req));
}
} catch (ApiException e) {
e.printStackTrace();
syncDingTalkErrorOperLog(BaseConstantUrl.WORK_RECORD_UPDATE,e.getMessage(),"WorkrecordUpdateRequest",JSON.toJSONString(req));
}
return false;
}
}

View File

@ -15,7 +15,7 @@
<!-- 通用工具--> <!-- 通用工具-->
<dependency> <dependency>
<groupId>com.snow</groupId> <groupId>com.snow</groupId>
<artifactId>snow-framework</artifactId> <artifactId>snow-dingtalk</artifactId>
<version>4.3.1</version> <version>4.3.1</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -31,7 +31,7 @@ public class FlowConstants {
/** /**
* 业务参数常量 * 业务参数常量
*/ */
public static final String BUS_VAR="bus_var"; public static final String BUS_VAR="bus_var_json";
/** /**
* 当前类的包名 * 当前类的包名
*/ */

View File

@ -0,0 +1,63 @@
package com.snow.flowable.config;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import org.flowable.common.engine.impl.cfg.IdGenerator;
import cn.hutool.core.lang.Console;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author qimingjin
* @Title: 修改flowAble id 生成器
* @Description:
* @date 2020/12/9 14:01
*/
public class FlowIdGenerator implements IdGenerator {
@Override
public synchronized String getNextId() {
Snowflake snowflake = IdUtil.getSnowflake(2, 1);
long id = snowflake.nextId();
return String.valueOf(id);
}
//很多时候我们需要简单模拟N个线程调用某个业务测试其并发状况于是Hutool提供了一个简单的并发测试类ConcurrencyTester
public static void main(String[] args) {
ThreadPoolExecutor executor = ExecutorBuilder.create().
setCorePoolSize(5).
setMaxPoolSize(10).
setWorkQueue(new LinkedBlockingQueue<>(100))
.build();
executor.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
Snowflake snowflake = IdUtil.getSnowflake(2, 1);
long id = snowflake.nextId();
ThreadUtil.sleep(1000);
Console.log("{} test finished, delay: {}", Thread.currentThread().getName(), id);
}
}
});
executor.shutdown();
/* ConcurrencyTester tester = ThreadUtil.concurrencyTest(100, () -> {
// 测试的逻辑内容
Snowflake snowflake = IdUtil.getSnowflake(2, 1);
long id = snowflake.nextId();
// ThreadUtil.sleep(1);
Console.log("{} test finished, delay: {}", Thread.currentThread().getName(), id);
});
// 获取总的执行时间单位毫秒
Console.log(tester.getInterval());*/
}
}

View File

@ -36,6 +36,8 @@ public class FlowableConfig {
configuration.setTransactionManager(transactionManager); configuration.setTransactionManager(transactionManager);
configuration.setDatabaseSchemaUpdate("false"); configuration.setDatabaseSchemaUpdate("false");
configuration.setAsyncExecutorActivate(true); configuration.setAsyncExecutorActivate(true);
//修改id生成器
configuration.setIdGenerator(new FlowIdGenerator());
configuration.setCustomPostDeployers(new ArrayList<EngineDeployer>(){ configuration.setCustomPostDeployers(new ArrayList<EngineDeployer>(){
private static final long serialVersionUID = 4041439225480991716L; private static final long serialVersionUID = 4041439225480991716L;
{ {

View File

@ -2,11 +2,9 @@ package com.snow.flowable.domain;
import com.snow.flowable.common.enums.FlowDefEnum; import com.snow.flowable.common.enums.FlowDefEnum;
import lombok.Data; import lombok.Data;
import org.json.JSONObject;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map;
/** /**
* @program: snow * @program: snow
@ -24,6 +22,23 @@ public abstract class AppForm implements Serializable {
private String startUserId; private String startUserId;
/**
* 业务参数
*/
@NotBlank(message = "业务参数不能为空")
private String businessKey;
/**
* 业务数据json
*/
private String busVarJson;
/**
* 当前类的包名称
*/
@NotBlank(message = "当前类的包名称不能为空")
private String classPackName;
/** /**
* 流程申请单需实现此方法,返回申请单对应的流程定义. * 流程申请单需实现此方法,返回申请单对应的流程定义.
* 一个流程对应一个申请单.(暂时) * 一个流程对应一个申请单.(暂时)
@ -32,19 +47,4 @@ public abstract class AppForm implements Serializable {
*/ */
public abstract FlowDefEnum getFlowDef(); public abstract FlowDefEnum getFlowDef();
/**
* 业务参数
*/
@NotBlank(message = "业务参数不能为空")
private String businessKey;
/**
* 业务数据JSOn
*/
private String classInfoJson;
/**
* 当前类的包名称
*/
@NotBlank(message = "当前类的包名称不能为空")
private String classPackName;
} }

View File

@ -9,6 +9,7 @@ import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.event.*; import org.flowable.engine.delegate.event.*;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity; import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.flowable.engine.impl.util.CommandContextUtil; import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.variable.api.event.FlowableVariableEvent; import org.flowable.variable.api.event.FlowableVariableEvent;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -71,7 +72,6 @@ import java.util.Set;
@Slf4j @Slf4j
@Service @Service
public abstract class AbstractEventListener extends AbstractFlowableEventListener { public abstract class AbstractEventListener extends AbstractFlowableEventListener {
/** /**
* 需要监听的类型集合 * 需要监听的类型集合
*/ */
@ -82,6 +82,7 @@ public abstract class AbstractEventListener extends AbstractFlowableEventListen
*/ */
protected Set<FlowDefEnum> flowDefEnums; protected Set<FlowDefEnum> flowDefEnums;
public AbstractEventListener() {} public AbstractEventListener() {}
public AbstractEventListener(Set<FlowableEngineEventType> types,Set<FlowDefEnum> flowDefEnums) { public AbstractEventListener(Set<FlowableEngineEventType> types,Set<FlowDefEnum> flowDefEnums) {
@ -258,16 +259,17 @@ public abstract class AbstractEventListener extends AbstractFlowableEventListen
//在流程中存在的才监听 //在流程中存在的才监听
if(flowDefEnum.getCode().equals(key)){ if(flowDefEnum.getCode().equals(key)){
initEngineEventType(flowableEngineEvent); initEngineEventType(flowableEngineEvent);
execute(); //会监听多次
execute(flowableEngineEvent);
} }
} }
} }
} }
protected void execute() { protected void execute(FlowableEngineEvent flowableEngineEvent) {
try { try {
process(); process(flowableEngineEvent);
} catch (RuntimeException e) { } catch (RuntimeException e) {
log.error("执行监听异常", e); log.error("执行监听异常", e);
throw e; throw e;
@ -277,7 +279,7 @@ public abstract class AbstractEventListener extends AbstractFlowableEventListen
/** /**
* 抽象需要执行的程序类 * 抽象需要执行的程序类
*/ */
protected abstract void process(); protected abstract void process(FlowableEngineEvent flowableEngineEvent);
@ -412,6 +414,21 @@ public abstract class AbstractEventListener extends AbstractFlowableEventListen
} }
return null; return null;
} }
/**
* 获取流程执行信息
* @param executionId
* @return
*/
protected DelegateExecution getExecutionById(String executionId) {
if (executionId != null) {
CommandContext commandContext = CommandContextUtil.getCommandContext();
if (commandContext != null) {
return CommandContextUtil.getExecutionEntityManager(commandContext).findById(executionId);
}
}
return null;
}
@Override @Override
public boolean isFireOnTransactionLifecycleEvent() { public boolean isFireOnTransactionLifecycleEvent() {
return false; return false;

View File

@ -1,21 +1,40 @@
package com.snow.flowable.listener.common; package com.snow.flowable.listener.common;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ExecutorBuilder;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.snow.common.enums.DingTalkListenerType;
import com.snow.dingtalk.model.WorkrecordAddRequest;
import com.snow.flowable.common.SpringContextUtil;
import com.snow.flowable.common.enums.FlowDefEnum; import com.snow.flowable.common.enums.FlowDefEnum;
import com.snow.flowable.listener.AbstractEventListener; import com.snow.flowable.listener.AbstractEventListener;
import com.snow.flowable.service.FlowableService;
import com.snow.system.domain.SysUser;
import com.snow.system.event.SyncEvent;
import com.snow.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType; import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity; import org.flowable.engine.RepositoryService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.identitylink.api.IdentityLink; import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.service.impl.persistence.entity.TaskEntity; import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
/** /**
* @author qimingjin * @author qimingjin
@ -27,6 +46,9 @@ import java.util.Set;
@Service @Service
public class SendMessageEventLister extends AbstractEventListener { public class SendMessageEventLister extends AbstractEventListener {
@Resource
private ApplicationContext applicationContext;
public SendMessageEventLister() { public SendMessageEventLister() {
super( super(
new HashSet<>(Arrays.asList( new HashSet<>(Arrays.asList(
@ -38,33 +60,106 @@ public class SendMessageEventLister extends AbstractEventListener {
} }
@Override @Override
protected void process() { protected void process(FlowableEngineEvent flowableEngineEvent) {
DelegateExecution execution = getExecution(flowableEngineEvent);
log.info("process========>{}",JSON.toJSONString(execution));
} }
@Override @Override
protected void taskCreated(FlowableEngineEntityEvent event) { 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)); log.info("ManagerTaskEventListener----taskCreated任务创建监听{}",JSON.toJSONString(event));
sendDingTalkMessage(event);
} }
public void sendMessage(){ /**
* 发送钉钉工作通知消息
* @param event
*/
public void sendDingTalkMessage(FlowableEngineEntityEvent event){
SysUserMapper sysUserMapper = (SysUserMapper)SpringContextUtil.getBean(SysUserMapper.class);
ThreadPoolExecutor executor = ExecutorBuilder.create().setCorePoolSize(5)
.setMaxPoolSize(10)
.setWorkQueue(new LinkedBlockingQueue<>(100))
.build();
executor.execute(new Runnable() {
@Override
public void run() {
//根据任务ID获取任务获选人人
TaskEntity entity = (TaskEntity)event.getEntity();
Set<IdentityLink> candidates = entity.getCandidates();
if(!CollectionUtils.isEmpty(candidates)){
candidates.forEach(t->{
String userId = t.getUserId();
String groupId = t.getGroupId();
if(!StringUtils.isEmpty(userId)){
WorkrecordAddRequest workrecordAddRequest = initWorkRecordAddRequest(userId, event);
SyncEvent syncEventGroup = new SyncEvent(workrecordAddRequest, DingTalkListenerType.WORK_RECODE_CREATE);
applicationContext.publishEvent(syncEventGroup);
}
else if(!StringUtils.isEmpty(groupId)) {
List<SysUser> sysUsers = sysUserMapper.selectUserListByRoleId(groupId);
sysUsers.forEach(sysUser->{
WorkrecordAddRequest workrecordAddRequest = initWorkRecordAddRequest(String.valueOf(sysUser.getUserId()), event);
SyncEvent syncEventGroup = new SyncEvent(workrecordAddRequest, DingTalkListenerType.WORK_RECODE_CREATE);
applicationContext.publishEvent(syncEventGroup);
});
}else {
log.warn("ManagerTaskEventListener----taskCreated任务创建监听 userId和groupId is all null");
}
});
}
}
});
executor.shutdown();
} }
/**
* 组装参数
* @param userId
* @param event
* @return
*/
public WorkrecordAddRequest initWorkRecordAddRequest(String userId,FlowableEngineEntityEvent event){
FlowableService flowableService = (FlowableService)SpringContextUtil.getBean(FlowableService.class);
ProcessInstance processInstance = flowableService.getProcessInstanceById(event.getProcessInstanceId());
TaskEntity entity = (TaskEntity)event.getEntity();
ProcessDefinition processDefinition = getProcessDefinition(event.getProcessDefinitionId());
WorkrecordAddRequest workrecordAddRequest=new WorkrecordAddRequest();
workrecordAddRequest.setUserid(userId);
workrecordAddRequest.setBizId(processInstance.getBusinessKey());
workrecordAddRequest.setUrl(entity.getFormKey());
workrecordAddRequest.setPcUrl(entity.getFormKey());
workrecordAddRequest.setSourceName("DING-FLOW");
workrecordAddRequest.setPcOpenType(2L);
workrecordAddRequest.setTitle(entity.getName());
workrecordAddRequest.setCreateTime(entity.getCreateTime().getTime());
List<WorkrecordAddRequest.FormItemVo> formItemList=Lists.newArrayList();
WorkrecordAddRequest.FormItemVo formItemVo = new WorkrecordAddRequest.FormItemVo();
formItemVo.setTitle("流程名称:");
formItemVo.setContent(processDefinition.getName());
formItemList.add(formItemVo);
WorkrecordAddRequest.FormItemVo formItemVo1 = new WorkrecordAddRequest.FormItemVo();
formItemVo1.setTitle("创建时间:");
formItemVo1.setContent(DateUtil.format(entity.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
formItemList.add(formItemVo1);
WorkrecordAddRequest.FormItemVo formItemVo2 = new WorkrecordAddRequest.FormItemVo();
formItemVo2.setTitle("发起人:");
formItemVo2.setContent(processInstance.getStartUserId());
formItemList.add(formItemVo2);
workrecordAddRequest.setFormItemList(formItemList);
return workrecordAddRequest;
}
protected ProcessDefinition getProcessDefinition(String processDefinitionId) {
RepositoryService repositoryService = (RepositoryService)SpringContextUtil.getBean(RepositoryService.class);
return repositoryService.createProcessDefinitionQuery()
.processDefinitionId(processDefinitionId)
.singleResult();
}
} }

View File

@ -28,15 +28,15 @@ public class LeaveEndListener extends AbstractExecutionListener<SysOaLeaveForm>
SysOaLeaveForm appForms = getAppForms(); SysOaLeaveForm appForms = getAppForms();
log.info("获取到的表单数据:{}",JSON.toJSONString(appForms)); log.info("获取到的表单数据:{}",JSON.toJSONString(appForms));
Boolean isPass = getVariable(FlowConstants.IS_PASS); Boolean isPass = getVariable(FlowConstants.IS_PASS);
String businessKey= getBusinessKey();
SysOaLeave sysOaLeave=new SysOaLeave();
sysOaLeave.setLeaveNo(businessKey);
if(isPass){ if(isPass){
String businessKey= getBusinessKey();
SysOaLeave sysOaLeave=new SysOaLeave();
sysOaLeave.setProcessStatus(ProcessStatus.PASS.ordinal()); sysOaLeave.setProcessStatus(ProcessStatus.PASS.ordinal());
sysOaLeave.setLeaveNo(businessKey);
sysOaLeaveService.updateSysOaLeaveByLeaveNo(sysOaLeave);
}else { }else {
log.info("上个节点的审批结果:{}",isPass); log.info("说明是重新申请取消的节点,上个节点的审批结果:{}",isPass);
sysOaLeave.setProcessStatus(ProcessStatus.CANCEL.ordinal());
} }
sysOaLeaveService.updateSysOaLeaveByLeaveNo(sysOaLeave);
} }
} }

View File

@ -10,6 +10,7 @@ import com.snow.system.domain.SysOaLeave;
import com.snow.system.service.impl.SysOaLeaveServiceImpl; import com.snow.system.service.impl.SysOaLeaveServiceImpl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType; import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity; import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
@ -43,11 +44,12 @@ public class ManagerTaskEventListener extends AbstractEventListener {
} }
@Override @Override
protected void process() { protected void process(FlowableEngineEvent flowableEngineEvent) {
} }
@Override @Override
protected void taskAssigned(FlowableEngineEntityEvent event) { protected void taskAssigned(FlowableEngineEntityEvent event) {

View File

@ -284,8 +284,9 @@ public class FlowableServiceImpl implements FlowableService {
String startUserId=appForm.getStartUserId(); String startUserId=appForm.getStartUserId();
//业务参数转成map //业务参数转成map
Map<String, Object> paramMap = BeanUtil.beanToMap(appForm); Map<String, Object> paramMap = BeanUtil.beanToMap(appForm);
paramMap.remove("busVarJson");
identityService.setAuthenticatedUserId(startUserId); identityService.setAuthenticatedUserId(startUserId);
paramMap.put(FlowConstants.BUS_VAR,appForm.getClassInfoJson()); paramMap.put(FlowConstants.BUS_VAR,appForm.getBusVarJson());
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(appForm.getFlowDef().getCode(),appForm.getBusinessKey(),paramMap); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(appForm.getFlowDef().getCode(),appForm.getBusinessKey(),paramMap);
//这个方法最终使用一个ThreadLocal类型的变量进行存储也就是与当前的线程绑定所以流程实例启动完毕之后需要设置为null防止多线程的时候出问题 //这个方法最终使用一个ThreadLocal类型的变量进行存储也就是与当前的线程绑定所以流程实例启动完毕之后需要设置为null防止多线程的时候出问题