Search in sources :

Example 16 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-boot by sofastack.

the class ConsumerConfigHelper method convertToMethodConfig.

private List<MethodConfig> convertToMethodConfig(List<RpcBindingMethodInfo> methodInfos) {
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    if (!CollectionUtils.isEmpty(methodInfos)) {
        for (RpcBindingMethodInfo info : methodInfos) {
            String name = info.getName();
            Integer timeout = info.getTimeout();
            Integer retries = info.getRetries();
            String type = info.getType();
            Object callbackHandler = info.getCallbackHandler();
            MethodConfig methodConfig = new MethodConfig();
            methodConfig.setName(name);
            if (timeout != null) {
                methodConfig.setTimeout(timeout);
            }
            if (retries != null) {
                methodConfig.setRetries(retries);
            }
            if (StringUtils.hasText(type)) {
                methodConfig.setInvokeType(type);
            }
            if (callbackHandler != null) {
                if (callbackHandler instanceof SofaResponseCallback) {
                    methodConfig.setOnReturn((SofaResponseCallback) callbackHandler);
                } else {
                    throw new SofaBootRpcRuntimeException("callback handler must implement SofaResponseCallback [" + callbackHandler + "]");
                }
            }
            methodConfigs.add(methodConfig);
        }
    }
    return methodConfigs;
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ArrayList(java.util.ArrayList) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) RpcBindingMethodInfo(com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo)

Example 17 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-boot by alipay.

the class ConsumerConfigHelper method getConsumerConfig.

/**
 * 获取 ConsumerConfig
 *
 * @param contract the Contract
 * @param binding  the RpcBinding
 * @return the ConsumerConfig
 */
public ConsumerConfig getConsumerConfig(Contract contract, RpcBinding binding) {
    RpcBindingParam param = binding.getRpcBindingParam();
    String id = binding.getBeanId();
    String interfaceId = contract.getInterfaceType().getName();
    String uniqueId = contract.getUniqueId();
    Integer timeout = param.getTimeout();
    Integer retries = param.getRetries();
    String type = param.getType();
    Integer addressWaitTime = param.getAddressWaitTime();
    Object callbackHandler = param.getCallbackHandler();
    String genericInterface = param.getGenericInterface();
    String loadBalancer = param.getLoadBalancer();
    Boolean lazy = param.getLazy();
    Boolean check = param.getCheck();
    String mockMode = param.getMockMode();
    String serialization = param.getSerialization();
    List<Filter> filters = param.getFilters();
    List<MethodConfig> methodConfigs = convertToMethodConfig(param.getMethodInfos());
    String targetUrl = param.getTargetUrl();
    String referenceLimit = sofaBootRpcProperties.getConsumerRepeatedReferenceLimit();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    if (StringUtils.hasText(appName)) {
        consumerConfig.setApplication(new ApplicationConfig().setAppName(appName));
    }
    if (StringUtils.hasText(id)) {
        consumerConfig.setId(id);
    }
    if (StringUtils.hasText(genericInterface)) {
        consumerConfig.setGeneric(true);
        consumerConfig.setInterfaceId(genericInterface);
    } else if (StringUtils.hasText(interfaceId)) {
        consumerConfig.setInterfaceId(interfaceId);
    }
    if (StringUtils.hasText(uniqueId)) {
        consumerConfig.setUniqueId(uniqueId);
    }
    if (timeout != null) {
        consumerConfig.setTimeout(timeout);
    }
    if (retries != null) {
        consumerConfig.setRetries(retries);
    }
    if (StringUtils.hasText(type)) {
        consumerConfig.setInvokeType(type);
    }
    if (addressWaitTime != null) {
        consumerConfig.setAddressWait(addressWaitTime);
    }
    if (StringUtils.hasText(loadBalancer)) {
        consumerConfig.setLoadBalancer(loadBalancer);
    }
    if (lazy != null) {
        consumerConfig.setLazy(lazy);
    }
    if (check != null) {
        consumerConfig.setCheck(check);
    }
    if (mockMode != null) {
        consumerConfig.setMockMode(mockMode);
    }
    if (callbackHandler != null) {
        if (callbackHandler instanceof SofaResponseCallback) {
            consumerConfig.setOnReturn((SofaResponseCallback) callbackHandler);
        } else {
            throw new SofaBootRpcRuntimeException("callback handler must implement SofaResponseCallback [" + callbackHandler + "]");
        }
    }
    if (!CollectionUtils.isEmpty(filters)) {
        consumerConfig.setFilterRef(filters);
    }
    if (!CollectionUtils.isEmpty(methodConfigs)) {
        consumerConfig.setMethods(methodConfigs);
    }
    if (StringUtils.hasText(targetUrl)) {
        consumerConfig.setDirectUrl(targetUrl);
        consumerConfig.setLazy(true);
        consumerConfig.setSubscribe(false);
        consumerConfig.setRegister(false);
    }
    if (StringUtils.hasText(referenceLimit)) {
        consumerConfig.setRepeatedReferLimit(Integer.valueOf(referenceLimit));
    }
    String protocol = binding.getBindingType().getType();
    consumerConfig.setBootstrap(protocol);
    if (protocol.equals(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
        consumerConfig.setInJVM(false);
    }
    if (param.getRegistrys() != null && param.getRegistrys().size() > 0) {
        List<String> registrys = param.getRegistrys();
        for (String registryAlias : registrys) {
            RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(registryAlias);
            consumerConfig.setRegistry(registryConfig);
        }
    } else if (registryConfigContainer.isMeshEnabled(protocol)) {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
        consumerConfig.setRegistry(registryConfig);
    } else {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig();
        consumerConfig.setRegistry(registryConfig);
    }
    if (StringUtils.hasText(serialization)) {
        consumerConfig.setSerialization(serialization);
    }
    if (Boolean.TRUE.toString().equals(sofaBootRpcProperties.getHystrixEnable())) {
        consumerConfig.setParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED, Boolean.TRUE.toString());
    }
    // after sofaBootRpcProperties#getHystrixEnable for override global config
    if (param.getParameters() != null) {
        consumerConfig.setParameters(param.getParameters());
    }
    return consumerConfig.setProtocol(protocol);
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) RpcBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) Filter(com.alipay.sofa.rpc.filter.Filter) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 18 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-boot by alipay.

the class ConsumerConfigHelper method convertToMethodConfig.

private List<MethodConfig> convertToMethodConfig(List<RpcBindingMethodInfo> methodInfos) {
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    if (!CollectionUtils.isEmpty(methodInfos)) {
        for (RpcBindingMethodInfo info : methodInfos) {
            String name = info.getName();
            Integer timeout = info.getTimeout();
            Integer retries = info.getRetries();
            String type = info.getType();
            Object callbackHandler = info.getCallbackHandler();
            MethodConfig methodConfig = new MethodConfig();
            methodConfig.setName(name);
            if (timeout != null) {
                methodConfig.setTimeout(timeout);
            }
            if (retries != null) {
                methodConfig.setRetries(retries);
            }
            if (StringUtils.hasText(type)) {
                methodConfig.setInvokeType(type);
            }
            if (callbackHandler != null) {
                if (callbackHandler instanceof SofaResponseCallback) {
                    methodConfig.setOnReturn((SofaResponseCallback) callbackHandler);
                } else {
                    throw new SofaBootRpcRuntimeException("callback handler must implement SofaResponseCallback [" + callbackHandler + "]");
                }
            }
            methodConfigs.add(methodConfig);
        }
    }
    return methodConfigs;
}
Also used : MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) ArrayList(java.util.ArrayList) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) RpcBindingMethodInfo(com.alipay.sofa.rpc.boot.runtime.binding.RpcBindingMethodInfo)

Example 19 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.

the class DefaultClientProxyInvoker method decorateRequest.

@Override
protected void decorateRequest(SofaRequest request) {
    // 公共的设置
    super.decorateRequest(request);
    // 缓存是为了加快速度
    request.setTargetServiceUniqueName(serviceName);
    request.setSerializeType(serializeType == null ? 0 : serializeType);
    if (!consumerConfig.isGeneric()) {
        // 找到调用类型, generic的时候类型在filter里进行判断
        request.setInvokeType(consumerConfig.getMethodInvokeType(request.getMethodName()));
    }
    RpcInvokeContext invokeCtx = RpcInvokeContext.peekContext();
    RpcInternalContext internalContext = RpcInternalContext.getContext();
    if (invokeCtx != null) {
        // 如果用户设置了调用级别回调函数
        SofaResponseCallback responseCallback = invokeCtx.getResponseCallback();
        if (responseCallback != null) {
            request.setSofaResponseCallback(responseCallback);
            // 一次性用完
            invokeCtx.setResponseCallback(null);
            invokeCtx.put(RemotingConstants.INVOKE_CTX_IS_ASYNC_CHAIN, isSendableResponseCallback(responseCallback));
        }
        // 如果用户设置了调用级别超时时间
        Integer timeout = invokeCtx.getTimeout();
        if (timeout != null) {
            request.setTimeout(timeout);
            // 一次性用完
            invokeCtx.setTimeout(null);
        }
        // 如果用户指定了调用的URL
        String targetURL = invokeCtx.getTargetURL();
        if (targetURL != null) {
            internalContext.setAttachment(HIDDEN_KEY_PINPOINT, targetURL);
            // 一次性用完
            invokeCtx.setTargetURL(null);
        }
        // 如果用户指定了透传数据
        if (RpcInvokeContext.isBaggageEnable()) {
            // 需要透传
            BaggageResolver.carryWithRequest(invokeCtx, request);
            internalContext.setAttachment(HIDDEN_KEY_INVOKE_CONTEXT, invokeCtx);
        }
    }
    if (RpcInternalContext.isAttachmentEnable()) {
        internalContext.setAttachment(INTERNAL_KEY_APP_NAME, consumerConfig.getAppName());
        internalContext.setAttachment(INTERNAL_KEY_PROTOCOL_NAME, consumerConfig.getProtocol());
    }
    // 额外属性通过HEAD传递给服务端
    request.addRequestProp(RemotingConstants.HEAD_APP_NAME, consumerConfig.getAppName());
    request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, consumerConfig.getProtocol());
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)

Example 20 with SofaResponseCallback

use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.

the class SofaRequestTest method getRequestProp.

@Test
public void getRequestProp() throws Exception {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(Invoker.class.getName());
    request.setMethodName("invoke");
    request.setMethod(Invoker.class.getMethod("invoke", SofaRequest.class));
    request.setMethodArgs(new Object[] { new SofaRequest() });
    request.setMethodArgSigs(new String[] { SofaRequest.class.getCanonicalName() });
    request.setTargetServiceUniqueName(Invoker.class.getName() + ":1.0");
    request.setTargetAppName("targetApp");
    request.setSerializeType((byte) 11);
    request.setTimeout(1024);
    request.setSerializeType((byte) 11);
    request.setData(new ByteArrayWrapperByteBuf(new byte[] { 1, 2, 3 }));
    request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
    request.setSofaResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
        }
    });
    Assert.assertEquals(Invoker.class.getName(), request.getInterfaceName());
    Assert.assertEquals("invoke", request.getMethodName());
    Assert.assertEquals("invoke", request.getMethod().getName());
    Assert.assertArrayEquals(new String[] { SofaRequest.class.getCanonicalName() }, request.getMethodArgSigs());
    Assert.assertEquals(1, request.getMethodArgs().length);
    Assert.assertEquals(Invoker.class.getName() + ":1.0", request.getTargetServiceUniqueName());
    Assert.assertEquals("targetApp", request.getTargetAppName());
    Assert.assertTrue(request.getSerializeType() == 11);
    Assert.assertTrue(request.getTimeout() == 1024);
    Assert.assertEquals(RpcConstants.INVOKER_TYPE_SYNC, request.getInvokeType());
    Assert.assertTrue(request.getSerializeType() == 11);
    Assert.assertTrue(request.getData().array().length == 3);
    Assert.assertNotNull(request.getSofaResponseCallback());
    Map<String, Object> map = request.getRequestProps();
    Assert.assertTrue(map == null);
    request.addRequestProp("1", "1");
    map = request.getRequestProps();
    Assert.assertTrue(map.size() == 1);
    request.addRequestProp(null, "1");
    Assert.assertTrue(map.size() == 1);
    request.addRequestProp("1", null);
    Assert.assertTrue(map.size() == 1);
    request.addRequestProps(null);
    Assert.assertTrue(map.size() == 1);
    request.addRequestProps(new HashMap<String, Object>());
    Assert.assertTrue(map.size() == 1);
    request.removeRequestProp("1");
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("1", "1");
    request.addRequestProps(hashMap);
    hashMap.put("2", "2");
    request.addRequestProps(hashMap);
    Assert.assertTrue(map.size() == 2);
    Assert.assertEquals("2", request.getRequestProp("2"));
    request.removeRequestProp(null);
    Assert.assertTrue(map.size() == 2);
    request.removeRequestProp("2");
    Assert.assertTrue(map.size() == 1);
    Assert.assertNull(request.getRequestProp("2"));
    Assert.assertFalse(request.isAsync());
    request.setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
    Assert.assertTrue(request.isAsync());
    request.setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    Assert.assertTrue(request.isAsync());
    request.setInvokeType(null);
    Assert.assertFalse(request.isAsync());
}
Also used : HashMap(java.util.HashMap) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Invoker(com.alipay.sofa.rpc.invoke.Invoker) Test(org.junit.Test)

Aggregations

SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)34 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)27 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)25 CountDownLatch (java.util.concurrent.CountDownLatch)16 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)14 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)14 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)13 Test (org.junit.Test)13 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)12 HelloService (com.alipay.sofa.rpc.test.HelloService)10 Filter (com.alipay.sofa.rpc.filter.Filter)8 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)7 ResponseFuture (com.alipay.sofa.rpc.message.ResponseFuture)7 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)6 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)6 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)5 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)5 EchoResponse (com.alipay.sofa.rpc.server.bolt.pb.EchoResponse)5 HttpService (com.alipay.sofa.rpc.server.http.HttpService)5