Skip to content

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