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;
}
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();
}
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);
}
Aggregations