Browse Source

异步发送工作通知测试

tags/24080901
CMM 1 year ago
parent
commit
458026a4af
3 changed files with 111 additions and 7 deletions
  1. +2
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/todocenter/manage/TodoCenterManage.java
  2. +41
    -0
      pmapi/src/test/java/com/ningdatech/pmapi/beanconfig/BeanConfig.java
  3. +68
    -6
      pmapi/src/test/java/com/ningdatech/pmapi/todocenter/TodoCenterTest.java

+ 2
- 1
pmapi/src/main/java/com/ningdatech/pmapi/todocenter/manage/TodoCenterManage.java View File

@@ -402,7 +402,8 @@ public class TodoCenterManage {
String organizationName = dingOrganization.getOrganizationName();
workNoticeInfo.setOrganizationName(organizationName);
// 构建唯一的消息ID
String bizMsgId = "ZD_WORK_NOTICE_" + StrUtil.UNDERLINE + organizationCode + StrUtil.UNDERLINE + organizationName + accountId;
String bizMsgId = "ZD_WORK_NOTICE_" + StrUtil.UNDERLINE + organizationCode + StrUtil.UNDERLINE
+ organizationName + accountId + StrUtil.UNDERLINE + System.currentTimeMillis();
workNoticeInfo.setBizMsgId(bizMsgId);
String receiverUserId = String.valueOf(accountId);
workNoticeInfo.setReceiverUserId(receiverUserId);


+ 41
- 0
pmapi/src/test/java/com/ningdatech/pmapi/beanconfig/BeanConfig.java View File

@@ -0,0 +1,41 @@
package com.ningdatech.pmapi.beanconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
* 发送工作通知线程池配置
*
* @author CMM
* @since 2023/02/23 09:53
*/
@EnableAsync
@Configuration
public class BeanConfig {
@Bean
public TaskExecutor executor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

// 设置核心线程数
executor.setCorePoolSize(50);
// 设置最大线程数
executor.setMaxPoolSize(200);
// 设置队列容量
executor.setQueueCapacity(200);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(800);
// 设置默认线程名称
executor.setThreadNamePrefix("task-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}

+ 68
- 6
pmapi/src/test/java/com/ningdatech/pmapi/todocenter/TodoCenterTest.java View File

@@ -1,13 +1,27 @@
package com.ningdatech.pmapi.todocenter;

import com.ningdatech.basic.exception.BizException;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.beanconfig.BeanConfig;
import com.ningdatech.pmapi.todocenter.bean.entity.WorkNoticeInfo;
import com.ningdatech.pmapi.todocenter.manage.TodoCenterManage;
import com.ningdatech.pmapi.user.entity.UserInfo;
import com.ningdatech.pmapi.user.service.IUserInfoService;
import com.ningdatech.zwdd.client.ZwddClient;
import liquibase.pro.packaged.U;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
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.function.Supplier;

import static com.ningdatech.pmapi.todocenter.constant.WorkNotice.PASS_MSG_TEMPLATE;

@@ -21,16 +35,64 @@ import static com.ningdatech.pmapi.todocenter.constant.WorkNotice.PASS_MSG_TEMPL
public class TodoCenterTest extends AppTests {

@Autowired
private TaskExecutor taskExecutor;

@Autowired
private TodoCenterManage todoCenterManage;

@Autowired
private IUserInfoService userInfoService;

@Autowired
private ZwddClient zwddClient;
@Test
public void sendWorkNoticeTest(){
String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0216-7-测试项目");
log.info("开始发送工作通知");
zwddClient.sendWorkNotice("846085","0216-8",msg);
// zwddClient.sendWorkNotice("829728","0216-5",msg);
log.info("发送工作通知结束");
public void sendWorkNoticeTest() {
//String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0223-00-测试项目");
//log.info("开始发送工作通知");
//zwddClient.sendWorkNotice("846085", "0223-00", msg);
//// zwddClient.sendWorkNotice("829728","0216-5",msg);
//log.info("发送工作通知结束");

//ApplicationContext ac = new AnnotationConfigApplicationContext(BeanConfig.class);
//
////若没有指定属性名,则默认为方法名
//TaskExecutor taskExecutor = (TaskExecutor) ac.getBean("executor");

Long userId = 4L;
UserInfo auditUserInfo = userInfoService.getById(userId);
// 获取发送浙政钉工作通知必要信息
WorkNoticeInfo workNoticeInfo = todoCenterManage.getSendWorkNoticeInfo(auditUserInfo);
String msg = String.format(PASS_MSG_TEMPLATE, "发改委", "0223-01-测试项目");

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

// 将发送工作通知交给异步任务Future
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// 调用浙政钉的接口发送工作通知
try {
long startTime = System.currentTimeMillis();
zwddClient.sendWorkNotice(workNoticeInfo.getReceiverUserId(), workNoticeInfo.getBizMsgId(), msg);
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);
}
log.info(result);
log.info("发送工作通知成功!");
}
}
}

Loading…
Cancel
Save