Search in sources :

Example 6 with CommunicationException

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);
    }
}
Also used : HeartEvent(com.alibaba.otter.shared.communication.core.model.heart.HeartEvent) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) Event(com.alibaba.otter.shared.communication.core.model.Event) HeartEvent(com.alibaba.otter.shared.communication.core.model.heart.HeartEvent) Method(java.lang.reflect.Method) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException)

Example 7 with CommunicationException

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);
}
Also used : CommunicationConnection(com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection) CommunicationParam(com.alibaba.otter.shared.communication.core.model.CommunicationParam) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with CommunicationException

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;
    }
}
Also used : CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CommunicationException(com.alibaba.otter.shared.communication.core.exception.CommunicationException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

CommunicationException (com.alibaba.otter.shared.communication.core.exception.CommunicationException)8 UnknownHostException (java.net.UnknownHostException)3 CommunicationParam (com.alibaba.otter.shared.communication.core.model.CommunicationParam)2 ExecutionException (java.util.concurrent.ExecutionException)2 CommunicationConnection (com.alibaba.otter.shared.communication.core.impl.connection.CommunicationConnection)1 Event (com.alibaba.otter.shared.communication.core.model.Event)1 HeartEvent (com.alibaba.otter.shared.communication.core.model.heart.HeartEvent)1 Method (java.lang.reflect.Method)1 InetAddress (java.net.InetAddress)1 RemoteException (java.rmi.RemoteException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 Future (java.util.concurrent.Future)1 RmiServiceExporter (org.springframework.remoting.rmi.RmiServiceExporter)1