Search in sources :

Example 1 with Subscription

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

the class SubscribeStreamProcessor method doAclCheck.

private void doAclCheck(Subscription subscription) throws AclException {
    RequestHeader header = subscription.getHeader();
    if (eventMeshGrpcServer.getEventMeshGrpcConfiguration().eventMeshServerSecurityEnable) {
        String remoteAdd = header.getIp();
        String user = header.getUsername();
        String pass = header.getPassword();
        String subsystem = header.getSys();
        for (Subscription.SubscriptionItem item : subscription.getSubscriptionItemsList()) {
            Acl.doAclCheckInHttpReceive(remoteAdd, user, pass, subsystem, item.getTopic(), RequestCode.SUBSCRIBE.getRequestCode());
        }
    }
}
Also used : RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) Subscription(org.apache.eventmesh.common.protocol.grpc.protos.Subscription)

Example 2 with Subscription

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

the class EventMeshGrpcConsumer method subscribe.

public void subscribe(List<SubscriptionItem> subscriptionItems) {
    logger.info("Create streaming subscription: " + subscriptionItems);
    if (listener == null) {
        logger.error("Error in subscriber, no Event Listener is registered.");
        return;
    }
    addSubscription(subscriptionItems, SDK_STREAM_URL);
    Subscription subscription = buildSubscription(subscriptionItems, null);
    synchronized (this) {
        if (subStreamHandler == null) {
            subStreamHandler = new SubStreamHandler<>(consumerAsyncClient, clientConfig, listener);
            subStreamHandler.start();
        }
    }
    subStreamHandler.sendSubscription(subscription);
}
Also used : Subscription(org.apache.eventmesh.common.protocol.grpc.protos.Subscription)

Example 3 with Subscription

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

the class EventMeshGrpcConsumer method buildSubscription.

private Subscription buildSubscription(List<SubscriptionItem> subscriptionItems, String url) {
    Subscription.Builder builder = Subscription.newBuilder().setHeader(EventMeshClientUtil.buildHeader(clientConfig, EventMeshCommon.EM_MESSAGE_PROTOCOL_NAME)).setConsumerGroup(clientConfig.getConsumerGroup());
    if (StringUtils.isNotEmpty(url)) {
        builder.setUrl(url);
    }
    for (SubscriptionItem subscriptionItem : subscriptionItems) {
        Subscription.SubscriptionItem.SubscriptionMode mode;
        if (SubscriptionMode.CLUSTERING.equals(subscriptionItem.getMode())) {
            mode = Subscription.SubscriptionItem.SubscriptionMode.CLUSTERING;
        } else if (SubscriptionMode.BROADCASTING.equals(subscriptionItem.getMode())) {
            mode = Subscription.SubscriptionItem.SubscriptionMode.BROADCASTING;
        } else {
            mode = Subscription.SubscriptionItem.SubscriptionMode.UNRECOGNIZED;
        }
        Subscription.SubscriptionItem.SubscriptionType type;
        if (SubscriptionType.ASYNC.equals(subscriptionItem.getType())) {
            type = Subscription.SubscriptionItem.SubscriptionType.ASYNC;
        } else if (SubscriptionType.SYNC.equals(subscriptionItem.getType())) {
            type = Subscription.SubscriptionItem.SubscriptionType.SYNC;
        } else {
            type = Subscription.SubscriptionItem.SubscriptionType.UNRECOGNIZED;
        }
        Subscription.SubscriptionItem item = Subscription.SubscriptionItem.newBuilder().setTopic(subscriptionItem.getTopic()).setMode(mode).setType(type).build();
        builder.addSubscriptionItems(item);
    }
    return builder.build();
}
Also used : SubscriptionItem(org.apache.eventmesh.common.protocol.SubscriptionItem) Subscription(org.apache.eventmesh.common.protocol.grpc.protos.Subscription)

Example 4 with Subscription

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

the class EventMeshGrpcConsumer method unsubscribe.

public Response unsubscribe(List<SubscriptionItem> subscriptionItems, String url) {
    logger.info("Removing subscription: " + subscriptionItems + ", url: " + url);
    removeSubscription(subscriptionItems);
    Subscription subscription = buildSubscription(subscriptionItems, url);
    try {
        Response response = consumerClient.unsubscribe(subscription);
        logger.info("Received response " + response.toString());
        return response;
    } catch (Exception e) {
        logger.error("Error in unsubscribe. error {}", e.getMessage());
        return null;
    }
}
Also used : Response(org.apache.eventmesh.common.protocol.grpc.protos.Response) Subscription(org.apache.eventmesh.common.protocol.grpc.protos.Subscription)

Example 5 with Subscription

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

the class SubStreamHandler method createReceiver.

private StreamObserver<SimpleMessage> createReceiver() {
    return new StreamObserver<SimpleMessage>() {

        @Override
        public void onNext(SimpleMessage message) {
            T msg = EventMeshClientUtil.buildMessage(message, listener.getProtocolType());
            if (msg instanceof Map) {
                logger.info("Received message from Server." + message);
            } else {
                logger.info("Received message from Server.|seq={}|uniqueId={}|", message.getSeqNum(), message.getUniqueId());
                Subscription streamReply = null;
                try {
                    Optional<T> reply = listener.handle(msg);
                    if (reply.isPresent()) {
                        streamReply = buildReplyMessage(message, reply.get());
                    }
                } catch (Throwable t) {
                    logger.error("Error in handling reply message.|seq={}|uniqueId={}|", message.getSeqNum(), message.getUniqueId(), t);
                }
                if (streamReply != null) {
                    logger.info("Sending reply message to Server.|seq={}|uniqueId={}|", streamReply.getReply().getSeqNum(), streamReply.getReply().getUniqueId());
                    senderOnNext(streamReply);
                }
            }
        }

        @Override
        public void onError(Throwable t) {
            logger.error("Received Server side error: " + t.getMessage());
            close();
        }

        @Override
        public void onCompleted() {
            logger.info("Finished receiving messages from server.");
            close();
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) Subscription(org.apache.eventmesh.common.protocol.grpc.protos.Subscription) Map(java.util.Map)

Aggregations

Subscription (org.apache.eventmesh.common.protocol.grpc.protos.Subscription)12 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)5 Date (java.util.Date)3 LinkedList (java.util.LinkedList)3 Response (org.apache.eventmesh.common.protocol.grpc.protos.Response)3 ConsumerManager (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager)3 EventMeshConsumer (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.EventMeshConsumer)3 ConsumerGroupClient (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)3 AclException (org.apache.eventmesh.api.exception.AclException)2 SimpleMessage (org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)2 StreamObserver (io.grpc.stub.StreamObserver)1 Map (java.util.Map)1 SubscriptionItem (org.apache.eventmesh.common.protocol.SubscriptionItem)1