Skip to content

Commit 558f4e7

Browse files
gtyangbinarywang
authored andcommitted
#535 修复Tomcat 不能正常关闭的问题,增加线程池shutdown相关的操作
1 parent a0240e7 commit 558f4e7

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ public WxMpMessageRouter(WxMpService wxMpService) {
7575
this.exceptionHandler = new LogExceptionHandler();
7676
}
7777

78+
/**
79+
* <pre>
80+
* 使用自定义的 {@link ExecutorService}
81+
* </pre>
82+
*/
83+
public WxMpMessageRouter(WxMpService wxMpService, ExecutorService executorService) {
84+
this.wxMpService = wxMpService;
85+
this.executorService = executorService;
86+
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
87+
this.sessionManager = new StandardSessionManager();
88+
this.exceptionHandler = new LogExceptionHandler();
89+
}
90+
91+
/**
92+
* <pre>
93+
* 如果使用默认的 {@link ExecutorService},则系统退出前,应该调用该方法。
94+
* </pre>
95+
*/
96+
public void shutDownExecutorService() {
97+
this.executorService.shutdown();
98+
}
99+
100+
78101
/**
79102
* <pre>
80103
* 设置自定义的 {@link ExecutorService}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMessageRouterTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.testng.*;
99
import org.testng.annotations.*;
1010

11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
1113
import java.util.Map;
1214

1315
/**
@@ -67,9 +69,23 @@ public void testAsync(WxMpXmlMessage message, String expected) throws Interrupte
6769
prepare(true, sb, router);
6870
router.route(message);
6971
Thread.sleep(500);
72+
router.shutDownExecutorService();
7073
Assert.assertEquals(sb.toString(), expected);
7174
}
7275

76+
@Test(dataProvider = "messages-1")
77+
public void testExternalExcutorService(WxMpXmlMessage message, String expected) throws InterruptedException {
78+
StringBuffer sb = new StringBuffer();
79+
ExecutorService executorService = Executors.newFixedThreadPool(100);
80+
WxMpMessageRouter router = new WxMpMessageRouter(null, executorService);
81+
prepare(true, sb, router);
82+
router.route(message);
83+
Thread.sleep(500);
84+
executorService.shutdown();
85+
Assert.assertEquals(sb.toString(), expected);
86+
}
87+
88+
7389
public void testConcurrency() throws InterruptedException {
7490
final WxMpMessageRouter router = new WxMpMessageRouter(null);
7591
router.rule().handler(new WxMpMessageHandler() {

0 commit comments

Comments
 (0)