use of com.yanghui.elephant.client.exception.MQClientException in project elephant by yanghuijava.
the class DefaultMQProducerImpl method send.
public SendResult send(Message msg) throws MQClientException {
checkMessage(msg, defaultMQProducer);
SendResult result = new SendResult();
SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
requestHeader.setDestination(msg.getDestination());
requestHeader.setMessageId(msg.getMessageId());
requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
requestHeader.setProperties(JSON.toJSONString(msg.getProperties()));
String MessageTypeName = msg.getProperties().get(MessageConstant.PROPERTY_TRANSACTION_PREPARED);
requestHeader.setMessageType(MessageType.valueOfName(MessageTypeName).getType());
RemotingCommand request = RemotingCommand.buildRequestCmd(requestHeader, RequestCode.SEND_MESSAGE);
request.setBody(msg.getBody());
MQClientException exception = null;
int timesTotal = 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed();
result.setMsgId(msg.getMessageId());
for (int times = 0; times < timesTotal; times++) {
try {
RemotingCommand response = this.mqProducerFactory.getRemotingClient().invokeSync(choiceOneServer(), request, this.defaultMQProducer.getSendMsgTimeout());
switch(response.getCode()) {
case ResponseCode.SUCCESS:
result.setSendStatus(SendStatus.SEND_OK);
break;
case ResponseCode.FUSH_DB_FAIL:
result.setSendStatus(SendStatus.FLUSH_DB_FAIL);
break;
case ResponseCode.SEND_MQ_FAIL:
result.setSendStatus(SendStatus.SEND_MQ_FAIL);
break;
case ResponseCode.SERVER_FAIL:
result.setSendStatus(SendStatus.SERVER_FAIL);
break;
default:
result.setSendStatus(SendStatus.SEND_FAIL);
break;
}
return result;
} catch (RemotingTimeoutException e) {
result.setSendStatus(SendStatus.FLUSH_DISK_TIMEOUT);
break;
} catch (Exception e) {
exception = new MQClientException("message send exception", e);
continue;
}
}
if (exception != null) {
throw exception;
}
return result;
}
use of com.yanghui.elephant.client.exception.MQClientException 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.exception.MQClientException in project elephant by yanghuijava.
the class Producer method main.
public static void main(String[] args) throws MQClientException {
DefaultMQProducer producer = new DefaultMQProducer("test");
producer.setRegisterCenter("120.77.152.143:2181");
producer.start();
/**
* 目前只支持activemq:
* 发送queue,message的destination值加上前缀:queue://
* 发送topic,message的destination值加上前缀:topic://
*/
try {
for (int i = 0; i < 1; i++) {
Message msg = new Message("queue://yanghui.queue.test1", ("我是消息" + i).getBytes());
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
} catch (Exception e) {
e.printStackTrace();
}
producer.shutdown();
}
Aggregations