1
0
mirror of synced 2025-12-17 21:18:07 +08:00

🎨 #2583 【企业微信】增加路由线程池关闭的方法,当通过http优雅关闭时需要调用,否则java进程不会结束

This commit is contained in:
nadirvishun
2022-04-11 15:21:47 +08:00
committed by Binary Wang
parent b00e938ef7
commit 63c0eb107e
3 changed files with 110 additions and 25 deletions

View File

@@ -76,6 +76,41 @@ public class WxCpMessageRouter {
this.exceptionHandler = new LogExceptionHandler(); this.exceptionHandler = new LogExceptionHandler();
} }
/**
* 使用自定义的 {@link ExecutorService}.
*/
public WxCpMessageRouter(WxCpService wxMpService, ExecutorService executorService) {
this.wxCpService = wxMpService;
this.executorService = executorService;
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
this.sessionManager = wxCpService.getSessionManager();
this.exceptionHandler = new LogExceptionHandler();
}
/**
* 系统退出前,应该调用该方法
*/
public void shutDownExecutorService() {
this.executorService.shutdown();
}
/**
* 系统退出前,应该调用该方法,增加了超时时间检测
*/
public void shutDownExecutorService(Integer second) {
this.executorService.shutdown();
try {
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS)) {
this.executorService.shutdownNow();
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS))
log.error("线程池未关闭!");
}
} catch (InterruptedException ie) {
this.executorService.shutdownNow();
Thread.currentThread().interrupt();
}
}
/** /**
* <pre> * <pre>
* 设置自定义的 {@link ExecutorService} * 设置自定义的 {@link ExecutorService}
@@ -219,8 +254,8 @@ public class WxCpMessageRouter {
return this.messageDuplicateChecker.isDuplicate(messageId.toString()); return this.messageDuplicateChecker.isDuplicate(messageId.toString());
} }
private void append(StringBuilder sb, String value){ private void append(StringBuilder sb, String value) {
if(StringUtils.isNotEmpty(value)){ if (StringUtils.isNotEmpty(value)) {
sb.append("-").append(value); sb.append("-").append(value);
} }
} }

View File

@@ -78,6 +78,41 @@ public class WxCpTpMessageRouter {
this.exceptionHandler = new LogExceptionHandler(); this.exceptionHandler = new LogExceptionHandler();
} }
/**
* 使用自定义的 {@link ExecutorService}.
*/
public WxCpTpMessageRouter(WxCpTpService wxCpTpService, ExecutorService executorService) {
this.wxCpTpService = wxCpTpService;
this.executorService = executorService;
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
this.sessionManager = wxCpTpService.getSessionManager();
this.exceptionHandler = new LogExceptionHandler();
}
/**
* 系统退出前,应该调用该方法
*/
public void shutDownExecutorService() {
this.executorService.shutdown();
}
/**
* 系统退出前,应该调用该方法,增加了超时时间检测
*/
public void shutDownExecutorService(Integer second) {
this.executorService.shutdown();
try {
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS)) {
this.executorService.shutdownNow();
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS))
log.error("线程池未关闭!");
}
} catch (InterruptedException ie) {
this.executorService.shutdownNow();
Thread.currentThread().interrupt();
}
}
/** /**
* <pre> * <pre>
* 设置自定义的 {@link ExecutorService} * 设置自定义的 {@link ExecutorService}
@@ -200,30 +235,29 @@ public class WxCpTpMessageRouter {
private boolean isMsgDuplicated(WxCpTpXmlMessage wxMessage) { private boolean isMsgDuplicated(WxCpTpXmlMessage wxMessage) {
StringBuilder messageId = new StringBuilder(); StringBuilder messageId = new StringBuilder();
if (wxMessage.getInfoType() != null) { if (wxMessage.getInfoType() != null) {
messageId.append(wxMessage.getInfoType()) messageId.append(wxMessage.getInfoType())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getSuiteId())) .append("-").append(StringUtils.trimToEmpty(wxMessage.getSuiteId()))
.append("-").append(wxMessage.getTimeStamp()) .append("-").append(wxMessage.getTimeStamp())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getAuthCorpId())) .append("-").append(StringUtils.trimToEmpty(wxMessage.getAuthCorpId()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getUserID())) .append("-").append(StringUtils.trimToEmpty(wxMessage.getUserID()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getChangeType())) .append("-").append(StringUtils.trimToEmpty(wxMessage.getChangeType()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getServiceCorpId())); .append("-").append(StringUtils.trimToEmpty(wxMessage.getServiceCorpId()));
} }
if (wxMessage.getMsgType() != null) { if (wxMessage.getMsgType() != null) {
if (wxMessage.getMsgId() != null) { if (wxMessage.getMsgId() != null) {
messageId.append(wxMessage.getMsgId()) messageId.append(wxMessage.getMsgId())
.append("-").append(wxMessage.getCreateTime()) .append("-").append(wxMessage.getCreateTime())
.append("-").append(wxMessage.getFromUserName()); .append("-").append(wxMessage.getFromUserName());
} } else {
else { messageId.append(wxMessage.getMsgType())
messageId.append(wxMessage.getMsgType()) .append("-").append(wxMessage.getCreateTime())
.append("-").append(wxMessage.getCreateTime()) .append("-").append(wxMessage.getFromUserName())
.append("-").append(wxMessage.getFromUserName()) .append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent())) .append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()));
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()));
}
} }
}
return this.messageDuplicateChecker.isDuplicate(messageId.toString()); return this.messageDuplicateChecker.isDuplicate(messageId.toString());
} }

View File

@@ -89,12 +89,28 @@ public class WxMpMessageRouter {
} }
/** /**
* 如果使用默认的 {@link ExecutorService},则系统退出前,应该调用该方法. * 系统退出前,应该调用该方法
*/ */
public void shutDownExecutorService() { public void shutDownExecutorService() {
this.executorService.shutdown(); this.executorService.shutdown();
} }
/**
* 系统退出前,应该调用该方法,增加了超时时间检测
*/
public void shutDownExecutorService(Integer second) {
this.executorService.shutdown();
try {
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS)) {
this.executorService.shutdownNow();
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS))
log.error("线程池未关闭!");
}
} catch (InterruptedException ie) {
this.executorService.shutdownNow();
Thread.currentThread().interrupt();
}
}
/** /**
* <pre> * <pre>