Search in sources :

Example 1 with TransportException

use of com.weibo.api.motan.transport.TransportException in project motan by weibocom.

the class NettyChannel method request.

@Override
public Response request(Request request) throws TransportException {
    int timeout = nettyClient.getUrl().getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.requestTimeout.getName(), URLParamType.requestTimeout.getIntValue());
    if (timeout <= 0) {
        throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
    NettyResponseFuture response = new NettyResponseFuture(request, timeout, this.nettyClient);
    this.nettyClient.registerCallback(request.getRequestId(), response);
    ChannelFuture writeFuture = this.channel.write(request);
    boolean result = writeFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS);
    if (result && writeFuture.isSuccess()) {
        response.addListener(new FutureListener() {

            @Override
            public void operationComplete(Future future) throws Exception {
                if (future.isSuccess() || (future.isDone() && ExceptionUtil.isBizException(future.getException()))) {
                    // 成功的调用 
                    nettyClient.resetErrorCount();
                } else {
                    // 失败的调用 
                    nettyClient.incrErrorCount();
                }
            }
        });
        return response;
    }
    writeFuture.cancel();
    response = this.nettyClient.removeCallback(request.getRequestId());
    if (response != null) {
        response.cancel();
    }
    // 失败的调用 
    nettyClient.incrErrorCount();
    if (writeFuture.getCause() != null) {
        throw new MotanServiceException("NettyChannel send request to server Error: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request), writeFuture.getCause());
    } else {
        throw new MotanServiceException("NettyChannel send request to server Timeout: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request));
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) FutureListener(com.weibo.api.motan.rpc.FutureListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Future(com.weibo.api.motan.rpc.Future) ChannelFuture(org.jboss.netty.channel.ChannelFuture) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) TransportException(com.weibo.api.motan.transport.TransportException)

Example 2 with TransportException

use of com.weibo.api.motan.transport.TransportException in project motan by weibocom.

the class NettyClient method request.

/**
	 * 请求remote service
	 * 
	 * <pre>
	 * 		1)  get connection from pool
	 * 		2)  async requset
	 * 		3)  return connection to pool
	 * 		4)  check if async return response, true: return ResponseFuture;  false: return result
	 * </pre>
	 * 
	 * @param request
	 * @param async
	 * @return
	 * @throws TransportException
	 */
private Response request(Request request, boolean async) throws TransportException {
    Channel channel = null;
    Response response = null;
    try {
        // return channel or throw exception(timeout or connection_fail)
        channel = borrowObject();
        if (channel == null) {
            LoggerUtil.error("NettyClient borrowObject null: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request));
            return null;
        }
        // async request
        response = channel.request(request);
        // return channel to pool
        returnObject(channel);
    } catch (Exception e) {
        LoggerUtil.error("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
        //TODO 对特定的异常回收channel
        invalidateObject(channel);
        if (e instanceof MotanAbstractException) {
            throw (MotanAbstractException) e;
        } else {
            throw new MotanServiceException("NettyClient request Error: url=" + url.getUri() + " " + MotanFrameworkUtil.toString(request), e);
        }
    }
    // aysnc or sync result
    response = asyncResponse(response, async);
    return response;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) MotanAbstractException(com.weibo.api.motan.exception.MotanAbstractException) Channel(com.weibo.api.motan.transport.Channel) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanAbstractException(com.weibo.api.motan.exception.MotanAbstractException) TransportException(com.weibo.api.motan.transport.TransportException)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)2 TransportException (com.weibo.api.motan.transport.TransportException)2 MotanAbstractException (com.weibo.api.motan.exception.MotanAbstractException)1 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)1 Future (com.weibo.api.motan.rpc.Future)1 FutureListener (com.weibo.api.motan.rpc.FutureListener)1 Response (com.weibo.api.motan.rpc.Response)1 Channel (com.weibo.api.motan.transport.Channel)1 ChannelFuture (org.jboss.netty.channel.ChannelFuture)1