Search in sources :

Example 1 with BatchMessageWrapper

use of org.apache.eventmesh.common.protocol.grpc.common.BatchMessageWrapper 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

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