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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

howareyouo
Copy link

@howareyouo howareyouo commented Aug 19, 2019

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

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

@borball
Copy link
Owner

borball commented Aug 19, 2019

@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.

@howareyouo
Copy link
Author

howareyouo commented Sep 9, 2019

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

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.

2 participants