Search in sources :

Example 1 with ResponseFuture

use of pers.cy.iris.commons.network.ResponseFuture in project iris by chicc999.

the class DefaultDispatcherHandler method processResponse.

private void processResponse(final ChannelHandlerContext ctx, final Command command) {
    Header header = command.getHeader();
    // 超时被删除了
    final ResponseFuture responseFuture = futures.remove(header.getRequestId());
    if (responseFuture == null) {
        logger.info("ack type:" + header.getTypeString() + " requestId:" + header.getRequestId() + " but responseFuture is null");
        return;
    }
    responseFuture.setResponse(command);
    //回调交给线程池,避免回调任务阻塞IO线程
    try {
        serviceExecutor.submit(new Runnable() {

            @Override
            public void run() {
                responseFuture.done();
            }
        });
    } catch (RejectedExecutionException e) {
        //队列已满,拒绝执行回调
        responseFuture.cancel(new ServiceTooBusyException("请求成功,但没有足够的资源执行回调", e));
    }
}
Also used : ServiceTooBusyException(pers.cy.iris.commons.exception.ServiceTooBusyException) Header(pers.cy.iris.commons.network.protocol.Header) ResponseFuture(pers.cy.iris.commons.network.ResponseFuture) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with ResponseFuture

use of pers.cy.iris.commons.network.ResponseFuture in project iris by chicc999.

the class NettyTransport method async.

@Override
public ResponseFuture async(Channel channel, Command command, CommandCallback callback) throws RemotingIOException {
    ArgumentUtil.isNotNull(new String[] { "channel", "command", "callback" }, channel, command, callback);
    // 同步调用
    final ResponseFuture responseFuture = new ResponseFuture(channel, command, config.getSendTimeout(), callback);
    futures.put(command.getRequestId(), responseFuture);
    channel.writeAndFlush(command).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            //写入过程出现异常,通知response,移除requestId,断开连接
            if (!future.isSuccess()) {
                Throwable failCause;
                if (future.cause() == null) {
                    failCause = new Throwable("发送请求发生了未知的错误");
                } else {
                    failCause = future.cause();
                }
                logger.error("send request error", failCause);
                responseFuture.cancel(failCause);
                futures.remove(responseFuture.getRequestId());
                future.channel().close();
            }
        }
    });
    return responseFuture;
}
Also used : ResponseFuture(pers.cy.iris.commons.network.ResponseFuture) RequestTimeoutException(pers.cy.iris.commons.exception.RequestTimeoutException) RemotingIOException(pers.cy.iris.commons.exception.RemotingIOException)

Example 3 with ResponseFuture

use of pers.cy.iris.commons.network.ResponseFuture in project iris by chicc999.

the class ClearTimeoutFutureTask method run.

@Override
public void run() {
    if (!parent.isStarted()) {
        return;
    }
    Iterator<Map.Entry<Integer, ResponseFuture>> it = futures.entrySet().iterator();
    Map.Entry<Integer, ResponseFuture> entry;
    ResponseFuture responseFuture;
    long timeout;
    while (it.hasNext()) {
        entry = it.next();
        responseFuture = entry.getValue();
        timeout = responseFuture.getBeginTime() + responseFuture.getTimeout() + 1000;
        if (timeout <= SystemClock.currentTimeMillis()) {
            it.remove();
            if (!responseFuture.isDone()) {
                try {
                    responseFuture.cancel(new RemotingIOException("请求" + responseFuture.getRequestId() + "超时," + "链接为" + responseFuture.getChannel()));
                } catch (Throwable e) {
                    logger.error("clear timeout response exception", e);
                }
                logger.info(String.format("remove timeout request id=%d begin=%d timeout=%d", responseFuture.getRequestId(), responseFuture.getBeginTime(), timeout));
            }
        }
    }
}
Also used : RemotingIOException(pers.cy.iris.commons.exception.RemotingIOException) ResponseFuture(pers.cy.iris.commons.network.ResponseFuture) Map(java.util.Map)

Example 4 with ResponseFuture

use of pers.cy.iris.commons.network.ResponseFuture in project iris by chicc999.

the class NettyTransport method sync.

@Override
public Command sync(Channel channel, Command command, int timeout) throws RemotingIOException, RequestTimeoutException {
    int sendTimeout = timeout <= 0 ? config.getSendTimeout() : timeout;
    // 同步调用
    ResponseFuture future = async(channel, command, null);
    future.setTimeout(sendTimeout);
    Command response;
    try {
        response = future.get(sendTimeout);
    } catch (InterruptedException e) {
        throw new RemotingIOException("线程被中断", e);
    }
    if (!future.isDone()) {
        throw new RequestTimeoutException("请求 requestId=" + command.getRequestId() + " 超时");
    }
    if (!future.isSuccess()) {
        throw new RemotingIOException(future.getCause());
    }
    return response;
}
Also used : RequestTimeoutException(pers.cy.iris.commons.exception.RequestTimeoutException) Command(pers.cy.iris.commons.network.protocol.Command) RemotingIOException(pers.cy.iris.commons.exception.RemotingIOException) ResponseFuture(pers.cy.iris.commons.network.ResponseFuture)

Aggregations

ResponseFuture (pers.cy.iris.commons.network.ResponseFuture)4 RemotingIOException (pers.cy.iris.commons.exception.RemotingIOException)3 RequestTimeoutException (pers.cy.iris.commons.exception.RequestTimeoutException)2 Map (java.util.Map)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ServiceTooBusyException (pers.cy.iris.commons.exception.ServiceTooBusyException)1 Command (pers.cy.iris.commons.network.protocol.Command)1 Header (pers.cy.iris.commons.network.protocol.Header)1