Przeglądaj źródła

异步发送工作通知优化

tags/24080901
CMM 1 rok temu
rodzic
commit
e2940b4c74
4 zmienionych plików z 103 dodań i 37 usunięć
  1. +57
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/common/util/SendWorkNoticeUtil.java
  2. +17
    -6
      pmapi/src/main/java/com/ningdatech/pmapi/todocenter/manage/TodoCenterManage.java
  3. +3
    -3
      pmapi/src/test/java/com/ningdatech/pmapi/beanconfig/BeanConfig.java
  4. +26
    -28
      pmapi/src/test/java/com/ningdatech/pmapi/todocenter/TodoCenterTest.java

+ 57
- 0
pmapi/src/main/java/com/ningdatech/pmapi/common/util/SendWorkNoticeUtil.java Wyświetl plik

@@ -0,0 +1,57 @@
package com.ningdatech.pmapi.common.util;

import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.pmapi.todocenter.bean.entity.WorkNoticeInfo;
import com.ningdatech.zwdd.client.ZwddClient;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
* 异步发送工作通知工具类
*
* @author CMM
* @since 2023/02/23 13:50
*/
@Slf4j
public class SendWorkNoticeUtil {

private SendWorkNoticeUtil(){

}

public static void sendWorkNotice(ZwddClient zwddClient, WorkNoticeInfo workNoticeInfo, String msg){
// 初始化线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
60, TimeUnit.SECONDS, new ArrayBlockingQueue(20), new ThreadPoolExecutor.AbortPolicy());

// 将发送工作通知交给异步任务Future
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// 调用浙政钉的接口发送工作通知
long startTime = System.currentTimeMillis();
GenericResult<Void> result =
zwddClient.sendWorkNotice(workNoticeInfo.getReceiverUserId(), workNoticeInfo.getBizMsgId(), msg);
String resultMsg = result.getMsg();
if (resultMsg.equals("success")) {
log.info("异步任务执行完成, " + workNoticeInfo.getBizMsgId() + " 当前线程:" + Thread.currentThread().getName());
long endTime = System.currentTimeMillis();
log.info("方法执行完成返回,耗时:" + (endTime - startTime));
}else {
return "发送工作通知失败!";
}
return "发送工作通知成功!";
}, threadPool);
String s;
try {
s = future.get();
} catch (Exception e) {
throw new BizException("获取异步线程处理结果失败!");
}
threadPool.shutdown();
while (threadPool.isTerminated()) {
log.info(s);
break;
}
}
}

+ 17
- 6
pmapi/src/main/java/com/ningdatech/pmapi/todocenter/manage/TodoCenterManage.java Wyświetl plik

@@ -4,12 +4,14 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.pmapi.common.constant.CommonConstant;
import com.ningdatech.pmapi.common.model.entity.ExcelExportWriter;
import com.ningdatech.pmapi.common.statemachine.event.ProjectStatusChangeEvent;
import com.ningdatech.pmapi.common.statemachine.util.StateMachineUtils;
import com.ningdatech.pmapi.common.util.ExcelDownUtil;
import com.ningdatech.pmapi.common.util.SendWorkNoticeUtil;
import com.ningdatech.pmapi.organization.model.entity.DingEmployeeInfo;
import com.ningdatech.pmapi.organization.model.entity.DingOrganization;
import com.ningdatech.pmapi.organization.service.IDingEmployeeInfoService;
@@ -52,6 +54,10 @@ import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import static cn.hutool.core.collection.CollUtil.isEmpty;
@@ -232,8 +238,9 @@ public class TodoCenterManage {
projectService.updateById(declaredProject);
// 获取发送浙政钉工作通知必要信息
WorkNoticeInfo passWorkNoticeInfo = getSendWorkNoticeInfo(auditUserInfo);
String passMsg = String.format(PASS_MSG_TEMPLATE, null, projectName);
// zwddClient.sendWorkNotice(passWorkNoticeInfo.getReceiverUserId(),passWorkNoticeInfo.getBizMsgId(),passMsg);
String passMsg = String.format(PASS_MSG_TEMPLATE, passWorkNoticeInfo.getOrganizationName(), projectName);
// 异步发送工作通知
SendWorkNoticeUtil.sendWorkNotice(zwddClient,passWorkNoticeInfo,passMsg);
return;
}
// 若不是被退回状态,流程通过后,判断当前登录用户是不是最后一个审核人
@@ -262,14 +269,16 @@ public class TodoCenterManage {
}
WorkNoticeInfo passWorkNoticeInfo2 = getSendWorkNoticeInfo(startUserInfo);
String passMsg2 = String.format(PASS_MSG_TEMPLATE2, projectName, processDefinitionName);
// zwddClient.sendWorkNotice(passWorkNoticeInfo2.getReceiverUserId(),passWorkNoticeInfo2.getBizMsgId(),passMsg2);
// 异步发送工作通知
SendWorkNoticeUtil.sendWorkNotice(zwddClient,passWorkNoticeInfo2,passMsg2);
}else {
// 若有下一个审核人(当前节点的用户),
// 向其发送浙政钉工作通知:标题:审核任务 内容:【单位名称】的【项目名称】需要您审核。
// 获取发送浙政钉工作通知必要信息
WorkNoticeInfo sendWorkNoticeInfo = getSendWorkNoticeInfo(auditUserInfo);
String msg = String.format(PASS_MSG_TEMPLATE, null, projectName);
// zwddClient.sendWorkNotice(sendWorkNoticeInfo.getReceiverUserId(),sendWorkNoticeInfo.getBizMsgId(),msg);
// 异步发送工作通知
SendWorkNoticeUtil.sendWorkNotice(zwddClient,sendWorkNoticeInfo,msg);
}
break;
// 盖章并通过
@@ -284,7 +293,8 @@ public class TodoCenterManage {
processTaskService.handleTask(param, userId);
WorkNoticeInfo rejectWorkNoticeInfo = getSendWorkNoticeInfo(startUserInfo);
String rejectMsg = String.format(REJECT_MSG_TEMPLATE, projectName, processDefinitionName);
// zwddClient.sendWorkNotice(rejectWorkNoticeInfo.getReceiverUserId(),rejectWorkNoticeInfo.getBizMsgId(),rejectMsg);
// 异步发送工作通知
SendWorkNoticeUtil.sendWorkNotice(zwddClient,rejectWorkNoticeInfo,rejectMsg);
// 更新项目状态和流程状态
updateRejectProjectStatus(userId, declaredProject);
break;
@@ -300,7 +310,8 @@ public class TodoCenterManage {
// 给项目创建人、流程发起人发送浙政钉工作通知:【项目名称】的【流程名称】被退回,请及时处理。
WorkNoticeInfo backWorkNoticeInfo = getSendWorkNoticeInfo(startUserInfo);
String backMsg = String.format(BACK_MSG_TEMPLATE, projectName, processDefinitionName);
// zwddClient.sendWorkNotice(backWorkNoticeInfo.getReceiverUserId(),backWorkNoticeInfo.getBizMsgId(),backMsg);
// 异步发送工作通知
SendWorkNoticeUtil.sendWorkNotice(zwddClient,backWorkNoticeInfo,backMsg);
break;
// 撤回(流程发起人和当前流程审核人的前一个审核人操作)
case WITHDRAW:


+ 3
- 3
pmapi/src/test/java/com/ningdatech/pmapi/beanconfig/BeanConfig.java Wyświetl plik

@@ -23,11 +23,11 @@ public class BeanConfig {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

// 设置核心线程数
executor.setCorePoolSize(50);
executor.setCorePoolSize(2);
// 设置最大线程数
executor.setMaxPoolSize(200);
executor.setMaxPoolSize(10);
// 设置队列容量
executor.setQueueCapacity(200);
executor.setQueueCapacity(10);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(800);
// 设置默认线程名称


+ 26
- 28
pmapi/src/test/java/com/ningdatech/pmapi/todocenter/TodoCenterTest.java Wyświetl plik

@@ -1,6 +1,7 @@
package com.ningdatech.pmapi.todocenter;

import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.beanconfig.BeanConfig;
import com.ningdatech.pmapi.todocenter.bean.entity.WorkNoticeInfo;
@@ -17,10 +18,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.core.task.TaskExecutor;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.*;
import java.util.function.Supplier;

import static com.ningdatech.pmapi.todocenter.constant.WorkNotice.PASS_MSG_TEMPLATE;
@@ -34,8 +32,8 @@ import static com.ningdatech.pmapi.todocenter.constant.WorkNotice.PASS_MSG_TEMPL
@Slf4j
public class TodoCenterTest extends AppTests {

@Autowired
private TaskExecutor taskExecutor;
//@Autowired
//private TaskExecutor executor;

@Autowired
private TodoCenterManage todoCenterManage;
@@ -46,7 +44,7 @@ public class TodoCenterTest extends AppTests {
@Autowired
private ZwddClient zwddClient;
@Test
public void sendWorkNoticeTest() {
public void sendWorkNoticeTest() throws ExecutionException, InterruptedException {
//String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0223-00-测试项目");
//log.info("开始发送工作通知");
//zwddClient.sendWorkNotice("846085", "0223-00", msg);
@@ -62,37 +60,37 @@ public class TodoCenterTest extends AppTests {
UserInfo auditUserInfo = userInfoService.getById(userId);
// 获取发送浙政钉工作通知必要信息
WorkNoticeInfo workNoticeInfo = todoCenterManage.getSendWorkNoticeInfo(auditUserInfo);
String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0223-01-测试项目");
// workNoticeInfo.setBizMsgId("1");
String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0223-02-测试项目");

//// 先创建1个活动线程的线程池
//ExecutorService executor = Executors.newFixedThreadPool(1);

// 先创建1个活动线程的线程池
ExecutorService executor = Executors.newFixedThreadPool(1);
// 初始化线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20,
60, TimeUnit.SECONDS, new ArrayBlockingQueue(20), new ThreadPoolExecutor.AbortPolicy());

// 将发送工作通知交给异步任务Future
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// 调用浙政钉的接口发送工作通知
try {
long startTime = System.currentTimeMillis();
long startTime = System.currentTimeMillis();
GenericResult<Void> result =
zwddClient.sendWorkNotice(workNoticeInfo.getReceiverUserId(), workNoticeInfo.getBizMsgId(), msg);
String resultMsg = result.getMsg();
if (resultMsg.equals("success")) {
log.info("异步任务执行完成, " + workNoticeInfo.getBizMsgId() + " 当前线程:" + Thread.currentThread().getName());
long endTime = System.currentTimeMillis();
log.info("方法执行完成返回,耗时:" + (endTime - startTime));
} catch (Exception e) {
throw new BizException("发送工作通知失败!");
}
return "task finished!";
}, executor);

executor.shutdown();

while (executor.isTerminated()) {
String result = null;
try {
result = future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}else {
return "发送工作通知失败!";
}
log.info(result);
log.info("发送工作通知成功!");
return "发送工作通知成功!";
}, threadPool);
String s = future.get();
threadPool.shutdown();
while (threadPool.isTerminated()) {
log.info(s);
break;
}
}
}

Ładowanie…
Anuluj
Zapisz