protected RemotingCommand invokeSync(final Connection conn, final RemotingCommand request,
                                         final int timeoutMillis) throws RemotingException,
                                                                 InterruptedException {
        int remainingTime = remainingTime(request, timeoutMillis);
        if (remainingTime <= ABANDONING_REQUEST_THRESHOLD) {
            // already timeout
            LOGGER
                .warn(
                    "already timeout before writing to the network, requestId: {}, remoting address: {}",
                    request.getId(),
                    conn.getUrl() != null ? conn.getUrl() : RemotingUtil.parseRemoteAddress(conn
                        .getChannel()));
            return this.getCommandFactory(conn).createTimeoutResponse(conn.getRemoteAddress());
        }

        final InvokeFuture future = createInvokeFuture(request, request.getInvokeContext());
        conn.addInvokeFuture(future);
        final int requestId = request.getId();
        InvokeContext invokeContext = request.getInvokeContext();
        if (null != invokeContext) {
            invokeContext.put(InvokeContext.BOLT_PROCESS_CLIENT_BEFORE_SEND, System.nanoTime());
        }
        try {
            conn.getChannel().writeAndFlush(request).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture f) throws Exception {
                    if (!f.isSuccess()) {
                        conn.removeInvokeFuture(requestId);
                        future.putResponse(getCommandFactory(conn).createSendFailedResponse(
                            conn.getRemoteAddress(), f.cause()));
                        LOGGER.error("Invoke send failed, id={}", requestId, f.cause());
                    }
                }

            });
            if (null != invokeContext) {
                invokeContext.put(InvokeContext.BOLT_PROCESS_CLIENT_AFTER_SEND, System.nanoTime());
            }
        } catch (Exception e) {
            conn.removeInvokeFuture(requestId);
            future.putResponse(getCommandFactory(conn).createSendFailedResponse(
                conn.getRemoteAddress(), e));
            LOGGER.error("Exception caught when sending invocation, id={}", requestId, e);
        }
        RemotingCommand response = future.waitResponse(remainingTime);

        if (null != invokeContext) {
            invokeContext.put(InvokeContext.BOLT_PROCESS_CLIENT_RECEIVED, System.nanoTime());
        }

        if (response == null) {
            conn.removeInvokeFuture(requestId);
            response = this.getCommandFactory(conn).createTimeoutResponse(conn.getRemoteAddress());
            LOGGER.warn("Wait response, request id={} timeout!", requestId);
        }

        return response;
    }
Synchronous invocation

@param conn
@param request
@param timeoutMillis
@return
@throws InterruptedException 
@throws RemotingException
src/main/java/com/alipay/remoting/BaseRemoting.java