Search in sources :

Example 1 with LocalTransactionState

use of com.yanghui.elephant.common.constant.LocalTransactionState in project elephant by yanghuijava.

the class DefaultMQProducerImpl method sendMessageInTransaction.

public TransactionSendResult sendMessageInTransaction(final Message msg, final LocalTransactionExecuter tranExecuter, final Object arg) throws MQClientException {
    TransactionSendResult transactionSendResult = new TransactionSendResult();
    SendResult sendResult = null;
    msg.getProperties().put(MessageConstant.PROPERTY_TRANSACTION_PREPARED, MessageType.TRANSACTION_PRE_MESSAGE.name());
    try {
        sendResult = this.send(msg);
    } catch (Exception e) {
        throw new MQClientException("send message Exception", e);
    }
    Throwable localException = null;
    LocalTransactionState localState = LocalTransactionState.UNKNOW;
    switch(sendResult.getSendStatus()) {
        case SEND_OK:
            try {
                localState = tranExecuter.executeLocalTransactionBranch(msg, arg);
                if (null == localState) {
                    localState = LocalTransactionState.UNKNOW;
                }
                if (localState != LocalTransactionState.COMMIT_MESSAGE) {
                    log.info("executeLocalTransactionBranch return {}", localState);
                    log.info(msg.toString());
                }
            } catch (Throwable e) {
                localException = e;
                log.info("executeLocalTransactionBranch exception", e);
                log.info(msg.toString());
            }
            break;
        default:
            localState = LocalTransactionState.ROLLBACK_MESSAGE;
            break;
    }
    try {
        this.endTransaction(sendResult, localState, localException);
    } catch (Exception e) {
        log.warn("local transaction execute " + localState + ", but end broker transaction failed", e);
    }
    transactionSendResult.setMsgId(sendResult.getMsgId());
    transactionSendResult.setSendStatus(sendResult.getSendStatus());
    transactionSendResult.setLocalTransactionState(localState);
    return transactionSendResult;
}
Also used : LocalTransactionState(com.yanghui.elephant.common.constant.LocalTransactionState) SendResult(com.yanghui.elephant.client.producer.SendResult) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) 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) MQClientException(com.yanghui.elephant.client.exception.MQClientException)

Example 2 with LocalTransactionState

use of com.yanghui.elephant.common.constant.LocalTransactionState in project elephant by yanghuijava.

the class AbstractRequestProcessor method handleTransactionState.

@SuppressWarnings("unchecked")
protected void handleTransactionState(String messageId, String commitOrRollback) {
    LocalTransactionState localState = LocalTransactionState.valueOfName(commitOrRollback);
    MessageEntity findMessageEntity = this.messageEntityManager.findByMessageId(messageId);
    boolean sendMq = false;
    switch(localState) {
        case COMMIT_MESSAGE:
            this.messageEntityManager.updateStatusByMessageId(MessageStatus.CONFIRMED.getStatus(), findMessageEntity.getMessageId());
            sendMq = true;
            break;
        case ROLLBACK_MESSAGE:
            this.messageEntityManager.updateStatusByMessageId(MessageStatus.ROLLBACK.getStatus(), findMessageEntity.getMessageId());
            break;
        default:
            break;
    }
    if (sendMq) {
        Message message = new Message();
        message.setBody(findMessageEntity.getBody());
        message.setDestination(findMessageEntity.getDestination());
        message.setMessageId(findMessageEntity.getMessageId());
        if (!StringUtil.isEmpty(findMessageEntity.getProperties())) {
            message.setProperties((Map<String, String>) JSON.parseObject(findMessageEntity.getProperties(), Map.class));
        }
        this.producerService.sendMessage(message);
        this.messageEntityManager.updateSendStatusByMessageId(SendStatus.ALREADY_SEND.getStatus(), findMessageEntity.getMessageId());
    }
}
Also used : MessageEntity(com.yanghui.elephant.store.entity.MessageEntity) Message(com.yanghui.elephant.common.message.Message) LocalTransactionState(com.yanghui.elephant.common.constant.LocalTransactionState)

Example 3 with LocalTransactionState

use of com.yanghui.elephant.common.constant.LocalTransactionState in project elephant by yanghuijava.

the class DefaultMQProducerImpl method checkTransactionState.

@Override
public void checkTransactionState(final String address, final String producerGroupe, final Message msg) {
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
                LocalTransactionState localState = producer.getTransactionCheckListener().checkLocalTransactionState(msg);
                CheckTransactionStateResponseHeader header = new CheckTransactionStateResponseHeader();
                header.setCommitOrRollback(localState.name());
                header.setMessageId(msg.getMessageId());
                header.setProducerGroup(defaultMQProducer.getProducerGroup());
                RemotingCommand request = RemotingCommand.buildRequestCmd(header, RequestCode.CHECK_TRANSACTION_RESPONSE);
                mqProducerFactory.getRemotingClient().invokeOneway(address, request, 0);
            } catch (Exception e) {
                log.error("check transaction state error:{}", e);
            }
        }
    };
    this.checkExecutor.submit(run);
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) LocalTransactionState(com.yanghui.elephant.common.constant.LocalTransactionState) CheckTransactionStateResponseHeader(com.yanghui.elephant.common.protocol.header.CheckTransactionStateResponseHeader) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer) 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)

Aggregations

LocalTransactionState (com.yanghui.elephant.common.constant.LocalTransactionState)3 MQClientException (com.yanghui.elephant.client.exception.MQClientException)2 RemotingConnectException (com.yanghui.elephant.remoting.exception.RemotingConnectException)2 RemotingSendRequestException (com.yanghui.elephant.remoting.exception.RemotingSendRequestException)2 RemotingTimeoutException (com.yanghui.elephant.remoting.exception.RemotingTimeoutException)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 Message (com.yanghui.elephant.common.message.Message)1 CheckTransactionStateResponseHeader (com.yanghui.elephant.common.protocol.header.CheckTransactionStateResponseHeader)1 RemotingCommand (com.yanghui.elephant.remoting.procotol.RemotingCommand)1 MessageEntity (com.yanghui.elephant.store.entity.MessageEntity)1