fix:增加对异常的拦截,修改map为ConcurrentHashMap

This commit is contained in:
xiaoguangbin 2024-06-27 09:58:22 +08:00
parent f6b963145f
commit 1423b2f09d

View File

@ -16,6 +16,7 @@ import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
@ -39,12 +40,12 @@ public class AutoProcessManager{
/**
* 以邮件Id为key邮件信息为value
*/
private Map<String,EmailProperties> emailMap = new HashMap<>();
private Map<String,EmailProperties> emailMap = new ConcurrentHashMap<>();
/**
* 以邮件id为key以邮件执行线程为value
*/
private Map<String,EmailParsingActuator> emailExecThreadMap = new HashMap<>();
private Map<String,EmailParsingActuator> emailExecThreadMap = new ConcurrentHashMap<>();
/**
* 启动自动处理
@ -81,11 +82,13 @@ public class AutoProcessManager{
public void run() {
for(;;){
long start = System.currentTimeMillis();
try {
if(!CollectionUtils.isEmpty(emailMap)){
synchronized (lock){
Iterator<EmailProperties> iterator = emailMap.values().iterator();
while(iterator.hasNext()){
EmailProperties next = iterator.next();
// ConcurrentModificationException
if (next.isResetFlag()) {
EmailParsingActuator actuator = emailExecThreadMap.get(next.getId());
actuator.updateEmail(next);
@ -110,6 +113,10 @@ public class AutoProcessManager{
}
}
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
}
long end = System.currentTimeMillis();
long sleepTime = taskProperties.getMonitoringMailDataCycle() - (end-start);
//如果sleepTime > 0 需要睡眠到指定时间否则继续下次获取邮件
@ -177,6 +184,7 @@ public class AutoProcessManager{
if(sleepTime > 0){
try {
TimeUnit.MILLISECONDS.sleep(sleepTime);
// throw new RuntimeException("运行时异常");
} catch (InterruptedException e) {
e.printStackTrace();
log.error(e.getMessage(), e);
@ -281,6 +289,7 @@ public class AutoProcessManager{
public void run() {
for(;;){
long start = System.currentTimeMillis();
try {
if(!CollectionUtils.isEmpty(emailExecThreadMap)){
//遍历邮箱执行线程如果状态为已停止则删除
final Iterator<Map.Entry<String, EmailParsingActuator>> checkStopThreads = emailExecThreadMap.entrySet().iterator();
@ -322,6 +331,10 @@ public class AutoProcessManager{
}
});
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
}
long end = System.currentTimeMillis();
long sleepTime = taskProperties.getDeletedMailThreadExecCycle() - (end-start);
//如果sleepTime > 0 需要睡眠到指定时间否则继续下次监测