weixin-sdk
weixin-sdk copied to clipboard
refactor: HttpClient 线程安全, 可以全局复用, 也可配置自定义的 httpClient 到 WxClient.
refactor: HttpClient 线程安全, 可以全局复用. 见文档: https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e213
也可以在初始化 WxClient
前通过 setDefaultHttpClient
指定自定义的 HttpClient.
例如集成hoverfly-java
进行单元测试时, 通过HttpClients.createSystem()
创建遵循系统代理设置的 httpclient
对微信接口返回数据进行自动 mock.
@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.
- Introduce a customized annotation like 'HttpClientProvider'
- 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();
}
-
API users can implement interface HttpClientsHolder inside which the HttpClient can be customized, and add annotation 'HttpClientProvider'.
-
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.
-
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.
-
If possible AccessTokenHolder can be refactored as well to use the annotation mechanism.
Your solution sounds ok to me, I shall do this by you way.