feat:并发编程优化
This commit is contained in:
parent
74938b3d44
commit
40b793811b
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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<SysEmail> list = ListUtil.toList(email1, email2);
|
||||
|
||||
List<CompletableFuture<Void>> 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();
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ public class EmailStatusManager {
|
|||
}
|
||||
|
||||
private void init(){
|
||||
sleepTime = 5 * 1000; // 睡眠时间5s
|
||||
sleepTime = 5 * 60 * 1000; // 睡眠时间5min
|
||||
emailService = SpringContextUtils.getBean(ISysEmailService.class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<AppLoginResp> 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){
|
||||
|
|
|
@ -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<SysEmail> {
|
|||
|
||||
void status2Redis();
|
||||
|
||||
void status2Redis(SysEmail email);
|
||||
|
||||
String getNameById(String id);
|
||||
}
|
||||
|
|
|
@ -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,15 +221,14 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
|
|||
public void status2Redis() {
|
||||
// 获取所有配置的邮箱服务器
|
||||
List<SysEmail> emails = list();
|
||||
// 查询是否有企业邮箱
|
||||
List<Integer> 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<String, Object> statusMap = new HashMap<>();
|
||||
// 使用并发 更新邮箱状态及用量
|
||||
for (SysEmail email : emails) {
|
||||
CompletableFuture.runAsync(() -> status2Redis(email));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void status2Redis(SysEmail email) {
|
||||
String id = email.getId();
|
||||
String name = email.getName();
|
||||
Integer isQiye = email.getIsQiye();
|
||||
|
@ -237,6 +237,7 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
|
|||
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];
|
||||
|
@ -246,11 +247,8 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
|
|||
nameValue.setUsage(usedQuota);
|
||||
}
|
||||
}
|
||||
statusMap.put(id, nameValue);
|
||||
}
|
||||
// 将邮箱服务器连接状态更新到reids
|
||||
String statusKey = RedisConstant.EMAIL_STATUS;
|
||||
redisUtil.hmset(statusKey, statusMap);
|
||||
redisUtil.hset(statusKey, id, nameValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue
Block a user