From 40b793811b5af6cf3699ea4f8422617529226c4f Mon Sep 17 00:00:00 2001 From: nieziyan Date: Fri, 15 Dec 2023 18:44:27 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=B9=B6=E5=8F=91=E7=BC=96?= =?UTF-8?q?=E7=A8=8B=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jeecg/common/constant/RedisConstant.java | 4 +- .../org/jeecg/modules/base/dto/AppLoginR.java | 19 +++++++ .../src/main/java/org/jeecg/modules/Demo.java | 21 +++++--- .../org/jeecg/modules/EmailStatusManager.java | 2 +- .../jeecg/modules/aspect/StatusAspect.java | 25 ++------- .../modules/qiyeEmail/base/InstanceSDK.java | 23 +++++++- .../modules/service/ISysEmailService.java | 3 ++ .../service/impl/SysEmailServiceImpl.java | 54 +++++++++---------- 8 files changed, 93 insertions(+), 58 deletions(-) create mode 100644 jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/dto/AppLoginR.java diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/RedisConstant.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/RedisConstant.java index 0d7378e1..59f9ba4f 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/RedisConstant.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/RedisConstant.java @@ -35,5 +35,7 @@ public interface RedisConstant { String EMAIL_SENDER = "Email_Sender"; - String MANAGE_TOKEN = "Manage:Token"; + String MANAGE_TOKEN = "Manage:Token"; // 运管系统Token + + String QIYE_EMAIL_TOKEN = "QiyeEmail:Token"; // 网易企业邮箱Token } diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/dto/AppLoginR.java b/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/dto/AppLoginR.java new file mode 100644 index 00000000..773b0a4c --- /dev/null +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/modules/base/dto/AppLoginR.java @@ -0,0 +1,19 @@ +package org.jeecg.modules.base.dto; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import java.io.Serializable; +import java.util.Date; + +@Data +public class AppLoginR implements Serializable { + + private String accessToken; + + private String refreshToken; + + private Date accessTokenExpiredTime; + + private Date refreshTokenExpiredTime; +} diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/Demo.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/Demo.java index c6b9ec9b..de3da4d9 100644 --- a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/Demo.java +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/Demo.java @@ -1,6 +1,8 @@ package org.jeecg.modules; import cn.hutool.core.collection.ListUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.ArrayUtil; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -10,8 +12,7 @@ import org.jeecg.modules.base.entity.postgre.SysEmail; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; +import java.util.concurrent.*; public class Demo { @@ -68,13 +69,21 @@ public class Demo { email2.setPort(143); List list = ListUtil.toList(email1, email2); + + List> futures = new ArrayList<>(); + Executor executor = Executors.newFixedThreadPool(5); EmailServiceManager manager = EmailServiceManager.getInstance(); for (SysEmail email : list) { manager.init(email); - CompletableFuture.runAsync(() -> { - - boolean success = manager.canReceive(); - }); + CompletableFuture.supplyAsync(manager::canReceive, executor) + .thenAccept(status -> FileUtil.writeUtf8Lines(ListUtil.toList(status), "C:\\Users\\a\\Desktop\\"+ email.getUsername() + ".txt")); } + /*CompletableFuture allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); + + try { + allFutures.get(); // 等待所有任务完成 + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + }*/ } } diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/EmailStatusManager.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/EmailStatusManager.java index 66934c38..3bf35a95 100644 --- a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/EmailStatusManager.java +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/EmailStatusManager.java @@ -54,7 +54,7 @@ public class EmailStatusManager { } private void init(){ - sleepTime = 5 * 1000; // 睡眠时间5s + sleepTime = 5 * 60 * 1000; // 睡眠时间5min emailService = SpringContextUtils.getBean(ISysEmailService.class); } } diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/aspect/StatusAspect.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/aspect/StatusAspect.java index 40699437..33cecb60 100644 --- a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/aspect/StatusAspect.java +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/aspect/StatusAspect.java @@ -50,8 +50,10 @@ public class StatusAspect { private MonitorAlarm monitorAlarm; @Autowired - private ISysServerService serverService; + private ISysEmailService emailService; + @Autowired + private ISysServerService serverService; // 新增|修改邮箱服务器信息后 异步更新其状态信息 @Async @@ -61,26 +63,7 @@ public class StatusAspect { Object[] args = point.getArgs(); if (ArrayUtil.length(args) > 0){ SysEmail email = (SysEmail) args[0]; - String id = email.getId(); - String name = email.getName(); - Integer isQiye = email.getIsQiye(); - boolean isConn = EmailUtil.isConnection(email); - NameValue nameValue = new NameValue(name, isConn); - if (ObjectUtil.isNotNull(isQiye) && isQiye == IS.getValue()){ - String username = email.getUsername(); - String[] info = StrUtil.split(username, SymbolConstant.AT); - QiyeOpenPlatSDK platSDK = InstanceSDK.getInstance(); - if (ArrayUtil.length(info) == 2 && ObjectUtil.isNotNull(platSDK)){ - String accountName = info[0]; - String domain = info[1]; - RParam param = new RParam(accountName, domain); - AccountInfo accountInfo = Account.getMailAccountInfo(platSDK, param); - Integer usedQuota = accountInfo.getUsedQuota(); - nameValue.setUsage(usedQuota); - } - } - String statusKey = RedisConstant.EMAIL_STATUS; - redisUtil.hset(statusKey, id, nameValue); + emailService.status2Redis(email); } } diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/qiyeEmail/base/InstanceSDK.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/qiyeEmail/base/InstanceSDK.java index db0de71b..68997a10 100644 --- a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/qiyeEmail/base/InstanceSDK.java +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/qiyeEmail/base/InstanceSDK.java @@ -1,5 +1,6 @@ package org.jeecg.modules.qiyeEmail.base; +import cn.hutool.core.bean.BeanUtil; import com.netease.qiye.qiyeopenplatform.common.constant.ResultEnum; import com.netease.qiye.qiyeopenplatform.common.dto.login.AppLoginResp; import com.netease.qiye.qiyeopenplatform.sdk.QiyeOpenPlatSDK; @@ -7,13 +8,22 @@ import com.netease.qiye.qiyeopenplatform.sdk.QiyeOpenPlatSDKConfig; import com.netease.qiye.qiyeopenplatform.sdk.dto.Q; import com.netease.qiye.qiyeopenplatform.sdk.dto.R; import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.constant.RedisConstant; +import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.SpringContextUtils; +import org.jeecg.modules.base.dto.AppLoginR; import org.springframework.core.env.Environment; @Slf4j public class InstanceSDK { + private static RedisUtil redisUtil; + + static { + redisUtil = SpringContextUtils.getBean(RedisUtil.class); + } + public static QiyeOpenPlatSDK getInstance() { - QiyeOpenPlatSDK qiyeOpenPlatSDK = null; + QiyeOpenPlatSDK qiyeOpenPlatSDK; try { // 获取nacos armd.yaml配置文件中企业邮箱的相关配置信息 Property property = Property.getInstance(); @@ -33,8 +43,19 @@ public class InstanceSDK { // 使用配置类创建SDK实例 qiyeOpenPlatSDK = new QiyeOpenPlatSDK("qiyeOpenPlatSDK", qiyeOpenPlatSDKConfig); // 通过授权码登录 获取Token类并给SDK实例设置Token类 + if (redisUtil.hasKey(RedisConstant.QIYE_EMAIL_TOKEN)){ + AppLoginR appLoginR = (AppLoginR) redisUtil.get(RedisConstant.QIYE_EMAIL_TOKEN); + AppLoginResp appLoginResp = new AppLoginResp(); + BeanUtil.copyProperties(appLoginR, appLoginResp); + qiyeOpenPlatSDK.getQiyeOpenPlatSDKConfig().setupToken(appLoginResp); + return qiyeOpenPlatSDK; + } R appLoginRespR = qiyeOpenPlatSDK.appLogin(authCode); AppLoginResp appLoginResp = appLoginRespR.getDataBean(AppLoginResp.class); + AppLoginR appLoginR = new AppLoginR(); + BeanUtil.copyProperties(appLoginResp, appLoginR); + // 设置有效期 12h + redisUtil.set(RedisConstant.QIYE_EMAIL_TOKEN, appLoginR, 12 * 60 * 60); qiyeOpenPlatSDK.getQiyeOpenPlatSDKConfig().setupToken(appLoginResp); return qiyeOpenPlatSDK; }catch (Exception e){ diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/ISysEmailService.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/ISysEmailService.java index 053b31f8..36c09ba7 100644 --- a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/ISysEmailService.java +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/ISysEmailService.java @@ -1,6 +1,7 @@ package org.jeecg.modules.service; import com.baomidou.mybatisplus.extension.service.IService; +import com.netease.qiye.qiyeopenplatform.sdk.QiyeOpenPlatSDK; import org.jeecg.common.api.QueryRequest; import org.jeecg.common.api.vo.Result; import org.jeecg.modules.base.dto.SourceDto; @@ -29,5 +30,7 @@ public interface ISysEmailService extends IService { void status2Redis(); + void status2Redis(SysEmail email); + String getNameById(String id); } diff --git a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/impl/SysEmailServiceImpl.java b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/impl/SysEmailServiceImpl.java index 26b4aa1e..56fdef2e 100644 --- a/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/impl/SysEmailServiceImpl.java +++ b/jeecg-module-abnormal-alarm/src/main/java/org/jeecg/modules/service/impl/SysEmailServiceImpl.java @@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import static org.jeecg.modules.base.enums.Enabled.ENABLED; @@ -220,37 +221,34 @@ public class SysEmailServiceImpl extends ServiceImpl i public void status2Redis() { // 获取所有配置的邮箱服务器 List emails = list(); - // 查询是否有企业邮箱 - List hasQieye = emails.stream().map(SysEmail::getIsQiye) - .filter(ObjectUtil::isNotNull).filter(isQiye -> Qiye.IS.getValue() == isQiye) - .collect(Collectors.toList()); - QiyeOpenPlatSDK platSDK = null; - // 如果emails中存在企业邮箱 则提前登录为查询邮箱信息做准备 - if (CollUtil.isNotEmpty(hasQieye)) platSDK = InstanceSDK.getInstance(); - Map statusMap = new HashMap<>(); + // 使用并发 更新邮箱状态及用量 for (SysEmail email : emails) { - String id = email.getId(); - String name = email.getName(); - Integer isQiye = email.getIsQiye(); - boolean isConn = EmailUtil.isConnection(email); - NameValue nameValue = new NameValue(name, isConn); - if (ObjectUtil.isNotNull(isQiye) && isQiye == IS.getValue()){ - String username = email.getUsername(); - String[] info = StrUtil.split(username, SymbolConstant.AT); - if (ArrayUtil.length(info) == 2 && ObjectUtil.isNotNull(platSDK)){ - String accountName = info[0]; - String domain = info[1]; - RParam param = new RParam(accountName, domain); - AccountInfo accountInfo = Account.getMailAccountInfo(platSDK, param); - Integer usedQuota = accountInfo.getUsedQuota(); - nameValue.setUsage(usedQuota); - } - } - statusMap.put(id, nameValue); + CompletableFuture.runAsync(() -> status2Redis(email)); + } + } + + @Override + public void status2Redis(SysEmail email) { + String id = email.getId(); + String name = email.getName(); + Integer isQiye = email.getIsQiye(); + boolean isConn = EmailUtil.isConnection(email); + NameValue nameValue = new NameValue(name, isConn); + if (ObjectUtil.isNotNull(isQiye) && isQiye == IS.getValue()){ + String username = email.getUsername(); + String[] info = StrUtil.split(username, SymbolConstant.AT); + QiyeOpenPlatSDK platSDK = InstanceSDK.getInstance(); + if (ArrayUtil.length(info) == 2 && ObjectUtil.isNotNull(platSDK)){ + String accountName = info[0]; + String domain = info[1]; + RParam param = new RParam(accountName, domain); + AccountInfo accountInfo = Account.getMailAccountInfo(platSDK, param); + Integer usedQuota = accountInfo.getUsedQuota(); + nameValue.setUsage(usedQuota); + } } - // 将邮箱服务器连接状态更新到reids String statusKey = RedisConstant.EMAIL_STATUS; - redisUtil.hmset(statusKey, statusMap); + redisUtil.hset(statusKey, id, nameValue); } @Override