Search in sources :

Example 1 with RemotingSendRequestException

use of com.yanghui.elephant.remoting.exception.RemotingSendRequestException in project elephant by yanghuijava.

the class NettyRemotingAbstract method invokeSyncImpl.

public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingTimeoutException, RemotingSendRequestException {
    final int unique = request.getUnique();
    try {
        final SocketAddress addr = channel.remoteAddress();
        final ResponseFuture responseFuture = new ResponseFuture(unique, timeoutMillis);
        this.responseTable.put(unique, responseFuture);
        channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    responseFuture.setSendRequestOK(true);
                    return;
                }
                responseFuture.setSendRequestOK(false);
                responseTable.remove(unique);
                responseFuture.setCause(future.cause());
                responseFuture.putResponse(null);
                log.warn("send a request command to channel <" + addr + "> failed.");
            }
        });
        RemotingCommand respose = responseFuture.waitRespose(timeoutMillis);
        if (null == respose) {
            if (responseFuture.isSendRequestOK()) {
                throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis, responseFuture.getCause());
            } else {
                throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
            }
        }
        return respose;
    } finally {
        this.responseTable.remove(unique);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) RemotingSendRequestException(com.yanghui.elephant.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException) SocketAddress(java.net.SocketAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingSendRequestException(com.yanghui.elephant.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException)

Aggregations

RemotingSendRequestException (com.yanghui.elephant.remoting.exception.RemotingSendRequestException)1 RemotingTimeoutException (com.yanghui.elephant.remoting.exception.RemotingTimeoutException)1 RemotingCommand (com.yanghui.elephant.remoting.procotol.RemotingCommand)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 SocketAddress (java.net.SocketAddress)1