Search in sources :

Example 1 with SendResult

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;
}
Also used : SendResult(org.apache.eventmesh.api.SendResult)

Example 2 with 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()));
    }
}
Also used : ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException) MessageEntity(org.apache.eventmesh.connector.standalone.broker.model.MessageEntity) SendResult(org.apache.eventmesh.api.SendResult) ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException)

Example 3 with SendResult

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);
    }
}
Also used : ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SendResult(org.apache.eventmesh.api.SendResult) ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException)

Example 4 with SendResult

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);
        }
    };
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SendResult(org.apache.eventmesh.api.SendResult) Package(org.apache.eventmesh.common.protocol.tcp.Package) SendCallback(org.apache.eventmesh.api.SendCallback)

Example 5 with SendResult

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());
        }
    });
}
Also used : EventMeshProducer(org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) ProducerManager(org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) AclException(org.apache.eventmesh.api.exception.AclException) SendMessageContext(org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext) SendResult(org.apache.eventmesh.api.SendResult) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) CloudEvent(io.cloudevents.CloudEvent) SendCallback(org.apache.eventmesh.api.SendCallback)

Aggregations

SendResult (org.apache.eventmesh.api.SendResult)20 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)17 SendCallback (org.apache.eventmesh.api.SendCallback)15 CloudEvent (io.cloudevents.CloudEvent)7 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)7 ConnectorRuntimeException (org.apache.eventmesh.api.exception.ConnectorRuntimeException)5 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.http.producer.EventMeshProducer)5 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.http.producer.SendMessageContext)5 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)4 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer)4 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext)4 AclException (org.apache.eventmesh.api.exception.AclException)3 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)3 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)2 ProducerManager (org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager)2 CompleteHandler (org.apache.eventmesh.runtime.core.protocol.http.async.CompleteHandler)2 UpStreamMsgContext (org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.UpStreamMsgContext)2 CloudEventData (io.cloudevents.CloudEventData)1 CloudEventBuilder (io.cloudevents.core.builder.CloudEventBuilder)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1