Search in sources :

Example 1 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class HeartbeatRequestProcessor method processRequest.

@Override
public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
    log.debug("Receive the heartbeat from the client:{},request:{}", ctx.channel().remoteAddress(), request);
    if (!StringUtils.isEmpty(request.getRemark())) {
        String[] groups = request.getRemark().split(",");
        for (String group : groups) {
            this.producerManager.registerProducer(group, ctx.channel());
        }
    }
    RemotingCommand response = RemotingCommand.buildResposeCmd(ResponseCode.SUCCESS, request.getUnique());
    return response;
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand)

Example 2 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand 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 3 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class DefaultMQProducerImpl method endTransaction.

private void endTransaction(SendResult sendResult, LocalTransactionState localState, Throwable localException) throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException, RemotingConnectException {
    EndTransactionRequestHeader requestHeader = new EndTransactionRequestHeader();
    requestHeader.setMsgId(sendResult.getMsgId());
    requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
    requestHeader.setCommitOrRollback(localState.name());
    RemotingCommand request = RemotingCommand.buildRequestCmd(requestHeader, RequestCode.END_TRANSACTION);
    if (localException != null) {
        request.setRemark("executeLocalTransactionBranch exception: " + localException.toString());
    }
    this.mqProducerFactory.getRemotingClient().invokeOneway(choiceOneServer(), request, this.defaultMQProducer.getSendMsgTimeout());
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) EndTransactionRequestHeader(com.yanghui.elephant.common.protocol.header.EndTransactionRequestHeader)

Example 4 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class MQClientInstance method sendHeartbeatToAllServer.

private void sendHeartbeatToAllServer() throws RemotingSendRequestException, RemotingTimeoutException, RemotingConnectException, InterruptedException {
    StringBuffer groups = new StringBuffer();
    for (Entry<String, MQProducerInner> entry : this.producerMap.entrySet()) {
        groups.append(entry.getKey()).append(",");
    }
    groups.deleteCharAt(groups.length() - 1);
    for (String address : this.servers) {
        RemotingCommand request = RemotingCommand.buildRequestCmd(RequestCode.HEART_BEAT, groups.toString());
        log.debug("Send a heartbea to 【{}】,request:{}", address, request);
        RemotingCommand response = this.remotingClient.invokeSync(address, request, 3000);
        log.debug("Receive heartbeat response:{}, from 【{}】", response, address);
    }
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand)

Example 5 with RemotingCommand

use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.

the class MessageRequestProcessor method normalMessageHandle.

@SuppressWarnings("unchecked")
private RemotingCommand normalMessageHandle(SendMessageRequestHeader requestHeader, RemotingCommand request) {
    RemotingCommand response = RemotingCommand.buildResposeCmd(ResponseCode.SERVER_FAIL, request.getUnique());
    MessageEntity entity = buildMessageEntity(request.getBody(), requestHeader, false);
    int responseCode = saveMessage(entity);
    if (responseCode != ResponseCode.SUCCESS) {
        response.setCode(responseCode);
        return response;
    }
    Message message = new Message();
    message.setBody(request.getBody());
    message.setDestination(requestHeader.getDestination());
    message.setMessageId(requestHeader.getMessageId());
    if (!StringUtil.isEmpty(requestHeader.getProperties())) {
        message.setProperties((Map<String, String>) JSON.parseObject(requestHeader.getProperties(), Map.class));
    }
    try {
        this.producerService.sendMessage(message);
    } catch (Exception e) {
        log.error("send mq exception:{}", e);
        response.setCode(ResponseCode.SEND_MQ_FAIL);
        return response;
    }
    response.setCode(ResponseCode.SUCCESS);
    try {
        this.messageEntityManager.updateSendStatusByMessageId(SendStatus.ALREADY_SEND.getStatus(), requestHeader.getMessageId());
    } catch (Exception e) {
        log.error("update message status exception:{}", e);
    }
    return response;
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) MessageEntity(com.yanghui.elephant.store.entity.MessageEntity) Message(com.yanghui.elephant.common.message.Message)

Aggregations

RemotingCommand (com.yanghui.elephant.remoting.procotol.RemotingCommand)10 RemotingSendRequestException (com.yanghui.elephant.remoting.exception.RemotingSendRequestException)3 RemotingTimeoutException (com.yanghui.elephant.remoting.exception.RemotingTimeoutException)3 MessageEntity (com.yanghui.elephant.store.entity.MessageEntity)3 MQClientException (com.yanghui.elephant.client.exception.MQClientException)2 RemotingConnectException (com.yanghui.elephant.remoting.exception.RemotingConnectException)2 SendResult (com.yanghui.elephant.client.producer.SendResult)1 TransactionMQProducer (com.yanghui.elephant.client.producer.TransactionMQProducer)1 TransactionSendResult (com.yanghui.elephant.client.producer.TransactionSendResult)1 LocalTransactionState (com.yanghui.elephant.common.constant.LocalTransactionState)1 Message (com.yanghui.elephant.common.message.Message)1 CheckTransactionStateRequestHeader (com.yanghui.elephant.common.protocol.header.CheckTransactionStateRequestHeader)1 CheckTransactionStateResponseHeader (com.yanghui.elephant.common.protocol.header.CheckTransactionStateResponseHeader)1 EndTransactionRequestHeader (com.yanghui.elephant.common.protocol.header.EndTransactionRequestHeader)1 SendMessageRequestHeader (com.yanghui.elephant.common.protocol.header.SendMessageRequestHeader)1 RequestProcessor (com.yanghui.elephant.remoting.RequestProcessor)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 SocketAddress (java.net.SocketAddress)1 ExecutorService (java.util.concurrent.ExecutorService)1