Search in sources :

Example 1 with TransactionSendResult

use of com.yanghui.elephant.client.producer.TransactionSendResult 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 TransactionSendResult

use of com.yanghui.elephant.client.producer.TransactionSendResult in project elephant by yanghuijava.

the class TransactionProducer method main.

public static void main(String[] args) throws MQClientException {
    TransactionMQProducer producer = new TransactionMQProducer("transaction-test");
    producer.setRegisterCenter("120.77.152.143:2181");
    producer.setTransactionCheckListener(new TransactionCheckListenerImpl());
    producer.start();
    LocalTransactionExecuter excuter = new LocalTransactionExecuterImpl();
    /**
     * 目前只支持activemq:
     * 发送queue,message的destination值加上前缀:queue://
     * 发送topic,message的destination值加上前缀:topic://
     */
    for (int i = 0; i < 1; i++) {
        Message msg = new Message("queue://yanghui.queue.test1", ("我是事务消息" + i).getBytes());
        TransactionSendResult transactionSendResult = producer.sendMessageTransaction(msg, excuter, null);
        System.out.println(transactionSendResult);
    }
// producer.shutdown();
}
Also used : Message(com.yanghui.elephant.common.message.Message) LocalTransactionExecuter(com.yanghui.elephant.client.producer.LocalTransactionExecuter) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer)

Example 3 with TransactionSendResult

use of com.yanghui.elephant.client.producer.TransactionSendResult in project elephant by yanghuijava.

the class TransactionProducer1 method main.

public static void main(String[] args) throws MQClientException {
    TransactionMQProducer producer1 = new TransactionMQProducer("transaction-test1");
    producer1.setRegisterCenter("172.16.21.12:2181");
    producer1.setTransactionCheckListener(new TransactionCheckListenerImpl());
    producer1.start();
    TransactionMQProducer producer2 = new TransactionMQProducer("transaction-test2");
    producer2.setRegisterCenter("172.16.21.12:2181");
    producer2.setTransactionCheckListener(new TransactionCheckListenerImpl2());
    producer2.start();
    LocalTransactionExecuter excuter = new LocalTransactionExecuterImpl();
    Message msg1 = new Message("topic://VirtualTopic.Test", ("我是事务消息1").getBytes());
    TransactionSendResult transactionSendResult1 = producer1.sendMessageTransaction(msg1, excuter, null);
    System.out.println("transactionSendResult1=" + transactionSendResult1);
    Message msg2 = new Message("topic://VirtualTopic.Test", ("我是事务消息2").getBytes());
    TransactionSendResult transactionSendResult2 = producer2.sendMessageTransaction(msg2, excuter, null);
    System.out.println("transactionSendResult2=" + transactionSendResult2);
}
Also used : Message(com.yanghui.elephant.common.message.Message) LocalTransactionExecuter(com.yanghui.elephant.client.producer.LocalTransactionExecuter) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer)

Aggregations

TransactionSendResult (com.yanghui.elephant.client.producer.TransactionSendResult)3 LocalTransactionExecuter (com.yanghui.elephant.client.producer.LocalTransactionExecuter)2 TransactionMQProducer (com.yanghui.elephant.client.producer.TransactionMQProducer)2 Message (com.yanghui.elephant.common.message.Message)2 MQClientException (com.yanghui.elephant.client.exception.MQClientException)1 SendResult (com.yanghui.elephant.client.producer.SendResult)1 LocalTransactionState (com.yanghui.elephant.common.constant.LocalTransactionState)1 RemotingConnectException (com.yanghui.elephant.remoting.exception.RemotingConnectException)1 RemotingSendRequestException (com.yanghui.elephant.remoting.exception.RemotingSendRequestException)1 RemotingTimeoutException (com.yanghui.elephant.remoting.exception.RemotingTimeoutException)1