Search in sources :

Example 1 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class SofaAsyncHystrixCommand method getFallback.

@Override
protected Object getFallback() {
    events.add(SofaAsyncHystrixEvent.FALLBACK_EMIT);
    if (lock.getCount() > 0) {
        // > 0 说明 run 方法没有执行,或是执行时立刻失败了
        this.sofaResponse = buildEmptyResponse(request);
        lock.countDown();
        events.add(SofaAsyncHystrixEvent.FALLBACK_UNLOCKED);
    }
    FallbackFactory fallbackFactory = SofaHystrixConfig.loadFallbackFactory((ConsumerConfig) invoker.getConfig());
    if (fallbackFactory == null) {
        return super.getFallback();
    }
    Object fallback = fallbackFactory.create(new FallbackContext(invoker, request, this.sofaResponse, this.getExecutionException()));
    if (fallback == null) {
        return super.getFallback();
    }
    try {
        return request.getMethod().invoke(fallback, request.getMethodArgs());
    } catch (IllegalAccessException e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HYSTRIX_FALLBACK_FAIL), e);
    } catch (InvocationTargetException e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HYSTRIX_FALLBACK_FAIL), e.getTargetException());
    } finally {
        events.add(SofaAsyncHystrixEvent.FALLBACK_SUCCESS);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class TempClassLoader method export.

@Test
public void export() throws Exception {
    // 发布一个服务,每个请求要执行2秒
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig0 = new ProviderConfig<HelloService>().setId("p-0").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRegister(false);
    providerConfig0.export();
    ProviderConfig<HelloService> providerConfig1 = new ProviderConfig<HelloService>().setId("p-1").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRegister(false);
    try {
        providerConfig1.export();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaRpcRuntimeException);
    }
    ProviderConfig<HelloService> providerConfig2 = new ProviderConfig<HelloService>().setId("p-2").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRepeatedExportLimit(2).setRegister(false);
    providerConfig2.export();
    ProviderConfig<HelloService> providerConfig3 = new ProviderConfig<HelloService>().setId("p-3").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRepeatedExportLimit(2).setRegister(false);
    try {
        providerConfig3.export();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaRpcRuntimeException);
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 3 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class DynamicConfigManagerFactory method getDynamicManager.

/**
 * 得到动态配置管理
 *
 * @param alias 别名
 * @return DynamicManager 实现
 */
public static synchronized DynamicConfigManager getDynamicManager(String appName, String alias) {
    if (ALL_DYNAMICS.size() > 3) {
        // 超过3次 是不是配错了?
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Size of dynamic manager is greater than 3, Please check it!");
        }
    }
    try {
        // 注意:RegistryConfig重写了equals方法,如果多个RegistryConfig属性一样,则认为是一个对象
        DynamicConfigManager registry = ALL_DYNAMICS.get(alias);
        if (registry == null) {
            ExtensionClass<DynamicConfigManager> ext = ExtensionLoaderFactory.getExtensionLoader(DynamicConfigManager.class).getExtensionClass(alias);
            if (ext == null) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "DynamicConfigManager", alias));
            }
            registry = ext.getExtInstance(new Class[] { String.class }, new Object[] { appName });
            ALL_DYNAMICS.put(alias, registry);
        }
        return registry;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "DynamicConfigManager", alias), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ExtensionClass(com.alipay.sofa.rpc.ext.ExtensionClass)

Example 4 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class RpcConfigs method init.

private static void init() {
    try {
        // loadDefault
        String json = FileUtils.file2String(RpcConfigs.class, "rpc-config-default.json", "UTF-8");
        Map map = JSON.parseObject(json, Map.class);
        CFG.putAll(map);
        // loadCustom
        loadCustom("sofa-rpc/rpc-config.json");
        loadCustom("META-INF/sofa-rpc/rpc-config.json");
        // load system properties
        // 注意部分属性可能被覆盖为字符串
        CFG.putAll(new HashMap(System.getProperties()));
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_RPC_CONFIGS), e);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) IOException(java.io.IOException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 5 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class AddressHolderFactory method getAddressHolder.

/**
 * 根据配置得到连接管理器
 *
 * @param consumerBootstrap 服务消费者配置
 * @return AddressHolder
 */
public static AddressHolder getAddressHolder(ConsumerBootstrap consumerBootstrap) {
    String addressHolder = null;
    try {
        addressHolder = consumerBootstrap.getConsumerConfig().getAddressHolder();
        ExtensionClass<AddressHolder> ext = ExtensionLoaderFactory.getExtensionLoader(AddressHolder.class).getExtensionClass(addressHolder);
        if (ext == null) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_ADDRESS_HOLDER, addressHolder));
        }
        return ext.getExtInstance(new Class[] { ConsumerBootstrap.class }, new Object[] { consumerBootstrap });
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_ADDRESS_HOLDER, addressHolder), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Aggregations

SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)64 Method (java.lang.reflect.Method)10 KeeperException (org.apache.zookeeper.KeeperException)7 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)6 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)6 NacosException (com.alibaba.nacos.api.exception.NacosException)5 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)4 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)4 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)3 ProviderPubEvent (com.alipay.sofa.rpc.event.ProviderPubEvent)3 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)3 SslContext (io.netty.handler.ssl.SslContext)3 File (java.io.File)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 Test (org.junit.Test)3 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2