use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.
the class CloudEventUtils method convertSendResult.
public static SendResult convertSendResult(org.apache.rocketmq.client.producer.SendResult rmqResult) {
SendResult sendResult = new SendResult();
sendResult.setTopic(rmqResult.getMessageQueue().getTopic());
sendResult.setMessageId(rmqResult.getMsgId());
return sendResult;
}
use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.
the class StandaloneProducer method publish.
public SendResult publish(CloudEvent cloudEvent) {
Preconditions.checkNotNull(cloudEvent);
try {
MessageEntity messageEntity = standaloneBroker.putMessage(cloudEvent.getSubject(), cloudEvent);
SendResult sendResult = new SendResult();
sendResult.setTopic(cloudEvent.getSubject());
sendResult.setMessageId(String.valueOf(messageEntity.getOffset()));
return sendResult;
} catch (Exception e) {
logger.error("send message error, topic: {}", cloudEvent.getSubject(), e);
throw new ConnectorRuntimeException(String.format("Send message error, topic: %s", cloudEvent.getSubject()));
}
}
use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.
the class StandaloneProducer method sendAsync.
public void sendAsync(CloudEvent cloudEvent, SendCallback sendCallback) {
Preconditions.checkNotNull(cloudEvent, "CloudEvent cannot be null");
Preconditions.checkNotNull(sendCallback, "Callback cannot be null");
// todo: current is not async
try {
SendResult sendResult = publish(cloudEvent);
sendCallback.onSuccess(sendResult);
} catch (Exception ex) {
OnExceptionContext onExceptionContext = OnExceptionContext.builder().messageId(cloudEvent.getId()).topic(cloudEvent.getSubject()).exception(new ConnectorRuntimeException(ex)).build();
sendCallback.onException(onExceptionContext);
}
}
use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.
the class UpStreamMsgContext method createSendCallback.
protected SendCallback createSendCallback(Command replyCmd, long taskExecuteTime, CloudEvent event) {
final long createTime = System.currentTimeMillis();
Package msg = new Package();
return new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
session.getSender().getUpstreamBuff().release();
logger.info("upstreamMsg message success|user={}|callback cost={}", session.getClient(), System.currentTimeMillis() - createTime);
if (replyCmd.equals(Command.BROADCAST_MESSAGE_TO_SERVER_ACK) || replyCmd.equals(Command.ASYNC_MESSAGE_TO_SERVER_ACK)) {
msg.setHeader(new Header(replyCmd, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), seq));
msg.setBody(event);
Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
}
}
@Override
public void onException(OnExceptionContext context) {
session.getSender().getUpstreamBuff().release();
// retry
UpStreamMsgContext upStreamMsgContext = new UpStreamMsgContext(session, event, header, startTime, taskExecuteTime);
upStreamMsgContext.delay(10000);
session.getClientGroupWrapper().get().getEventMeshTcpRetryer().pushRetry(upStreamMsgContext);
session.getSender().failMsgCount.incrementAndGet();
logger.error("upstreamMsg mq message error|user={}|callback cost={}, errMsg={}", session.getClient(), System.currentTimeMillis() - createTime, new Exception(context.getException()));
msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), context.getException().toString(), seq));
msg.setBody(event);
Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
}
};
}
use of org.apache.eventmesh.api.SendResult in project incubator-eventmesh by apache.
the class ReplyMessageProcessor method process.
public void process(SimpleMessage message, EventEmitter<SimpleMessage> emitter) throws Exception {
RequestHeader requestHeader = message.getHeader();
if (!ServiceUtils.validateHeader(requestHeader)) {
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
return;
}
if (!ServiceUtils.validateMessage(message)) {
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
return;
}
try {
doAclCheck(message);
} catch (Exception e) {
aclLogger.warn("CLIENT HAS NO PERMISSION,RequestReplyMessageProcessor reply failed", e);
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_ACL_ERR, e.getMessage(), emitter);
return;
}
// control flow rate limit
if (!eventMeshGrpcServer.getMsgRateLimiter().tryAcquire(EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) {
logger.error("Send message speed over limit.");
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_SEND_MESSAGE_SPEED_OVER_LIMIT_ERR, emitter);
return;
}
String seqNum = message.getSeqNum();
String uniqueId = message.getUniqueId();
String producerGroup = message.getProducerGroup();
// set reply topic for ths message
String mqCluster = message.getPropertiesOrDefault(EventMeshConstants.PROPERTY_MESSAGE_CLUSTER, "defaultCluster");
String replyTopic = mqCluster + "-" + EventMeshConstants.RR_REPLY_TOPIC;
message = SimpleMessage.newBuilder(message).setTopic(replyTopic).build();
String protocolType = requestHeader.getProtocolType();
ProtocolAdaptor<ProtocolTransportObject> grpcCommandProtocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
CloudEvent cloudEvent = grpcCommandProtocolAdaptor.toCloudEvent(new SimpleMessageWrapper(message));
ProducerManager producerManager = eventMeshGrpcServer.getProducerManager();
EventMeshProducer eventMeshProducer = producerManager.getEventMeshProducer(producerGroup);
SendMessageContext sendMessageContext = new SendMessageContext(message.getSeqNum(), cloudEvent, eventMeshProducer, eventMeshGrpcServer);
long startTime = System.currentTimeMillis();
eventMeshProducer.reply(sendMessageContext, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
long endTime = System.currentTimeMillis();
logger.info("message|mq2eventmesh|REPLY|ReplyToServer|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, replyTopic, seqNum, uniqueId);
}
@Override
public void onException(OnExceptionContext onExceptionContext) {
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_REPLY_MSG_ERR, EventMeshUtil.stackTrace(onExceptionContext.getException(), 2), emitter);
long endTime = System.currentTimeMillis();
logger.error("message|mq2eventmesh|REPLY|ReplyToServer|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, replyTopic, seqNum, uniqueId, onExceptionContext.getException());
}
});
}
Aggregations