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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations