diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java index a4795c9b4d..6cd603929d 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java @@ -128,16 +128,20 @@ public WxMaXmlOutMessage route(final WxMaMessage wxMessage, final Map> futures = new ArrayList<>(); WxMaXmlOutMessage result = null; for (final WxMaMessageRouterRule rule : matchRules) { // 返回最后一个非异步的rule的执行结果 if (rule.isAsync()) { + //获取当前线程使用的实际appId,兼容只有一个appId,且未显式设置当前使用的appId的情况 + String miniAppId = this.wxMaService.getWxMaConfig().getAppid(); futures.add( this.executorService.submit(() -> { + //子线程中设置实际的appId this.wxMaService.switchoverTo(miniAppId); rule.service(wxMessage, context, WxMaMessageRouter.this.wxMaService, WxMaMessageRouter.this.sessionManager, WxMaMessageRouter.this.exceptionHandler); + WxMaConfigHolder.remove(); }) ); } else { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java index 2ae798bb5e..067226cc85 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java @@ -248,16 +248,19 @@ public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage, final Map> futures = new ArrayList<>(); - String appId = WxMpConfigStorageHolder.get(); + for (final WxMpMessageRouterRule rule : matchRules) { // 返回最后一个非异步的rule的执行结果 if (rule.isAsync()) { + //获取当前线程使用的实际appId。兼容只有一个appId,且未显式设置当前使用的appId的情况 + String appId = this.wxMpService.getWxMpConfigStorage().getAppId(); futures.add( this.executorService.submit(() -> { //传入父线程的appId this.wxMpService.switchoverTo(appId); rule.service(wxMessage, context, mpService, WxMpMessageRouter.this.sessionManager, WxMpMessageRouter.this.exceptionHandler); + WxMpConfigStorageHolder.remove(); }) ); } else {