use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by alipay.
the class RpcBindingConverter method convertServiceAnnotation.
/**
* convert props to RpcBindingParam
*
* @param bindingParam
* @param sofaServiceAnnotation
* @param sofaServiceBindingAnnotation
* @param bindingConverterContext
*/
protected void convertServiceAnnotation(RpcBindingParam bindingParam, SofaService sofaServiceAnnotation, SofaServiceBinding sofaServiceBindingAnnotation, BindingConverterContext bindingConverterContext) {
if (sofaServiceBindingAnnotation.timeout() != 0) {
bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout());
}
// TODO need a magic number
if (sofaServiceBindingAnnotation.weight() != 0) {
bindingParam.setWeight(sofaServiceBindingAnnotation.weight());
}
if (sofaServiceBindingAnnotation.warmUpTime() != 0) {
bindingParam.setWarmUpTime(sofaServiceBindingAnnotation.warmUpTime());
}
if (sofaServiceBindingAnnotation.warmUpWeight() != 0) {
bindingParam.setWarmUpWeight(sofaServiceBindingAnnotation.warmUpWeight());
}
if (StringUtils.hasText(sofaServiceBindingAnnotation.serializeType())) {
bindingParam.setSerialization(sofaServiceBindingAnnotation.serializeType());
}
ApplicationContext applicationContext = bindingConverterContext.getApplicationContext();
List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(applicationContext));
String[] filterNames = sofaServiceBindingAnnotation.filters();
if (filterNames.length > 0) {
for (String filterName : filterNames) {
Object filter = applicationContext.getBean(filterName);
if (filter instanceof Filter) {
filters.add((Filter) filter);
} else {
throw new SofaBootRpcRuntimeException("filter name[" + filterName + "] is not ref a Filter.");
}
}
}
if (!CollectionUtils.isEmpty(filters)) {
bindingParam.setFilters(filters);
}
String threadPool = sofaServiceBindingAnnotation.userThreadPool();
if (StringUtils.hasText(threadPool)) {
UserThreadPool threadPoolObj = (UserThreadPool) applicationContext.getBean(threadPool);
String interfaceName = sofaServiceAnnotation.interfaceType().getCanonicalName();
String uniqId = sofaServiceAnnotation.uniqueId();
String uniqueName = interfaceName + ":1.0" + (StringUtils.isEmpty(uniqId) ? "" : ":" + uniqId);
UserThreadPoolManager.registerUserThread(uniqueName, threadPoolObj);
}
String registryAlias = sofaServiceBindingAnnotation.registry();
if (StringUtils.hasText(registryAlias)) {
String[] registrys = registryAlias.split(",");
bindingParam.setRegistrys(Arrays.asList(registrys));
}
SofaParameter[] parameters = sofaServiceBindingAnnotation.parameters();
if (parameters.length > 0) {
bindingParam.setParameters(parseSofaParameters(parameters));
}
SofaMethod[] sofaMethods = sofaServiceBindingAnnotation.methodInfos();
if (sofaMethods.length > 0) {
bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
}
}
use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by alipay.
the class ServerConfigContainerTest method testStartCustomThreadPoolMonitor.
@Test
public void testStartCustomThreadPoolMonitor() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
UserThreadPoolManager.registerUserThread("service1", new UserThreadPool());
UserThreadPoolManager.registerUserThread("service2", new UserThreadPool());
UserThreadPoolManager.registerUserThread("service3", new UserThreadPool("same-name"));
UserThreadPoolManager.registerUserThread("service4", new UserThreadPool("same-name"));
Method privateStartMethod = serverConfigContainer.getClass().getDeclaredMethod("startCustomThreadPoolMonitor");
privateStartMethod.setAccessible(true);
privateStartMethod.invoke(serverConfigContainer);
Field privateField = serverConfigContainer.getClass().getDeclaredField("customThreadPoolMonitorList");
privateField.setAccessible(true);
Object value = privateField.get(serverConfigContainer);
List<RpcThreadPoolMonitor> customThreadPoolMonitorList = (List<RpcThreadPoolMonitor>) value;
Assert.assertEquals(customThreadPoolMonitorList.size(), 4);
boolean hasHashCode = false;
for (RpcThreadPoolMonitor monitor : customThreadPoolMonitorList) {
if (monitor.getPoolName().contains("same-name-")) {
hasHashCode = true;
}
}
Assert.assertTrue(hasHashCode);
Method privateStopMethod = serverConfigContainer.getClass().getDeclaredMethod("stopCustomThreadPoolMonitor");
privateStopMethod.setAccessible(true);
privateStopMethod.invoke(serverConfigContainer);
Assert.assertEquals(customThreadPoolMonitorList.size(), 0);
}
use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by sofastack.
the class ProviderConfigHelper method getProviderConfig.
/**
* 获取 ProviderConfig
*
* @param contract the Contract
* @param binding the RpcBinding
* @param target 服务实例
* @return the ProviderConfig
* @throws SofaBootRpcRuntimeException
*/
public ProviderConfig getProviderConfig(Contract contract, RpcBinding binding, Object target) throws SofaBootRpcRuntimeException {
RpcBindingParam param = binding.getRpcBindingParam();
String id = binding.getBeanId();
String interfaceId = contract.getInterfaceType().getName();
Object ref = target;
String uniqueId = contract.getUniqueId();
Integer timeout = param.getTimeout();
Integer weight = param.getWeight();
Integer warmupTime = param.getWarmUpTime();
Integer warmupWeight = param.getWarmUpWeight();
UserThreadPool threadPool = param.getUserThreadPool();
String serialization = param.getSerialization();
List<Filter> filters = param.getFilters();
List<MethodConfig> methodConfigs = convertToMethodConfig(param.getMethodInfos());
ServerConfig serverConfig = serverConfigContainer.getServerConfig(binding.getBindingType().getType());
ProviderConfig providerConfig = new ProviderConfig();
if (StringUtils.hasText(appName)) {
providerConfig.setApplication(new ApplicationConfig().setAppName(appName));
}
if (StringUtils.hasText(id)) {
providerConfig.setId(id);
}
if (StringUtils.hasText(interfaceId)) {
providerConfig.setInterfaceId(interfaceId);
}
if (contract.getInterfaceType() != null) {
providerConfig.setProxyClass(contract.getInterfaceType());
}
if (ref != null) {
providerConfig.setRef(ref);
}
if (StringUtils.hasText(uniqueId)) {
providerConfig.setUniqueId(uniqueId);
}
if (timeout != null) {
providerConfig.setTimeout(timeout);
}
if (weight != null) {
providerConfig.setWeight(weight);
}
if (warmupTime != null) {
providerConfig.setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, String.valueOf(warmupTime));
}
if (warmupWeight != null) {
providerConfig.setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, String.valueOf(warmupWeight));
}
if (!CollectionUtils.isEmpty(filters)) {
providerConfig.setFilterRef(filters);
}
if (!CollectionUtils.isEmpty(methodConfigs)) {
providerConfig.setMethods(methodConfigs);
}
if (threadPool != null) {
UserThreadPoolManager.registerUserThread(ConfigUniqueNameGenerator.getUniqueName(providerConfig), threadPool);
}
providerConfig.setServer(serverConfig);
String protocol = binding.getBindingType().getType();
// http protocol use default protocol
if (!SofaBootRpcConfigConstants.RPC_PROTOCOL_HTTP.equals(protocol)) {
providerConfig.setBootstrap(protocol);
}
if (StringUtils.hasText(serialization)) {
providerConfig.setSerialization(serialization);
}
if (param.getParameters() != null) {
providerConfig.setParameters(param.getParameters());
}
if (param.getRegistrys() != null && param.getRegistrys().size() > 0) {
List<String> registrys = param.getRegistrys();
for (String registryAlias : registrys) {
RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(registryAlias);
providerConfig.setRegistry(registryConfig);
}
} else if (registryConfigContainer.isMeshEnabled(protocol)) {
RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
providerConfig.setRegistry(registryConfig);
} else {
RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig();
providerConfig.setRegistry(registryConfig);
}
providerConfig.setRegister(false);
return providerConfig;
}
use of com.alipay.sofa.rpc.server.UserThreadPool in project sofa-boot by sofastack.
the class RpcBindingConverter method convertServiceAnnotation.
/**
* convert props to RpcBindingParam
*
* @param bindingParam
* @param sofaServiceAnnotation
* @param sofaServiceBindingAnnotation
* @param bindingConverterContext
*/
protected void convertServiceAnnotation(RpcBindingParam bindingParam, SofaService sofaServiceAnnotation, SofaServiceBinding sofaServiceBindingAnnotation, BindingConverterContext bindingConverterContext) {
if (sofaServiceBindingAnnotation.timeout() != 0) {
bindingParam.setTimeout(sofaServiceBindingAnnotation.timeout());
}
// TODO need a magic number
if (sofaServiceBindingAnnotation.weight() != 0) {
bindingParam.setWeight(sofaServiceBindingAnnotation.weight());
}
if (sofaServiceBindingAnnotation.warmUpTime() != 0) {
bindingParam.setWarmUpTime(sofaServiceBindingAnnotation.warmUpTime());
}
if (sofaServiceBindingAnnotation.warmUpWeight() != 0) {
bindingParam.setWarmUpWeight(sofaServiceBindingAnnotation.warmUpWeight());
}
if (StringUtils.hasText(sofaServiceBindingAnnotation.serializeType())) {
bindingParam.setSerialization(sofaServiceBindingAnnotation.serializeType());
}
ApplicationContext applicationContext = bindingConverterContext.getApplicationContext();
List<Filter> filters = new ArrayList<Filter>(RpcFilterContainer.getInstance().getFilters(applicationContext));
String[] filterNames = sofaServiceBindingAnnotation.filters();
if (filterNames.length > 0) {
for (String filterName : filterNames) {
Object filter = applicationContext.getBean(filterName);
if (filter instanceof Filter) {
filters.add((Filter) filter);
} else {
throw new SofaBootRpcRuntimeException("filter name[" + filterName + "] is not ref a Filter.");
}
}
}
if (!CollectionUtils.isEmpty(filters)) {
bindingParam.setFilters(filters);
}
String threadPool = sofaServiceBindingAnnotation.userThreadPool();
if (StringUtils.hasText(threadPool)) {
UserThreadPool threadPoolObj = (UserThreadPool) applicationContext.getBean(threadPool);
String interfaceName = sofaServiceAnnotation.interfaceType().getCanonicalName();
String uniqId = sofaServiceAnnotation.uniqueId();
String uniqueName = interfaceName + ":1.0" + (StringUtils.isEmpty(uniqId) ? "" : ":" + uniqId);
UserThreadPoolManager.registerUserThread(uniqueName, threadPoolObj);
}
String registryAlias = sofaServiceBindingAnnotation.registry();
if (StringUtils.hasText(registryAlias)) {
String[] registrys = registryAlias.split(",");
bindingParam.setRegistrys(Arrays.asList(registrys));
}
SofaParameter[] parameters = sofaServiceBindingAnnotation.parameters();
if (parameters.length > 0) {
bindingParam.setParameters(parseSofaParameters(parameters));
}
SofaMethod[] sofaMethods = sofaServiceBindingAnnotation.methodInfos();
if (sofaMethods.length > 0) {
bindingParam.setMethodInfos(parseSofaMethods(sofaMethods));
}
}
Aggregations