Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【小程序】路由规则使用异步时,当前的appId没有传入到子线程,导致wxMaService在子线程中无法取到父线程的appId。 #2961

Merged
merged 1 commit into from
Mar 24, 2023

Conversation

biubiubiu3971
Copy link

小程序异步执行消息路由时子线程添加appid

@binarywang binarywang merged commit bae84e1 into binarywang:develop Mar 24, 2023
@FreeOfYou
Copy link

我用反射改成了使用阿里的TTL的线程池了

//初始化为微信的公共包数据
    static {
        //改变threadLocal为ttl的
        setTTlThreadPool();
    }

    @SneakyThrows
    @SuppressWarnings({"rawtypes", "unchecked"})
    private static void setTTlThreadPool() {
        TransmittableThreadLocal<String> ttlThreadLocal = new TransmittableThreadLocal() {
            @Override
            protected String initialValue() {
                return "default";
            }
        };
        Field threadLocal = ReflectionUtils.findField(WxMaConfigHolder.class, "THREAD_LOCAL");
        if (threadLocal != null) {
            ReflectionUtils.makeAccessible(threadLocal);
            Field modifiers = ReflectionUtils.findField(threadLocal.getClass(), StringPool.MODIFIERS);
            if (modifiers != null) {
                ReflectionUtils.makeAccessible(modifiers);
                ReflectionUtils.setField(modifiers, threadLocal, threadLocal.getModifiers() ^ Modifier.FINAL);
            }
            ReflectionUtils.setField(threadLocal, WxMaConfigHolder.class, ttlThreadLocal);
        }
    }

@nadirvishun
Copy link

原先没有这个功能的时候是从context上下文中传递过去的,比较麻烦:

    private void route(WxMaMessage message, String appid) {
        //将appid传递到过去,否则如果是异步路由,无法从threadLocal中获取当前appid
        Map<String, Object> context = new HashMap<>();
        context.put(WxampContextConstants.APPID, appid);
        try {
            wxMaMessageRouter.route(message, context);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

@biubiubiu3971
Copy link
Author

原先没有这个功能的时候是从context上下文中传递过去的,比较麻烦:

    private void route(WxMaMessage message, String appid) {
        //将appid传递到过去,否则如果是异步路由,无法从threadLocal中获取当前appid
        Map<String, Object> context = new HashMap<>();
        context.put(WxampContextConstants.APPID, appid);
        try {
            wxMaMessageRouter.route(message, context);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

是的,在实际业务执行的handler里面如果有对微信请求的话,里面还需要设置一遍,比较麻烦。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants