Search in sources :

Example 6 with UserThreadPool

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));
    }
}
Also used : SofaMethod(com.alipay.sofa.runtime.api.annotation.SofaMethod) ArrayList(java.util.ArrayList) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ApplicationContext(org.springframework.context.ApplicationContext) ExcludeFilter(com.alipay.sofa.rpc.filter.ExcludeFilter) Filter(com.alipay.sofa.rpc.filter.Filter) SofaParameter(com.alipay.sofa.runtime.api.annotation.SofaParameter)

Example 7 with UserThreadPool

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);
}
Also used : RpcThreadPoolMonitor(com.alipay.sofa.rpc.boot.common.RpcThreadPoolMonitor) Field(java.lang.reflect.Field) List(java.util.List) Method(java.lang.reflect.Method) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) ActivelyDestroyTest(com.alipay.sofa.rpc.boot.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 8 with UserThreadPool

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;
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RpcBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig)

Example 9 with UserThreadPool

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));
    }
}
Also used : SofaMethod(com.alipay.sofa.runtime.api.annotation.SofaMethod) ArrayList(java.util.ArrayList) UserThreadPool(com.alipay.sofa.rpc.server.UserThreadPool) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ApplicationContext(org.springframework.context.ApplicationContext) ExcludeFilter(com.alipay.sofa.rpc.filter.ExcludeFilter) Filter(com.alipay.sofa.rpc.filter.Filter) SofaParameter(com.alipay.sofa.runtime.api.annotation.SofaParameter)

Aggregations

UserThreadPool (com.alipay.sofa.rpc.server.UserThreadPool)9 RpcThreadPoolMonitor (com.alipay.sofa.rpc.boot.common.RpcThreadPoolMonitor)4 Filter (com.alipay.sofa.rpc.filter.Filter)4 Test (org.junit.Test)3 SofaBootRpcRuntimeException (com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException)2 RpcBindingParam (com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam)2 ActivelyDestroyTest (com.alipay.sofa.rpc.boot.test.ActivelyDestroyTest)2 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)2 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)2 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)2 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 ExcludeFilter (com.alipay.sofa.rpc.filter.ExcludeFilter)2 SofaMethod (com.alipay.sofa.runtime.api.annotation.SofaMethod)2 SofaParameter (com.alipay.sofa.runtime.api.annotation.SofaParameter)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2