Search in sources :

Example 1 with SimpleMessageWrapper

use of org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper in project incubator-eventmesh by apache.

the class GrpcMessageProtocolResolver method buildSimpleMessage.

public static SimpleMessageWrapper buildSimpleMessage(CloudEvent cloudEvent) {
    String env = cloudEvent.getExtension(ProtocolKey.ENV) == null ? "env" : cloudEvent.getExtension(ProtocolKey.ENV).toString();
    String idc = cloudEvent.getExtension(ProtocolKey.IDC) == null ? "idc" : cloudEvent.getExtension(ProtocolKey.IDC).toString();
    String ip = cloudEvent.getExtension(ProtocolKey.IP) == null ? "127.0.0.1" : cloudEvent.getExtension(ProtocolKey.IP).toString();
    String pid = cloudEvent.getExtension(ProtocolKey.PID) == null ? "123" : cloudEvent.getExtension(ProtocolKey.PID).toString();
    String sys = cloudEvent.getExtension(ProtocolKey.SYS) == null ? "sys123" : cloudEvent.getExtension(ProtocolKey.SYS).toString();
    String userName = cloudEvent.getExtension(ProtocolKey.USERNAME) == null ? "user" : cloudEvent.getExtension(ProtocolKey.USERNAME).toString();
    String passwd = cloudEvent.getExtension(ProtocolKey.PASSWD) == null ? "pass" : cloudEvent.getExtension(ProtocolKey.PASSWD).toString();
    String language = cloudEvent.getExtension(ProtocolKey.LANGUAGE) == null ? "JAVA" : cloudEvent.getExtension(ProtocolKey.LANGUAGE).toString();
    String protocol = cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE) == null ? "protocol" : cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE).toString();
    String protocolDesc = cloudEvent.getExtension(ProtocolKey.PROTOCOL_DESC) == null ? "protocolDesc" : cloudEvent.getExtension(ProtocolKey.PROTOCOL_DESC).toString();
    String protocolVersion = cloudEvent.getExtension(ProtocolKey.PROTOCOL_VERSION) == null ? "1.0" : cloudEvent.getExtension(ProtocolKey.PROTOCOL_VERSION).toString();
    String seqNum = cloudEvent.getExtension(ProtocolKey.SEQ_NUM) == null ? "" : cloudEvent.getExtension(ProtocolKey.SEQ_NUM).toString();
    String uniqueId = cloudEvent.getExtension(ProtocolKey.UNIQUE_ID) == null ? "" : cloudEvent.getExtension(ProtocolKey.UNIQUE_ID).toString();
    String producerGroup = cloudEvent.getExtension(ProtocolKey.PRODUCERGROUP) == null ? "producerGroup" : cloudEvent.getExtension(ProtocolKey.PRODUCERGROUP).toString();
    String ttl = cloudEvent.getExtension(ProtocolKey.TTL) == null ? "3000" : cloudEvent.getExtension(ProtocolKey.TTL).toString();
    RequestHeader header = RequestHeader.newBuilder().setEnv(env).setIdc(idc).setIp(ip).setPid(pid).setSys(sys).setUsername(userName).setPassword(passwd).setLanguage(language).setProtocolType(protocol).setProtocolDesc(protocolDesc).setProtocolVersion(protocolVersion).build();
    String contentType = cloudEvent.getDataContentType();
    EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(contentType);
    SimpleMessage.Builder messageBuilder = SimpleMessage.newBuilder().setHeader(header).setContent(new String(eventFormat.serialize(cloudEvent), StandardCharsets.UTF_8)).setProducerGroup(producerGroup).setSeqNum(seqNum).setUniqueId(uniqueId).setTopic(cloudEvent.getSubject()).setTtl(ttl).putProperties(ProtocolKey.CONTENT_TYPE, contentType);
    for (String key : cloudEvent.getExtensionNames()) {
        messageBuilder.putProperties(key, cloudEvent.getExtension(key).toString());
    }
    SimpleMessage simpleMessage = messageBuilder.build();
    return new SimpleMessageWrapper(simpleMessage);
}
Also used : EventFormat(io.cloudevents.core.format.EventFormat) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)

Example 2 with SimpleMessageWrapper

use of org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper in project incubator-eventmesh by apache.

the class MeshMessageProtocolAdaptor method toCloudEvent.

@Override
public CloudEvent toCloudEvent(ProtocolTransportObject protocol) throws ProtocolHandleException {
    if (protocol instanceof Package) {
        Package tcpPackage = (Package) protocol;
        Header header = tcpPackage.getHeader();
        String bodyJson = (String) tcpPackage.getBody();
        return deserializeTcpProtocol(header, bodyJson);
    } else if (protocol instanceof HttpCommand) {
        org.apache.eventmesh.common.protocol.http.header.Header header = ((HttpCommand) protocol).getHeader();
        Body body = ((HttpCommand) protocol).getBody();
        String requestCode = ((HttpCommand) protocol).getRequestCode();
        return deserializeHttpProtocol(requestCode, header, body);
    } else if (protocol instanceof SimpleMessageWrapper) {
        SimpleMessage message = ((SimpleMessageWrapper) protocol).getMessage();
        return deserializeGrpcProtocol(message);
    } else {
        throw new ProtocolHandleException(String.format("protocol class: %s", protocol.getClass()));
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 3 with SimpleMessageWrapper

use of org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper in project incubator-eventmesh by apache.

the class CloudEventsProtocolAdaptor method toCloudEvent.

@Override
public CloudEvent toCloudEvent(ProtocolTransportObject cloudEvent) throws ProtocolHandleException {
    if (cloudEvent instanceof Package) {
        Package tcpPackage = (Package) cloudEvent;
        Header header = tcpPackage.getHeader();
        String cloudEventJson = tcpPackage.getBody().toString();
        return deserializeTcpProtocol(header, cloudEventJson);
    } else if (cloudEvent instanceof HttpCommand) {
        org.apache.eventmesh.common.protocol.http.header.Header header = ((HttpCommand) cloudEvent).getHeader();
        Body body = ((HttpCommand) cloudEvent).getBody();
        String requestCode = ((HttpCommand) cloudEvent).getRequestCode();
        return deserializeHttpProtocol(requestCode, header, body);
    } else if (cloudEvent instanceof SimpleMessageWrapper) {
        SimpleMessage simpleMessage = ((SimpleMessageWrapper) cloudEvent).getMessage();
        return GrpcMessageProtocolResolver.buildEvent(simpleMessage);
    } else {
        throw new ProtocolHandleException(String.format("protocol class: %s", cloudEvent.getClass()));
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 4 with SimpleMessageWrapper

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

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

Aggregations

SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)9 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)5 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)5 SimpleMessage (org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)4 CloudEvent (io.cloudevents.CloudEvent)3 AclException (org.apache.eventmesh.api.exception.AclException)3 EventMeshProducer (org.apache.eventmesh.runtime.core.protocol.grpc.producer.EventMeshProducer)3 ProducerManager (org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager)3 SendMessageContext (org.apache.eventmesh.runtime.core.protocol.grpc.producer.SendMessageContext)3 SendCallback (org.apache.eventmesh.api.SendCallback)2 SendResult (org.apache.eventmesh.api.SendResult)2 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)2 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)2 Body (org.apache.eventmesh.common.protocol.http.body.Body)2 Header (org.apache.eventmesh.common.protocol.tcp.Header)2 Package (org.apache.eventmesh.common.protocol.tcp.Package)2 ProtocolHandleException (org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)2 EventFormat (io.cloudevents.core.format.EventFormat)1 RequestReplyCallback (org.apache.eventmesh.api.RequestReplyCallback)1