use of com.alibaba.otter.shared.communication.core.exception.CommunicationException in project otter by alibaba.
the class AbstractCommunicationEndpoint method acceptEvent.
/**
* 处理指定的事件
*/
public Object acceptEvent(Event event) {
if (event instanceof HeartEvent) {
// 针对心跳请求,返回一个随意结果
return event;
}
try {
Object action = CommunicationRegistry.getAction(event.getType());
if (action != null) {
// 通过反射获取方法并执行
String methodName = "on" + StringUtils.capitalize(event.getType().toString());
Method method = ReflectionUtils.findMethod(action.getClass(), methodName, new Class[] { event.getClass() });
if (method == null) {
// 尝试一下默认方法
methodName = DEFAULT_METHOD;
method = ReflectionUtils.findMethod(action.getClass(), methodName, new Class[] { event.getClass() });
if (method == null) {
// 再尝试一下Event参数
method = ReflectionUtils.findMethod(action.getClass(), methodName, new Class[] { Event.class });
}
}
// 方法不为空就调用指定的方法,反之调用缺省的处理函数
if (method != null) {
try {
ReflectionUtils.makeAccessible(method);
return method.invoke(action, new Object[] { event });
} catch (Throwable e) {
throw new CommunicationException("method_invoke_error:" + methodName, e);
}
} else {
throw new CommunicationException("no_method_error for[" + StringUtils.capitalize(event.getType().toString()) + "] in Class[" + action.getClass().getName() + "]");
}
}
throw new CommunicationException("eventType_no_action", event.getType().name());
} catch (RuntimeException e) {
logger.error("endpoint_error", e);
throw e;
} catch (Exception e) {
logger.error("endpoint_error", e);
throw new CommunicationException(e);
}
}
use of com.alibaba.otter.shared.communication.core.exception.CommunicationException in project otter by alibaba.
the class DefaultCommunicationClientImpl method call.
public Object call(final String addr, final Event event) {
Assert.notNull(this.factory, "No factory specified");
CommunicationParam params = buildParams(addr);
CommunicationConnection connection = null;
int count = 0;
Throwable ex = null;
while (count++ < retry) {
try {
connection = factory.createConnection(params);
return connection.call(event);
} catch (Exception e) {
logger.error(String.format("call[%s] , retry[%s]", addr, count), e);
try {
Thread.sleep(count * retryDelay);
} catch (InterruptedException e1) {
// ignore
}
ex = e;
} finally {
if (connection != null) {
connection.close();
}
}
}
logger.error("call[{}] failed , event[{}]!", addr, event.toString());
throw new CommunicationException("call[" + addr + "] , Event[" + event.toString() + "]", ex);
}
use of com.alibaba.otter.shared.communication.core.exception.CommunicationException in project otter by alibaba.
the class DefaultCommunicationClientImpl method call.
public Object call(final String[] addrs, final Event event) {
Assert.notNull(this.factory, "No factory specified");
if (addrs == null || addrs.length == 0) {
throw new IllegalArgumentException("addrs example: 127.0.0.1:1099");
}
ExecutorCompletionService completionService = new ExecutorCompletionService(executor);
List<Future<Object>> futures = new ArrayList<Future<Object>>(addrs.length);
List result = new ArrayList(10);
for (final String addr : addrs) {
futures.add(completionService.submit((new Callable<Object>() {
@Override
public Object call() throws Exception {
return DefaultCommunicationClientImpl.this.call(addr, event);
}
})));
}
Exception ex = null;
int errorIndex = 0;
while (errorIndex < futures.size()) {
try {
// 它也可能被打断
Future future = completionService.take();
future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
ex = e;
break;
} catch (ExecutionException e) {
ex = e;
break;
}
errorIndex++;
}
if (errorIndex < futures.size()) {
for (int index = 0; index < futures.size(); index++) {
Future<Object> future = futures.get(index);
if (future.isDone() == false) {
future.cancel(true);
}
}
} else {
for (int index = 0; index < futures.size(); index++) {
Future<Object> future = futures.get(index);
try {
result.add(future.get());
} catch (InterruptedException e) {
// ignore
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
// ignore
}
}
}
if (ex != null) {
throw new CommunicationException(String.format("call addr[%s] error by %s", addrs[errorIndex], ex.getMessage()), ex);
} else {
return result;
}
}
Aggregations