Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加企业微信路由线程池关闭方法 #2583

Merged
merged 3 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@ public WxCpMessageRouter(WxCpService wxCpService) {
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>
* 设置自定义的 {@link ExecutorService}
Expand Down Expand Up @@ -219,8 +254,8 @@ private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
return this.messageDuplicateChecker.isDuplicate(messageId.toString());
}

private void append(StringBuilder sb, String value){
if(StringUtils.isNotEmpty(value)){
private void append(StringBuilder sb, String value) {
if (StringUtils.isNotEmpty(value)) {
sb.append("-").append(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ public WxCpTpMessageRouter(WxCpTpService wxCpTpService) {
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>
* 设置自定义的 {@link ExecutorService}
Expand Down Expand Up @@ -200,30 +235,29 @@ public WxCpXmlOutMessage route(final WxCpTpXmlMessage wxMessage) {

private boolean isMsgDuplicated(WxCpTpXmlMessage wxMessage) {
StringBuilder messageId = new StringBuilder();
if (wxMessage.getInfoType() != null) {
messageId.append(wxMessage.getInfoType())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getSuiteId()))
.append("-").append(wxMessage.getTimeStamp())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getAuthCorpId()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getUserID()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getChangeType()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getServiceCorpId()));
}
if (wxMessage.getInfoType() != null) {
messageId.append(wxMessage.getInfoType())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getSuiteId()))
.append("-").append(wxMessage.getTimeStamp())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getAuthCorpId()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getUserID()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getChangeType()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getServiceCorpId()));
}

if (wxMessage.getMsgType() != null) {
if (wxMessage.getMsgId() != null) {
messageId.append(wxMessage.getMsgId())
.append("-").append(wxMessage.getCreateTime())
.append("-").append(wxMessage.getFromUserName());
}
else {
messageId.append(wxMessage.getMsgType())
.append("-").append(wxMessage.getCreateTime())
.append("-").append(wxMessage.getFromUserName())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()));
}
if (wxMessage.getMsgType() != null) {
if (wxMessage.getMsgId() != null) {
messageId.append(wxMessage.getMsgId())
.append("-").append(wxMessage.getCreateTime())
.append("-").append(wxMessage.getFromUserName());
} else {
messageId.append(wxMessage.getMsgType())
.append("-").append(wxMessage.getCreateTime())
.append("-").append(wxMessage.getFromUserName())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()));
}
}

return this.messageDuplicateChecker.isDuplicate(messageId.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,28 @@ public WxMpMessageRouter(WxMpService wxMpService, ExecutorService executorServic
}

/**
* 如果使用默认的 {@link ExecutorService},则系统退出前,应该调用该方法.
* 系统退出前,应该调用该方法
*/
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>
Expand Down