GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 【小程序/公众号】异步消息路由 by biubiubiu3971 · Pull Request #2999 · binarywang/WxJava

Conversation

@biubiubiu3971

@biubiubiu3971 biubiubiu3971 commented Apr 29, 2023

Copy link
Copy Markdown

使用异步消息时,向子线程传递父线程的appId的时候。兼容只有一个appId的时候,未显式设置appId的情况

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
biubiubiu3971 and others added 4 commits March 23, 2023 18:21
@biubiubiu3971
【小程序】路由规则使用异步时,当前的appId没有传入到子线程,导致wxMaService在子线程中无法取到父线程的appId。
a075c61
@biubiubiu3971
Merge branch 'Wechat-Group:develop' into develop
b204ed1
@biubiubiu3971
Merge branch 'Wechat-Group:develop' into develop
07b0f16
@biubiubiu3971
【小程序/公众号】消息路由,使用异步消息时,向子线程传递父线程的appId的时候,兼容只有一个appId的时候,未显式设置appId的情况
8dd138f
@nadirvishun

nadirvishun commented Apr 30, 2023

Copy link
Copy Markdown

只有一个appId,不也要走 WxMaConfigHolder.set方法吗?不过最后调用WxMaConfigHolder.remove();是应该的。

https://github.com/Wechat-Group/WxJava/blob/6e93062104376870a8c2ebe48f943e98ff1c346a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java#L341-L357

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

这个只设置了当前线程,未设置的线程的依然是default。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

只有在初始化填充config的时候,填充的线程才有设置,其他的线程没有设置。所以其他的线程初始状态依然是“default”。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

获取的时候,如果是只有一个appId,那么就只会取那个,这个不会使用到“default”这个appId。
实际业务如果是多个appId的时候,肯定需要在线程变量中设置appId了,否则业务肯定跑不下去了,所以也不会取到“default”。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nadirvishun

nadirvishun commented Apr 30, 2023

Copy link
Copy Markdown

不是很明白,其它线程是什么意思?在路由分发的时候就只有当前的线程啊,此时的appId已经确定了,直接从WxMaConfigHolder.get()取不就能取出来吗?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nadirvishun

nadirvishun commented Apr 30, 2023

Copy link
Copy Markdown

我是对这个改动有些疑问,为什么要放到for循环中,而不是一开始这种:
image

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

不是很明白,其它线程是什么意思?在路由分发的时候就只有当前的线程啊,此时的appId已经确定了,直接从WxMaConfigHolder.get()取不就能取出来吗?

使用线程池的情况,非初始化线程取出来的就是“default”,初始化的时候,只设置了初始化线程的appId。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

我是对这个改动有些疑问,为什么要放到for循环中,而不是一开始这种: image

放在这里的原因是同步操作不需要获取父线程的appId,只有异步操作需要获取。本着不做非必要操作的原则。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nadirvishun

nadirvishun commented Apr 30, 2023

Copy link
Copy Markdown

个人感觉是没必要,最初的那个就挺好,放在循环中反而浪费。而且也没必要wxMaService.getWxMaConfig().getAppId(),直接WxMaConfigHolder.get()就可以。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

这个是为了hold住在父线程中未设置appId的情况。WxMaConfigHolder.get() 会取出“default”,设置进去的时候会因为没有“default”这个appId导致报错。“default” 这个appId本身是不存在的,在子线程中设置这个不合理。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

而且也没必要``

本地去获取也没有太耗时的

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nadirvishun

nadirvishun commented Apr 30, 2023

Copy link
Copy Markdown

这个是为了hold住在父线程中未设置appId的情况。WxMaConfigHolder.get() 会取出“default”,设置进去的时候会因为没有“default”这个appId导致报错。“default” 这个appId本身是不存在的,在子线程中设置这个不合理。

消息过来时,都会判定appId吧?但也说不准有人特立独行不这样判定,那确实会有这个问题,但是如果他不这么判定,那么在后续的同步路由中,同样也无法从ThreadLocal获取到这个appId。感觉没必要考虑这么多。

if (!wxMaService.switchover(appid)) {
  throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

后面的是直接获取config。在一个appId的情况,不会使用本地线程变量。设计上确实是没有说必须在本地线程设置appId(在路由前并没有设置失败就报错的逻辑),所以需要跟随原始的设计想法。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

在mp里面有多个路由方法,有不传appId和穿appId两种,传了appId没找到报错。但是ma里面是只有一个路由方法,没有传appId,所以之前是允许在线程变量不设置appId的。不能说因为这个原因打破这个特性,必须要在线程变量中设置appId

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

这个是为了hold住在父线程中未设置appId的情况。WxMaConfigHolder.get() 会取出“default”,设置进去的时候会因为没有“default”这个appId导致报错。“default” 这个appId本身是不存在的,在子线程中设置这个不合理。

消息过来时,都会判定appId吧?但也说不准有人特立独行不这样判定,那确实会有这个问题,但是如果他不这么判定,那么在后续的同步路由中,同样也无法从ThreadLocal获取到这个appId。感觉没必要考虑这么多。

if (!wxMaService.switchover(appid)) {
  throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
}

这个是为了hold住只有一个appId的情况,只有一个appId为了方便,自然可以不设置,设计逻辑也是这样。
这个设计的具体代码在 BaseWxMaServiceImpl.getWxMaConfig中有体现。mp和ma都有这个体现

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nadirvishun

nadirvishun commented Apr 30, 2023

Copy link
Copy Markdown

哦,倒是没有考虑兼容旧设计的问题。那也可以这样:

String miniAppId = WxMaConfigHolder.get();
for(循环){
    if(StringUtils.isNotBlank(miniAppId)){
        this.wxMaService.switchoverTo(miniAppId);
    }
    if(StringUtils.isNotBlank(miniAppId)){
        WxMaConfigHolder.remove();
    }
}

都可以,目前没什么疑问了

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@biubiubiu3971

biubiubiu3971 commented Apr 30, 2023

Copy link
Copy Markdown
Author

哦,倒是没有考虑兼容旧设计的问题。那也可以这样:

String miniAppId = WxMaConfigHolder.get();
for(循环){
    if(StringUtils.isNotBlank(miniAppId)){
        this.wxMaService.switchoverTo(miniAppId);
    }
    if(StringUtils.isNotBlank(miniAppId)){
        WxMaConfigHolder.remove();
    }
}

都可以,目前没什么疑问了

本地线程可能会取出来“default”的字符,所以又会出现目前的问题。当然也可以判断是否“default”,但是这样就有硬编码了。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nadirvishun

nadirvishun commented Apr 30, 2023

Copy link
Copy Markdown

哦,默认值不是空。那硬编码确实不好。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was linked to issues May 4, 2023
4.5.0版本公众号消息异常 #3003
Closed
weixin-java-mp v4.5.0升级回归测试报错 #2995
Closed
@binarywang
binarywang merged commit de0266b into binarywang:develop May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4.5.0版本公众号消息异常 weixin-java-mp v4.5.0升级回归测试报错

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@biubiubiu3971 @nadirvishun @binarywang
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.