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