weixin-sdk icon indicating copy to clipboard operation
weixin-sdk copied to clipboard

refactor: HttpClient 线程安全, 可以全局复用, 也可配置自定义的 httpClient 到 WxClient.

Open howareyouo opened this issue 5 years ago • 2 comments

refactor: HttpClient 线程安全, 可以全局复用. 见文档: https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e213

也可以在初始化 WxClient 前通过 setDefaultHttpClient 指定自定义的 HttpClient. 例如集成hoverfly-java进行单元测试时, 通过HttpClients.createSystem()创建遵循系统代理设置的 httpclient 对微信接口返回数据进行自动 mock.

howareyouo avatar Aug 19 '19 04:08 howareyouo

@howareyouo Thanks for the PR.

WxClient might have multiple instances in the system especially for a SaaS provider, i.g. they have multiple clientIds.

I understand your main purpose for this PR is to provide the API users a possibility to customize their own HTTP client implementation, in this way they can set proxy or something was not considered in the default one.

One solution to achieve this, not sure if you are able to implement.

  1. Introduce a customized annotation like 'HttpClientProvider'
  2. Introduce one new interface: HttpClientsHolder
public interface HttpClientsHolder {

    /**
     * Get a map of ClientId:HttpClient mapping
     * key shall be WX clientId, value shall be the HttpClient this clientId suppose to use
     * 
     * @return
     */
    Map<String, HttpClient> getHttpClients();

}
  1. API users can implement interface HttpClientsHolder inside which the HttpClient can be customized, and add annotation 'HttpClientProvider'.

  2. In SDK side, during system startup, it shall scan the classpath to fetch if any sub-class of interface 'HttpClientsHolder' has that annotation and pick up one if it is existing.

  3. In WxClient, it shall use the customized HttpClient fetched from the map above. A default one shall be used if no annotation is found in the classpath or no value found in the map.

  4. If possible AccessTokenHolder can be refactored as well to use the annotation mechanism.

borball avatar Aug 19 '19 17:08 borball

Your solution sounds ok to me, I shall do this by you way.

howareyouo avatar Sep 09 '19 08:09 howareyouo