com.riversoft.weixin.mp.MpWxClientFactory Maven / Gradle / Ivy
package com.riversoft.weixin.mp;
import com.riversoft.weixin.common.WxClient;
import com.riversoft.weixin.mp.base.AppSetting;
import com.riversoft.weixin.mp.base.WxEndpoint;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by exizhai on 11/12/2015.
*/
public class MpWxClientFactory {
private static MpWxClientFactory instance = null;
private static ConcurrentHashMap wxClients = new ConcurrentHashMap<>();
private MpWxClientFactory() {
}
public synchronized static MpWxClientFactory getInstance() {
if (instance == null) {
instance = new MpWxClientFactory();
}
return instance;
}
public WxClient defaultWxClient() {
return with(AppSetting.defaultSettings());
}
public WxClient with(AppSetting appSetting) {
if (!wxClients.containsKey(key(appSetting))) {
String url = WxEndpoint.get("url.token.get");
WxClient wxClient = new WxClient(url, appSetting.getAppId(), appSetting.getSecret());
wxClients.putIfAbsent(key(appSetting), wxClient);
}
return wxClients.get(key(appSetting));
}
private String key(AppSetting appSetting) {
return appSetting.getAppId() + ":" + appSetting.getSecret();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy