Search in sources :

Example 1 with SendMessageContext

use of org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext 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)

Example 2 with SendMessageContext

use of org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext in project incubator-eventmesh by apache.

the class RequestMessageProcessor method process.

public void process(SimpleMessage message, EventEmitter<SimpleMessage> emitter) throws Exception {
    RequestHeader requestHeader = message.getHeader();
    if (!ServiceUtils.validateHeader(requestHeader)) {
        ServiceUtils.sendStreamRespAndDone(message.getHeader(), StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
        return;
    }
    if (!ServiceUtils.validateMessage(message)) {
        ServiceUtils.sendStreamRespAndDone(message.getHeader(), StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    String seqNum = message.getSeqNum();
    String uniqueId = message.getUniqueId();
    String topic = message.getTopic();
    String producerGroup = message.getProducerGroup();
    int ttl = Integer.parseInt(message.getTtl());
    try {
        doAclCheck(message);
    } catch (Exception e) {
        aclLogger.warn("CLIENT HAS NO PERMISSION,RequestReplyMessageProcessor send failed", e);
        ServiceUtils.sendStreamRespAndDone(message.getHeader(), 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(message.getHeader(), StatusCode.EVENTMESH_SEND_MESSAGE_SPEED_OVER_LIMIT_ERR, emitter);
        return;
    }
    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.request(sendMessageContext, new RequestReplyCallback() {

        @Override
        public void onSuccess(CloudEvent event) {
            try {
                SimpleMessageWrapper wrapper = (SimpleMessageWrapper) grpcCommandProtocolAdaptor.fromCloudEvent(event);
                emitter.onNext(wrapper.getMessage());
                emitter.onCompleted();
                long endTime = System.currentTimeMillis();
                logger.info("message|eventmesh2client|REPLY|RequestReply|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, topic, seqNum, uniqueId);
            } catch (Exception e) {
                ServiceUtils.sendStreamRespAndDone(message.getHeader(), StatusCode.EVENTMESH_REQUEST_REPLY_MSG_ERR, EventMeshUtil.stackTrace(e, 2), emitter);
                long endTime = System.currentTimeMillis();
                logger.error("message|mq2eventmesh|REPLY|RequestReply|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, topic, seqNum, uniqueId, e);
            }
        }

        @Override
        public void onException(Throwable e) {
            ServiceUtils.sendStreamRespAndDone(message.getHeader(), StatusCode.EVENTMESH_REQUEST_REPLY_MSG_ERR, EventMeshUtil.stackTrace(e, 2), emitter);
            long endTime = System.currentTimeMillis();
            logger.error("message|eventMesh2mq|REPLY|RequestReply|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, topic, seqNum, uniqueId, e);
        }
    }, ttl);
}
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) 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) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) RequestReplyCallback(org.apache.eventmesh.api.RequestReplyCallback) CloudEvent(io.cloudevents.CloudEvent)

Example 3 with SendMessageContext

use of org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext in project incubator-eventmesh by apache.

the class SendAsyncMessageProcessor method process.

public void process(SimpleMessage message, EventEmitter<Response> emitter) throws Exception {
    RequestHeader requestHeader = message.getHeader();
    if (!ServiceUtils.validateHeader(requestHeader)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
        return;
    }
    if (!ServiceUtils.validateMessage(message)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    String seqNum = message.getSeqNum();
    String uniqueId = message.getUniqueId();
    String topic = message.getTopic();
    String producerGroup = message.getProducerGroup();
    try {
        doAclCheck(message);
    } catch (Exception e) {
        aclLogger.warn("CLIENT HAS NO PERMISSION,SendAsyncMessageProcessor send failed", e);
        ServiceUtils.sendRespAndDone(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.sendRespAndDone(StatusCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR, emitter);
        return;
    }
    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.send(sendMessageContext, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            ServiceUtils.sendRespAndDone(StatusCode.SUCCESS, sendResult.toString(), emitter);
            long endTime = System.currentTimeMillis();
            logger.info("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, topic, seqNum, uniqueId);
        }

        @Override
        public void onException(OnExceptionContext context) {
            ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_SEND_ASYNC_MSG_ERR, EventMeshUtil.stackTrace(context.getException(), 2), emitter);
            long endTime = System.currentTimeMillis();
            logger.error("message|eventMesh2mq|REQ|ASYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, topic, seqNum, uniqueId, context.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)

Example 4 with SendMessageContext

use of org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext in project incubator-eventmesh by apache.

the class EventMeshConsumer method sendMessageBack.

public void sendMessageBack(String consumerGroup, final CloudEvent event, final String uniqueId, String bizSeqNo) throws Exception {
    EventMeshProducer producer = eventMeshGrpcServer.getProducerManager().getEventMeshProducer(consumerGroup);
    if (producer == null) {
        logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqNo:{}, uniqueId:{}", consumerGroup, bizSeqNo, uniqueId);
        return;
    }
    final SendMessageContext sendMessageBackContext = new SendMessageContext(bizSeqNo, event, producer, eventMeshGrpcServer);
    producer.send(sendMessageBackContext, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
        }

        @Override
        public void onException(OnExceptionContext context) {
            logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqNo:{}, uniqueId:{}", consumerGroup, bizSeqNo, uniqueId);
        }
    });
}
Also used : EventMeshProducer(org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) SendMessageContext(org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext) SendResult(org.apache.eventmesh.api.SendResult) SendCallback(org.apache.eventmesh.api.SendCallback)

Example 5 with SendMessageContext

use of org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext in project incubator-eventmesh by apache.

the class BatchPublishMessageProcessor method process.

public void process(BatchMessage message, EventEmitter<Response> emitter) throws Exception {
    RequestHeader requestHeader = message.getHeader();
    if (!ServiceUtils.validateHeader(requestHeader)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
        return;
    }
    if (!ServiceUtils.validateBatchMessage(message)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    try {
        doAclCheck(message);
    } catch (Exception e) {
        aclLogger.warn("CLIENT HAS NO PERMISSION,BatchSendMessageProcessor send failed", e);
        ServiceUtils.sendRespAndDone(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.sendRespAndDone(StatusCode.EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR, emitter);
        return;
    }
    String topic = message.getTopic();
    String producerGroup = message.getProducerGroup();
    String protocolType = requestHeader.getProtocolType();
    ProtocolAdaptor<ProtocolTransportObject> grpcCommandProtocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
    List<CloudEvent> cloudEvents = grpcCommandProtocolAdaptor.toBatchCloudEvent(new BatchMessageWrapper(message));
    for (CloudEvent event : cloudEvents) {
        String seqNum = event.getId();
        String uniqueId = event.getExtension(ProtocolKey.UNIQUE_ID).toString();
        ProducerManager producerManager = eventMeshGrpcServer.getProducerManager();
        EventMeshProducer eventMeshProducer = producerManager.getEventMeshProducer(producerGroup);
        SendMessageContext sendMessageContext = new SendMessageContext(seqNum, event, eventMeshProducer, eventMeshGrpcServer);
        long startTime = System.currentTimeMillis();
        eventMeshProducer.send(sendMessageContext, new SendCallback() {

            @Override
            public void onSuccess(SendResult sendResult) {
                long endTime = System.currentTimeMillis();
                logger.info("message|eventMesh2mq|REQ|BatchSend|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, topic, seqNum, uniqueId);
            }

            @Override
            public void onException(OnExceptionContext context) {
                long endTime = System.currentTimeMillis();
                logger.error("message|eventMesh2mq|REQ|BatchSend|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, topic, seqNum, uniqueId, context.getException());
            }
        });
    }
    ServiceUtils.sendRespAndDone(StatusCode.SUCCESS, "batch publish success", emitter);
}
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) BatchMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.BatchMessageWrapper) 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

EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer)5 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext)5 CloudEvent (io.cloudevents.CloudEvent)4 SendCallback (org.apache.eventmesh.api.SendCallback)4 SendResult (org.apache.eventmesh.api.SendResult)4 AclException (org.apache.eventmesh.api.exception.AclException)4 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)4 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)4 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)4 ProducerManager (org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager)4 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)3 RequestReplyCallback (org.apache.eventmesh.api.RequestReplyCallback)1 BatchMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.BatchMessageWrapper)1