Search in sources :

Example 6 with EndTransactionRequestHeader

use of org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader in project rocketmq by apache.

the class DefaultMQProducerImpl method checkTransactionState.

@Override
public void checkTransactionState(final String addr, final MessageExt msg, final CheckTransactionStateRequestHeader header) {
    Runnable request = new Runnable() {

        private final String brokerAddr = addr;

        private final MessageExt message = msg;

        private final CheckTransactionStateRequestHeader checkRequestHeader = header;

        private final String group = DefaultMQProducerImpl.this.defaultMQProducer.getProducerGroup();

        @Override
        public void run() {
            TransactionCheckListener transactionCheckListener = DefaultMQProducerImpl.this.checkListener();
            if (transactionCheckListener != null) {
                LocalTransactionState localTransactionState = LocalTransactionState.UNKNOW;
                Throwable exception = null;
                try {
                    localTransactionState = transactionCheckListener.checkLocalTransactionState(message);
                } catch (Throwable e) {
                    log.error("Broker call checkTransactionState, but checkLocalTransactionState exception", e);
                    exception = e;
                }
                this.processTransactionState(localTransactionState, group, exception);
            } else {
                log.warn("checkTransactionState, pick transactionCheckListener by group[{}] failed", group);
            }
        }

        private void processTransactionState(final LocalTransactionState localTransactionState, final String producerGroup, final Throwable exception) {
            final EndTransactionRequestHeader thisHeader = new EndTransactionRequestHeader();
            thisHeader.setCommitLogOffset(checkRequestHeader.getCommitLogOffset());
            thisHeader.setProducerGroup(producerGroup);
            thisHeader.setTranStateTableOffset(checkRequestHeader.getTranStateTableOffset());
            thisHeader.setFromTransactionCheck(true);
            String uniqueKey = message.getProperties().get(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
            if (uniqueKey == null) {
                uniqueKey = message.getMsgId();
            }
            thisHeader.setMsgId(uniqueKey);
            thisHeader.setTransactionId(checkRequestHeader.getTransactionId());
            switch(localTransactionState) {
                case COMMIT_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_COMMIT_TYPE);
                    break;
                case ROLLBACK_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_ROLLBACK_TYPE);
                    log.warn("when broker check, client rollback this transaction, {}", thisHeader);
                    break;
                case UNKNOW:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_NOT_TYPE);
                    log.warn("when broker check, client does not know this transaction state, {}", thisHeader);
                    break;
                default:
                    break;
            }
            String remark = null;
            if (exception != null) {
                remark = "checkLocalTransactionState Exception: " + RemotingHelper.exceptionSimpleDesc(exception);
            }
            try {
                DefaultMQProducerImpl.this.mQClientFactory.getMQClientAPIImpl().endTransactionOneway(brokerAddr, thisHeader, remark, 3000);
            } catch (Exception e) {
                log.error("endTransactionOneway exception", e);
            }
        }
    };
    this.checkExecutor.submit(request);
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) LocalTransactionState(org.apache.rocketmq.client.producer.LocalTransactionState) CheckTransactionStateRequestHeader(org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) EndTransactionRequestHeader(org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader)

Aggregations

EndTransactionRequestHeader (org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)4 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 LocalTransactionState (org.apache.rocketmq.client.producer.LocalTransactionState)2 TransactionCheckListener (org.apache.rocketmq.client.producer.TransactionCheckListener)2 MessageId (org.apache.rocketmq.common.message.MessageId)2 CheckTransactionStateRequestHeader (org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader)2 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)2 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)2 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)2 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)2 MessageExtBrokerInner (org.apache.rocketmq.store.MessageExtBrokerInner)2 MessageStore (org.apache.rocketmq.store.MessageStore)2 PutMessageResult (org.apache.rocketmq.store.PutMessageResult)2