Java Code Examples for com.alibaba.rocketmq.remoting.protocol.RemotingCommand#getOpaque()

The following examples show how to use com.alibaba.rocketmq.remoting.protocol.RemotingCommand#getOpaque() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyRemotingAbstract.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis)
        throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
    final int opaque = request.getOpaque();

    try {
        final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null);
        this.responseTable.put(opaque, responseFuture);
        final SocketAddress addr = channel.remoteAddress();
        channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture f) throws Exception {
                if (f.isSuccess()) {
                    responseFuture.setSendRequestOK(true);
                    return;
                } else {
                    responseFuture.setSendRequestOK(false);
                }

                responseTable.remove(opaque);
                responseFuture.setCause(f.cause());
                responseFuture.putResponse(null);
                plog.warn("send a request command to channel <" + addr + "> failed.");
            }
        });

        RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
        if (null == responseCommand) {
            if (responseFuture.isSendRequestOK()) {
                throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis,
                        responseFuture.getCause());
            } else {
                throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
            }
        }

        return responseCommand;
    } finally {
        this.responseTable.remove(opaque);
    }
}
 
Example 2
Source File: NettyRemotingAbstract.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request,
        final long timeoutMillis) throws InterruptedException, RemotingSendRequestException,
        RemotingTimeoutException {
    try {
        final ResponseFuture responseFuture =
                new ResponseFuture(request.getOpaque(), timeoutMillis, null, null);
        this.responseTable.put(request.getOpaque(), responseFuture);
        channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture f) throws Exception {
                if (f.isSuccess()) {
                    responseFuture.setSendRequestOK(true);
                    return;
                }
                else {
                    responseFuture.setSendRequestOK(false);
                }

                responseTable.remove(request.getOpaque());
                responseFuture.setCause(f.cause());
                responseFuture.putResponse(null);
                plog.warn("send a request command to channel <" + channel.remoteAddress() + "> failed.");
                plog.warn(request.toString());
            }
        });
        //前面的发送和异步发送处理一样, 在这里通过reponseFuture的countdownlatch实现同步阻塞等待。
        RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
        if (null == responseCommand) {
            if (responseFuture.isSendRequestOK()) {
                throw new RemotingTimeoutException(RemotingHelper.parseChannelRemoteAddr(channel),
                    timeoutMillis, responseFuture.getCause());
            }
            else {
                throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel),
                    responseFuture.getCause());
            }
        }

        return responseCommand;
    }
    finally {
        this.responseTable.remove(request.getOpaque());
    }
}
 
Example 3
Source File: NettyRemotingAbstract.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
/**
 * 同步请求
 *
 * @param channel
 *            netty socket通道
 * @param request
 *            请求对象
 * @param timeoutMillis
 *            超时时间
 * @return
 * @throws InterruptedException
 * @throws RemotingSendRequestException
 * @throws RemotingTimeoutException
 */
public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request,
        final long timeoutMillis)
                throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
    try {
        // 构造ResponseFuture
        final ResponseFuture responseFuture =
                new ResponseFuture(request.getOpaque(), timeoutMillis, null, null);
        // 将ResponseFuture put进缓存列表(即map里)
        this.responseTable.put(request.getOpaque(), responseFuture);
        // 向socket 通道中写入请求并注册监听
        channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture f) throws Exception {
                if (f.isSuccess()) {
                    // 请求发送成功
                    responseFuture.setSendRequestOK(true);
                    return;
                }
                else {
                    // 请求发送失败
                    responseFuture.setSendRequestOK(false);
                }
                // 从缓存列表移除ResponseFuture
                responseTable.remove(request.getOpaque());
                // 向ResponseFuture添加异常(ChannelFuture的异常)
                responseFuture.setCause(f.cause());

                responseFuture.putResponse(null);
                plog.warn("send a request command to channel <" + channel.remoteAddress() + "> failed.");
                plog.warn(request.toString());
            }
        });
        // 等待处理response线程处理完,并获取response
        RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
        if (null == responseCommand) {
            if (responseFuture.isSendRequestOK()) {
                throw new RemotingTimeoutException(RemotingHelper.parseChannelRemoteAddr(channel),
                    timeoutMillis, responseFuture.getCause());
            }
            else {
                throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel),
                    responseFuture.getCause());
            }
        }

        return responseCommand;
    }
    finally {
        this.responseTable.remove(request.getOpaque());
    }
}