◐ Shell
clean mode source ↗

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

@FreeOfYou

我用反射改成了使用阿里的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

原先没有这个功能的时候是从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

原先没有这个功能的时候是从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里面如果有对微信请求的话,里面还需要设置一遍,比较麻烦。