Skip to content

Commit 63c0eb1

Browse files
nadirvishunbinarywang
authored andcommitted
🎨 #2583 【企业微信】增加路由线程池关闭的方法,当通过http优雅关闭时需要调用,否则java进程不会结束
1 parent b00e938 commit 63c0eb1

File tree

3 files changed

+110
-25
lines changed

3 files changed

+110
-25
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouter.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ public WxCpMessageRouter(WxCpService wxCpService) {
7676
this.exceptionHandler = new LogExceptionHandler();
7777
}
7878

79+
/**
80+
* 使用自定义的 {@link ExecutorService}.
81+
*/
82+
public WxCpMessageRouter(WxCpService wxMpService, ExecutorService executorService) {
83+
this.wxCpService = wxMpService;
84+
this.executorService = executorService;
85+
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
86+
this.sessionManager = wxCpService.getSessionManager();
87+
this.exceptionHandler = new LogExceptionHandler();
88+
}
89+
90+
/**
91+
* 系统退出前,应该调用该方法
92+
*/
93+
public void shutDownExecutorService() {
94+
this.executorService.shutdown();
95+
}
96+
97+
/**
98+
* 系统退出前,应该调用该方法,增加了超时时间检测
99+
*/
100+
public void shutDownExecutorService(Integer second) {
101+
this.executorService.shutdown();
102+
try {
103+
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS)) {
104+
this.executorService.shutdownNow();
105+
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS))
106+
log.error("线程池未关闭!");
107+
}
108+
} catch (InterruptedException ie) {
109+
this.executorService.shutdownNow();
110+
Thread.currentThread().interrupt();
111+
}
112+
}
113+
79114
/**
80115
* <pre>
81116
* 设置自定义的 {@link ExecutorService}
@@ -219,8 +254,8 @@ private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
219254
return this.messageDuplicateChecker.isDuplicate(messageId.toString());
220255
}
221256

222-
private void append(StringBuilder sb, String value){
223-
if(StringUtils.isNotEmpty(value)){
257+
private void append(StringBuilder sb, String value) {
258+
if (StringUtils.isNotEmpty(value)) {
224259
sb.append("-").append(value);
225260
}
226261
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouter.java

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,41 @@ public WxCpTpMessageRouter(WxCpTpService wxCpTpService) {
7878
this.exceptionHandler = new LogExceptionHandler();
7979
}
8080

81+
/**
82+
* 使用自定义的 {@link ExecutorService}.
83+
*/
84+
public WxCpTpMessageRouter(WxCpTpService wxCpTpService, ExecutorService executorService) {
85+
this.wxCpTpService = wxCpTpService;
86+
this.executorService = executorService;
87+
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
88+
this.sessionManager = wxCpTpService.getSessionManager();
89+
this.exceptionHandler = new LogExceptionHandler();
90+
}
91+
92+
/**
93+
* 系统退出前,应该调用该方法
94+
*/
95+
public void shutDownExecutorService() {
96+
this.executorService.shutdown();
97+
}
98+
99+
/**
100+
* 系统退出前,应该调用该方法,增加了超时时间检测
101+
*/
102+
public void shutDownExecutorService(Integer second) {
103+
this.executorService.shutdown();
104+
try {
105+
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS)) {
106+
this.executorService.shutdownNow();
107+
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS))
108+
log.error("线程池未关闭!");
109+
}
110+
} catch (InterruptedException ie) {
111+
this.executorService.shutdownNow();
112+
Thread.currentThread().interrupt();
113+
}
114+
}
115+
81116
/**
82117
* <pre>
83118
* 设置自定义的 {@link ExecutorService}
@@ -200,30 +235,29 @@ public WxCpXmlOutMessage route(final WxCpTpXmlMessage wxMessage) {
200235

201236
private boolean isMsgDuplicated(WxCpTpXmlMessage wxMessage) {
202237
StringBuilder messageId = new StringBuilder();
203-
if (wxMessage.getInfoType() != null) {
204-
messageId.append(wxMessage.getInfoType())
205-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getSuiteId()))
206-
.append("-").append(wxMessage.getTimeStamp())
207-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getAuthCorpId()))
208-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getUserID()))
209-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getChangeType()))
210-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getServiceCorpId()));
211-
}
238+
if (wxMessage.getInfoType() != null) {
239+
messageId.append(wxMessage.getInfoType())
240+
.append("-").append(StringUtils.trimToEmpty(wxMessage.getSuiteId()))
241+
.append("-").append(wxMessage.getTimeStamp())
242+
.append("-").append(StringUtils.trimToEmpty(wxMessage.getAuthCorpId()))
243+
.append("-").append(StringUtils.trimToEmpty(wxMessage.getUserID()))
244+
.append("-").append(StringUtils.trimToEmpty(wxMessage.getChangeType()))
245+
.append("-").append(StringUtils.trimToEmpty(wxMessage.getServiceCorpId()));
246+
}
212247

213-
if (wxMessage.getMsgType() != null) {
214-
if (wxMessage.getMsgId() != null) {
215-
messageId.append(wxMessage.getMsgId())
216-
.append("-").append(wxMessage.getCreateTime())
217-
.append("-").append(wxMessage.getFromUserName());
218-
}
219-
else {
220-
messageId.append(wxMessage.getMsgType())
221-
.append("-").append(wxMessage.getCreateTime())
222-
.append("-").append(wxMessage.getFromUserName())
223-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()))
224-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()));
225-
}
248+
if (wxMessage.getMsgType() != null) {
249+
if (wxMessage.getMsgId() != null) {
250+
messageId.append(wxMessage.getMsgId())
251+
.append("-").append(wxMessage.getCreateTime())
252+
.append("-").append(wxMessage.getFromUserName());
253+
} else {
254+
messageId.append(wxMessage.getMsgType())
255+
.append("-").append(wxMessage.getCreateTime())
256+
.append("-").append(wxMessage.getFromUserName())
257+
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()))
258+
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()));
226259
}
260+
}
227261

228262
return this.messageDuplicateChecker.isDuplicate(messageId.toString());
229263
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,28 @@ public WxMpMessageRouter(WxMpService wxMpService, ExecutorService executorServic
8989
}
9090

9191
/**
92-
* 如果使用默认的 {@link ExecutorService},则系统退出前,应该调用该方法.
92+
* 系统退出前,应该调用该方法
9393
*/
9494
public void shutDownExecutorService() {
9595
this.executorService.shutdown();
9696
}
9797

98+
/**
99+
* 系统退出前,应该调用该方法,增加了超时时间检测
100+
*/
101+
public void shutDownExecutorService(Integer second) {
102+
this.executorService.shutdown();
103+
try {
104+
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS)) {
105+
this.executorService.shutdownNow();
106+
if (!this.executorService.awaitTermination(second, TimeUnit.SECONDS))
107+
log.error("线程池未关闭!");
108+
}
109+
} catch (InterruptedException ie) {
110+
this.executorService.shutdownNow();
111+
Thread.currentThread().interrupt();
112+
}
113+
}
98114

99115
/**
100116
* <pre>

0 commit comments

Comments
 (0)