Search in sources :

Example 1 with RemotingTimeoutException

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

the class DefaultMQProducerImpl method send.

public SendResult send(Message msg) throws MQClientException {
    checkMessage(msg, defaultMQProducer);
    SendResult result = new SendResult();
    SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
    requestHeader.setDestination(msg.getDestination());
    requestHeader.setMessageId(msg.getMessageId());
    requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
    requestHeader.setProperties(JSON.toJSONString(msg.getProperties()));
    String MessageTypeName = msg.getProperties().get(MessageConstant.PROPERTY_TRANSACTION_PREPARED);
    requestHeader.setMessageType(MessageType.valueOfName(MessageTypeName).getType());
    RemotingCommand request = RemotingCommand.buildRequestCmd(requestHeader, RequestCode.SEND_MESSAGE);
    request.setBody(msg.getBody());
    MQClientException exception = null;
    int timesTotal = 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed();
    result.setMsgId(msg.getMessageId());
    for (int times = 0; times < timesTotal; times++) {
        try {
            RemotingCommand response = this.mqProducerFactory.getRemotingClient().invokeSync(choiceOneServer(), request, this.defaultMQProducer.getSendMsgTimeout());
            switch(response.getCode()) {
                case ResponseCode.SUCCESS:
                    result.setSendStatus(SendStatus.SEND_OK);
                    break;
                case ResponseCode.FUSH_DB_FAIL:
                    result.setSendStatus(SendStatus.FLUSH_DB_FAIL);
                    break;
                case ResponseCode.SEND_MQ_FAIL:
                    result.setSendStatus(SendStatus.SEND_MQ_FAIL);
                    break;
                case ResponseCode.SERVER_FAIL:
                    result.setSendStatus(SendStatus.SERVER_FAIL);
                    break;
                default:
                    result.setSendStatus(SendStatus.SEND_FAIL);
                    break;
            }
            return result;
        } catch (RemotingTimeoutException e) {
            result.setSendStatus(SendStatus.FLUSH_DISK_TIMEOUT);
            break;
        } catch (Exception e) {
            exception = new MQClientException("message send exception", e);
            continue;
        }
    }
    if (exception != null) {
        throw exception;
    }
    return result;
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) SendMessageRequestHeader(com.yanghui.elephant.common.protocol.header.SendMessageRequestHeader) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException) SendResult(com.yanghui.elephant.client.producer.SendResult) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) MQClientException(com.yanghui.elephant.client.exception.MQClientException) RemotingSendRequestException(com.yanghui.elephant.remoting.exception.RemotingSendRequestException) MQClientException(com.yanghui.elephant.client.exception.MQClientException) RemotingConnectException(com.yanghui.elephant.remoting.exception.RemotingConnectException) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException)

Example 2 with RemotingTimeoutException

use of com.yanghui.elephant.remoting.exception.RemotingTimeoutException 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)2 RemotingTimeoutException (com.yanghui.elephant.remoting.exception.RemotingTimeoutException)2 RemotingCommand (com.yanghui.elephant.remoting.procotol.RemotingCommand)2 MQClientException (com.yanghui.elephant.client.exception.MQClientException)1 SendResult (com.yanghui.elephant.client.producer.SendResult)1 TransactionSendResult (com.yanghui.elephant.client.producer.TransactionSendResult)1 SendMessageRequestHeader (com.yanghui.elephant.common.protocol.header.SendMessageRequestHeader)1 RemotingConnectException (com.yanghui.elephant.remoting.exception.RemotingConnectException)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 SocketAddress (java.net.SocketAddress)1