消息通知注解加入执行监听器支持

This commit is contained in:
wangliwen 2022-06-01 11:38:47 +08:00
parent 71bfdf5faa
commit 22391d1fc6
6 changed files with 120 additions and 15 deletions

View File

@ -25,4 +25,9 @@ public @interface ActivitiNoticeOperation {
* @return * @return
*/ */
String process() default ""; String process() default "";
/**
* @return activiti 监听器类型 1:TaskListener 任务监听器; 2:ExecutionListener 执行监听器;
*/
int type() default 1;
} }

View File

@ -7,7 +7,9 @@ import io.renren.modules.notice.enums.NoticeStatusEnum;
import io.renren.modules.notice.service.SysNoticeService; import io.renren.modules.notice.service.SysNoticeService;
import io.renren.modules.sys.dto.SysUserDTO; import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService; import io.renren.modules.sys.service.SysUserService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask; import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener; import org.activiti.engine.delegate.TaskListener;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
@ -57,25 +59,119 @@ public class ActivitiNoticeAspect {
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes()); Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
final ActivitiNoticeOperation activitiNoticeOperation = method.getAnnotation(ActivitiNoticeOperation.class); final ActivitiNoticeOperation activitiNoticeOperation = method.getAnnotation(ActivitiNoticeOperation.class);
switch (activitiNoticeOperation.type()) {
case 1: {
Arrays.asList(joinPoint.getArgs()).stream().findFirst().ifPresent(arg -> { Arrays.asList(joinPoint.getArgs()).stream().findFirst().ifPresent(arg -> {
final DelegateTask delegateTask = (DelegateTask) arg; final DelegateTask delegateTask = (DelegateTask) arg;
final String eventName = delegateTask.getEventName(); final String eventName = delegateTask.getEventName();
switch (eventName) { switch (eventName) {
case TaskListener.EVENTNAME_ASSIGNMENT: // 节点被委派给某人 case TaskListener.EVENTNAME_ASSIGNMENT: // 节点被委派给某人
logger.error("事件:" + eventName); logger.error("任务监听器事件:" + eventName);
notice(delegateTask, activitiNoticeOperation); assignment_notice(delegateTask, activitiNoticeOperation);
break; break;
default: default:
} }
}); });
} // 任务监听器类型
break;
case 2: {
Arrays.asList(joinPoint.getArgs()).stream().findFirst().ifPresent(arg -> {
final DelegateExecution execution = (DelegateExecution) arg;
final String eventName = execution.getEventName();
switch (eventName) {
case ExecutionListener.EVENTNAME_END: // 节点被委派给某人
logger.error("执行监听器事件:" + eventName);
end_notice(execution, activitiNoticeOperation);
break;
default:
}
});
} // 执行监听器
break;
}
//执行时长(毫秒) //执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime; long time = System.currentTimeMillis() - beginTime;
logger.error("执行时长{} ms", time); logger.error("执行时长{} ms", time);
} }
private void notice(DelegateTask delegateTask, final ActivitiNoticeOperation activitiNoticeOperation) { /**
* 流程结束时通知
*
* @param execution
* @param activitiNoticeOperation
*/
private void end_notice(final DelegateExecution execution, final ActivitiNoticeOperation activitiNoticeOperation) {
Map<String, Object> kv = execution.getVariables();
logger.error("表单:" + kv.toString());
if (work_.contains(kv.get("id").toString())) {
logger.error("------------出现重放------------");
return;
}
work_.add(kv.get("id").toString());
String creator = null;
if (kv.containsKey("creator")) { // 表单存在创建者
creator = kv.get("creator").toString();
} else if (kv.containsKey("userId")) {
creator = kv.get("userId").toString();
}
if (StringUtils.isEmpty(creator)) {
return;
}
try {
String result = "";
Boolean termination =
Boolean.valueOf(execution.getVariables().get("termination").toString()); // 直接终结
Boolean reject =
Boolean.valueOf(execution.getVariables().get("reject").toString()); // 被拒绝
if (termination) {
result = "终止被拒";
} else if (reject) {
result = "被拒";
} else {
result = "通过";
}
String finalCreator = creator;
String finalResult = result;
CompletableFuture.runAsync(() -> { // 发起人
String content = "您发起的流程<" + activitiNoticeOperation.process() + "> 已结束。审核结果为:" + finalResult;
SysNoticeDTO dto = new SysNoticeDTO();
dto.setType(2);
dto.setTitle("流程结束系统通知");
dto.setContent(content); // 通知内容
dto.setReceiverType(1);
dto.setReceiverTypeIds(finalCreator);
dto.setStatus(NoticeStatusEnum.SEND.value());
dto.setSenderName("流程系统");
dto.setSenderDate(new Date());
dto.setCreator(sysUserService.getByUsername("admin").getId());
dto.setCreateDate(new Date());
sysNoticeService.save(dto);
}).thenRunAsync(() -> {
// 防止重放
new Thread(() -> {
try {
Thread.sleep(200);
work_.remove(kv.get("id").toString());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}).start();
});
} catch (Exception exception) {
logger.error("发送通知消息异常", exception);
}
}
/**
* 分配审批人时
*
* @param delegateTask
* @param activitiNoticeOperation
*/
private void assignment_notice(final DelegateTask delegateTask, final ActivitiNoticeOperation activitiNoticeOperation) {
Map<String, Object> kv = delegateTask.getVariables(); Map<String, Object> kv = delegateTask.getVariables();
logger.error("录入表单:" + kv.toString()); logger.error("表单:" + kv.toString());
if (work_.contains(kv.get("id").toString())) { if (work_.contains(kv.get("id").toString())) {
logger.error("------------出现重放------------"); logger.error("------------出现重放------------");
return; return;
@ -96,7 +192,7 @@ public class ActivitiNoticeAspect {
SysUserDTO assignee = sysUserService.get(Long.valueOf(delegateTask.getAssignee())); SysUserDTO assignee = sysUserService.get(Long.valueOf(delegateTask.getAssignee()));
String content = "您发起的流程<" + activitiNoticeOperation.process() + ">当前审核节点为:" + String content = "您发起的流程<" + activitiNoticeOperation.process() + ">当前审核节点为:" +
activitiNoticeOperation.value() + activitiNoticeOperation.value() +
"<br>当前审核人为\"" + ";当前审核人为:\"" +
assignee.getDeptName() + "\"审核负责人\"" + assignee.getRealName(); assignee.getDeptName() + "\"审核负责人\"" + assignee.getRealName();
SysNoticeDTO dto = new SysNoticeDTO(); SysNoticeDTO dto = new SysNoticeDTO();
dto.setType(2); dto.setType(2);
@ -112,7 +208,7 @@ public class ActivitiNoticeAspect {
sysNoticeService.save(dto); sysNoticeService.save(dto);
}).thenRunAsync(() -> { // 审批者 }).thenRunAsync(() -> { // 审批者
SysUserDTO owner = sysUserService.get(Long.valueOf(finalCreator)); SysUserDTO owner = sysUserService.get(Long.valueOf(finalCreator));
String content = owner.getRealName() + "发起的流程<" + activitiNoticeOperation.process() + ">当前审核节点为:" + activitiNoticeOperation.value() + "<br>当前审核人指派为您"; String content = owner.getRealName() + "发起的流程<" + activitiNoticeOperation.process() + ">当前审核节点为:" + activitiNoticeOperation.value() + "当前审核人指派为您";
SysNoticeDTO dto = new SysNoticeDTO(); SysNoticeDTO dto = new SysNoticeDTO();
dto.setType(2); dto.setType(2);
dto.setTitle("流程流转系统通知"); dto.setTitle("流程流转系统通知");

View File

@ -60,6 +60,7 @@ public class DemandDataListener implements TaskListener, ExecutionListener, Acti
private TDemandDataService tDemandDataService; private TDemandDataService tDemandDataService;
@Override @Override
@ActivitiNoticeOperation(value = "流程结束", process = "能力需求申请", type = 2)
public void notify(DelegateExecution execution) throws Exception { public void notify(DelegateExecution execution) throws Exception {
logger.error("----------------------进入能力需求受理者结束节点---------------------------"); logger.error("----------------------进入能力需求受理者结束节点---------------------------");
execution.getProcessBusinessKey(); execution.getProcessBusinessKey();

View File

@ -84,6 +84,7 @@ public class CorrectionListener implements TaskListener, ExecutionListener, Acti
} }
@Override @Override
@ActivitiNoticeOperation(value = "流程结束", process = "能力申请流程", type = 2)
public void notify(DelegateExecution delegateExecution) throws Exception { public void notify(DelegateExecution delegateExecution) throws Exception {
logger.error("----------------------进入部门审批结束节点---------------------------"); logger.error("----------------------进入部门审批结束节点---------------------------");
delegateExecution.getProcessBusinessKey(); delegateExecution.getProcessBusinessKey();

View File

@ -74,6 +74,7 @@ public class ResourceUndercarriageListener implements TaskListener, ExecutionLis
} }
@Override @Override
@ActivitiNoticeOperation(value = "流程结束", process = "能力资源下架", type = 2)
public void notify(DelegateExecution execution) throws Exception { public void notify(DelegateExecution execution) throws Exception {
logger.error("----------------------进入审批通过节点---------------------------"); logger.error("----------------------进入审批通过节点---------------------------");
logger.error("事件类型:" + execution.getEventName()); logger.error("事件类型:" + execution.getEventName());

View File

@ -56,6 +56,7 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A
private ResourceService resourceService; private ResourceService resourceService;
@Override @Override
@ActivitiNoticeOperation(value = "流程结束", process = "能力资源上架", type = 2)
public void notify(DelegateExecution execution) throws Exception { public void notify(DelegateExecution execution) throws Exception {
logger.error("----------------------进入审批通过节点---------------------------"); logger.error("----------------------进入审批通过节点---------------------------");
logger.error("事件类型:" + execution.getEventName()); logger.error("事件类型:" + execution.getEventName());