Skip to content

Commit 4d07fbc

Browse files
committed
fix: 明确小店下载与路由背压行为
1 parent 49adff1 commit 4d07fbc

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

weixin-java-store/src/main/java/com/binarywang/wxjava/store/api/WxStoreBasicService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public interface WxStoreBasicService {
8888
* 根据media_id获取图片
8989
*
9090
* @param mediaId media_id
91+
* @return 图片下载结果;调用方使用完 {@link StoreImageResponse#getFile()} 后必须删除该临时文件
9192
*/
9293
StoreImageResponse getImg(String mediaId) throws WxErrorException;
9394

weixin-java-store/src/main/java/com/binarywang/wxjava/store/executor/StoreMediaDownloadRequestExecutor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public static RequestExecutor<StoreImageResponse, String> create(RequestHttp<?,
6060
public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile)
6161
throws IOException {
6262
File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile);
63-
resultFile.deleteOnExit();
6463
try (InputStream in = inputStream; OutputStream out = openOutputStream(resultFile)) {
6564
IOUtils.copy(in, out);
6665
}

weixin-java-store/src/main/java/com/binarywang/wxjava/store/message/WxStoreMessageRouter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
public class WxStoreMessageRouter {
3636
/** 规则列表 */
3737
private final List<WxStoreMessageRouterRule<? extends WxStoreMessage>> rules = new ArrayList<>();
38-
/** 线程池 */
38+
/**
39+
* 线程池。默认使用容量为 1000 的有界队列;队列与最大线程数均耗尽时,
40+
* 由提交消息的调用线程执行任务,以施加背压并避免丢弃回调消息。
41+
*/
3942
private ExecutorService executorService;
4043
/** 异常处理器 */
4144
private WxErrorExceptionHandler exceptionHandler;
@@ -47,7 +50,8 @@ public class WxStoreMessageRouter {
4750
public WxStoreMessageRouter() {
4851
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("WxChMsgRouter-pool-%d").build();
4952
this.executorService = new ThreadPoolExecutor(2, 100,
50-
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
53+
60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1000), namedThreadFactory,
54+
new ThreadPoolExecutor.CallerRunsPolicy());
5155
this.sessionManager = new StandardSessionManager();
5256
this.exceptionHandler = new LogExceptionHandler();
5357
this.messageDuplicateChecker = WxMessageInMemoryDuplicateCheckerSingleton.getInstance();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.binarywang.wxjava.store.message;
2+
3+
import java.util.concurrent.ThreadPoolExecutor;
4+
import org.testng.Assert;
5+
import org.testng.annotations.Test;
6+
7+
/** Tests the default router backpressure policy. */
8+
public class WxStoreMessageRouterTest {
9+
10+
@Test
11+
public void shouldUseBoundedQueueAndCallerRunsPolicy() {
12+
WxStoreMessageRouter router = new WxStoreMessageRouter();
13+
ThreadPoolExecutor executor = (ThreadPoolExecutor) router.getExecutorService();
14+
15+
Assert.assertTrue(executor.getQueue().remainingCapacity() > 0);
16+
Assert.assertTrue(executor.getRejectedExecutionHandler() instanceof ThreadPoolExecutor.CallerRunsPolicy);
17+
router.shutDownExecutorService();
18+
}
19+
}

0 commit comments

Comments
 (0)