use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.
the class HeartbeatRequestProcessor method processRequest.
@Override
public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
log.debug("Receive the heartbeat from the client:{},request:{}", ctx.channel().remoteAddress(), request);
if (!StringUtils.isEmpty(request.getRemark())) {
String[] groups = request.getRemark().split(",");
for (String group : groups) {
this.producerManager.registerProducer(group, ctx.channel());
}
}
RemotingCommand response = RemotingCommand.buildResposeCmd(ResponseCode.SUCCESS, request.getUnique());
return response;
}
use of com.yanghui.elephant.remoting.procotol.RemotingCommand 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.remoting.procotol.RemotingCommand in project elephant by yanghuijava.
the class DefaultMQProducerImpl method endTransaction.
private void endTransaction(SendResult sendResult, LocalTransactionState localState, Throwable localException) throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException, RemotingConnectException {
EndTransactionRequestHeader requestHeader = new EndTransactionRequestHeader();
requestHeader.setMsgId(sendResult.getMsgId());
requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
requestHeader.setCommitOrRollback(localState.name());
RemotingCommand request = RemotingCommand.buildRequestCmd(requestHeader, RequestCode.END_TRANSACTION);
if (localException != null) {
request.setRemark("executeLocalTransactionBranch exception: " + localException.toString());
}
this.mqProducerFactory.getRemotingClient().invokeOneway(choiceOneServer(), request, this.defaultMQProducer.getSendMsgTimeout());
}
use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.
the class MQClientInstance method sendHeartbeatToAllServer.
private void sendHeartbeatToAllServer() throws RemotingSendRequestException, RemotingTimeoutException, RemotingConnectException, InterruptedException {
StringBuffer groups = new StringBuffer();
for (Entry<String, MQProducerInner> entry : this.producerMap.entrySet()) {
groups.append(entry.getKey()).append(",");
}
groups.deleteCharAt(groups.length() - 1);
for (String address : this.servers) {
RemotingCommand request = RemotingCommand.buildRequestCmd(RequestCode.HEART_BEAT, groups.toString());
log.debug("Send a heartbea to 【{}】,request:{}", address, request);
RemotingCommand response = this.remotingClient.invokeSync(address, request, 3000);
log.debug("Receive heartbeat response:{}, from 【{}】", response, address);
}
}
use of com.yanghui.elephant.remoting.procotol.RemotingCommand in project elephant by yanghuijava.
the class MessageRequestProcessor method normalMessageHandle.
@SuppressWarnings("unchecked")
private RemotingCommand normalMessageHandle(SendMessageRequestHeader requestHeader, RemotingCommand request) {
RemotingCommand response = RemotingCommand.buildResposeCmd(ResponseCode.SERVER_FAIL, request.getUnique());
MessageEntity entity = buildMessageEntity(request.getBody(), requestHeader, false);
int responseCode = saveMessage(entity);
if (responseCode != ResponseCode.SUCCESS) {
response.setCode(responseCode);
return response;
}
Message message = new Message();
message.setBody(request.getBody());
message.setDestination(requestHeader.getDestination());
message.setMessageId(requestHeader.getMessageId());
if (!StringUtil.isEmpty(requestHeader.getProperties())) {
message.setProperties((Map<String, String>) JSON.parseObject(requestHeader.getProperties(), Map.class));
}
try {
this.producerService.sendMessage(message);
} catch (Exception e) {
log.error("send mq exception:{}", e);
response.setCode(ResponseCode.SEND_MQ_FAIL);
return response;
}
response.setCode(ResponseCode.SUCCESS);
try {
this.messageEntityManager.updateSendStatusByMessageId(SendStatus.ALREADY_SEND.getStatus(), requestHeader.getMessageId());
} catch (Exception e) {
log.error("update message status exception:{}", e);
}
return response;
}
Aggregations