Exemplo n.º 1
0
        private static void StartSubscriberClient(CommandLineOptions opts)
        {
            Log.Info("Starting test client as Subscriber");
            var ipAddress = Dns.GetHostEntry(opts.Hostname).AddressList[0];
            SubscriberClient subscriber = new SubscriberClient(ipAddress, opts.Port);

            subscriber.Connect();
            subscriber.Subscribe("test-channel");
            subscriber.MessageReceived.Subscribe((s) => Log.Info($"Received Message: {s}"));

            Log.Info("Waiting");
            while (true)
            {
                var newLine      = Console.ReadLine();
                var newLineSplit = newLine.Split(' ');

                if (newLineSplit[0] == "dispose")
                {
                    subscriber.Dispose();
                    break;
                }
                else if (newLineSplit[0] == "subscribe" && newLineSplit.Length > 1)
                {
                    subscriber.Subscribe(newLineSplit[1]);
                }
                else if (newLineSplit[0] == "unsubscribe" && newLineSplit.Length > 1)
                {
                    subscriber.Unsubscribe(newLineSplit[1]);
                }
            }
            Log.Info("SubscriberClient test program complete");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        private async Task <object> PullMessagesAsync(string projectId, string subscriptionId, bool acknowledge)
        {
            var subscriptionName = new SubscriptionName(projectId, subscriptionId);
            var subscription     = await SubscriberClient.CreateAsync(subscriptionName);

            await subscription.StartAsync(
                async (PubsubMessage message, CancellationToken cancel) =>
            {
                string json = Encoding.UTF8.GetString(message.Data.ToArray());

                await Console.Out.WriteLineAsync($"Message {message.MessageId}: {json}");

                // Insert or update author
                IncreasingCP increasingCP = JsonConvert.DeserializeObject <IncreasingCP>(json);
                _userRepository.IncreaseContributionPoint(increasingCP.UserId, increasingCP.Point);

                return(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
            });

            //// Run for 3 seconds.
            //await Task.Delay(3000);
            //await subscription.StopAsync(CancellationToken.None);

            // [END pubsub_subscriber_async_pull]
            return(0);
        }
        public async Task <int> Process()
        {
            var name   = new SubscriptionName(Options.ProjectId, Options.SubscriptionId);
            var client = await SubscriberClient.CreateAsync(name, GetSettings(), new SubscriberClient.Settings {
            });

            var messageCount = 0;

            var startTask = client.StartAsync(async(message, _) =>
            {
                var notification = message.ToNotification();

                Interlocked.Increment(ref messageCount);

                if (!notification.IsTest)
                {
                    await ProccessNotification(notification);
                }

                return(SubscriberClient.Reply.Ack);
            });

            await Task.Delay(5.Seconds());

            await client.StopAsync(CancellationToken.None);

            await startTask;

            Logger.Debug($"{messageCount} queue messages are processed.");

            return(messageCount);
        }
Exemplo n.º 4
0
        private String GetSubscriberStatus(String mobileNumber)
        {
            SubscriberClient subscriberService = new SubscriberClient("SubscriberWSServiceHttpEndpoint");
            string           securityToken     = sbtLogin();
            string           orgCode           = "wt63419";
            List <string>    phone             = new List <string>();

            phone.Add(mobileNumber);
            WSSubscribersStatusResponse wSSubscribersStatusResponse;

            // Moses Newman 07/12/2019 catch EndpointNotFound errors.
            try
            {
                wSSubscribersStatusResponse = subscriberService.GetSubscribersStatus(securityToken, orgCode, phone.ToArray());
            }
            catch (System.ServiceModel.EndpointNotFoundException e)
            {
                return(e.Message);
            }
            if (!wSSubscribersStatusResponse.Result)
            {
                return(wSSubscribersStatusResponse.Message);
            }
            else
            {
                return(wSSubscribersStatusResponse.Response[0].Status);
            }
        }
        public static Task Subscribe(string subscriptionId, IServiceScopeFactory scopeFactory)
        {
            var subscriptionName = SubscriptionName.FromProjectSubscription("kwetter-308618", subscriptionId);
            var subscription     = SubscriberClient.CreateAsync(subscriptionName).Result;


            return(subscription.StartAsync((message, _) =>
            {
                var mediator = scopeFactory.CreateScope().ServiceProvider.GetRequiredService <IMediator>();
                var json = Encoding.UTF8.GetString(message.Data.ToArray());

                switch (subscriptionId)
                {
                case "profileservice--user-created":
                    mediator.Send(JsonConvert.DeserializeObject <UserCreatedEvent>(json), _);
                    break;

                case "profileservice--user-deleted":
                    mediator.Send(JsonConvert.DeserializeObject <UserDeletedEvent>(json), _);
                    break;
                }

                return Task.FromResult(SubscriberClient.Reply.Ack);
            }));
        }
        public async Task StartSubscribeMessageHandler()
        {
            _logger.LogInformation("Message Listener starting for {eventSubscriptionName}", _eventSubscriptionName.SubscriptionId);

            // Pull messages from the subscription using SimpleSubscriber.
            _messageSubscriber = await SubscriberClient.CreateAsync(_messageSubscriptionName);

            await _messageSubscriber.StartAsync(async (msg, cancellationToken) =>
            {
                try
                {
                    _logger.LogDebug("Message receipt from {messageSubscriptionName}", _messageSubscriptionName.SubscriptionId);
                    string messageString        = msg.Data.ToStringUtf8();
                    JObject json                = JsonConvert.DeserializeObject <JObject>(messageString);
                    OutputMessage outputMessage = _limeConverter.ConvertToOutputMessage(json);
                    _logger.LogInformation("Message receipt {id}", outputMessage.id);
                    await _messageRepository.SaveMessage(outputMessage);
                    // Return Reply.Ack to indicate this message has been handled.
                    return(SubscriberClient.Reply.Ack);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.ToString() + " " + msg.Data.ToStringUtf8());
                    return(SubscriberClient.Reply.Nack);
                }
            });
        }
Exemplo n.º 7
0
        public async Task <object> CreateSubscriber(TopicName topic)
        {
            SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create();

            SubscriptionName subscriptionName = new SubscriptionName(_projectId, "SubscriptionJ");

            var subscriptionCreated = subscriber.CreateSubscription(subscriptionName, topic, pushConfig: null, ackDeadlineSeconds: 60);

            SubscriberClient subscriberClient = await SubscriberClient.CreateAsync(subscriptionName);

            try
            {
                _ = subscriberClient.StartAsync(
                    async(PubsubMessage message, CancellationToken cancel) =>
                {
                    string text = Encoding.UTF8.GetString(message.Data.ToArray());

                    await Console.Out.WriteLineAsync($"Consumer {message.MessageId} => Message:{text}");

                    Console.WriteLine(message);

                    return(await Task.FromResult(SubscriberClient.Reply.Ack));
                });

                await Task.Delay(3000);

                await subscriberClient.StopAsync(CancellationToken.None);
            }
            catch (RpcException exception)
            {
            }

            return(0);
        }
Exemplo n.º 8
0
        public static async Task <object> PullMessageAsync(string projectId, string subscriptionId, bool acknowledge)
        {
            SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);
            SubscriberClient subscriber       = await SubscriberClient.CreateAsync(subscriptionName);

            FireStoreHandler handler     = new FireStoreHandler();
            HttpHandler      httpHandler = new HttpHandler();
            UTF8Encoding     encoding    = new UTF8Encoding();
            await subscriber.StartAsync(
                async (PubsubMessage message, CancellationToken cancel) =>
            {
                Byte[] encodedBytes  = message.Data.ToArray();
                var jsonString       = Encoding.UTF8.GetString(message.Data.ToArray());
                string decodedString = encoding.GetString(encodedBytes);
                await handler.PersistAsync(message.MessageId, decodedString);
                httpHandler.SetTopOfTheDay(decodedString, cancel);
                return(acknowledge ? SubscriberClient.Reply.Ack
                        : SubscriberClient.Reply.Nack);
            });

            await Task.Delay(3000);

            await subscriber.StopAsync(CancellationToken.None);

            return(0);
        }
        public async Task ListSubscriptionsAsync()
        {
            // Snippet: ListSubscriptionsAsync(string,string,int?,CallSettings)
            // Create client
            SubscriberClient subscriberClient = SubscriberClient.Create();
            // Initialize request argument(s)
            string formattedProject = SubscriberClient.FormatProjectName("[PROJECT]");
            // Make the request
            IPagedAsyncEnumerable <ListSubscriptionsResponse, Subscription> response =
                subscriberClient.ListSubscriptionsAsync(formattedProject);

            // Iterate over all response items, lazily performing RPCs as required
            await response.ForEachAsync((Subscription item) =>
            {
                // Do something with each item
                Console.WriteLine(item);
            });

            // Or iterate over fixed-sized pages, lazily performing RPCs as required
            int pageSize = 10;
            IAsyncEnumerable <FixedSizePage <Subscription> > fixedSizePages = response.AsPages().WithFixedSize(pageSize);
            await fixedSizePages.ForEachAsync((FixedSizePage <Subscription> page) =>
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (Subscription item in page)
                {
                    Console.WriteLine(item);
                }
            });

            // End snippet
        }
Exemplo n.º 10
0
        private async Task <object> PullMessagesAsync(string projectId, string subscriptionId, bool acknowledge)
        {
            var subscriptionName = new SubscriptionName(projectId, subscriptionId);
            var subscription     = await SubscriberClient.CreateAsync(subscriptionName);

            await subscription.StartAsync(
                async (PubsubMessage message, CancellationToken cancel) =>
            {
                string json = Encoding.UTF8.GetString(message.Data.ToArray());

                await Console.Out.WriteLineAsync($"Message {message.MessageId}: {json}");

                // Insert or update author
                User user  = JsonConvert.DeserializeObject <User>(json);
                var result = _userRepository.InsertOrUpdate(user);

                if (result == null)
                {
                    return(SubscriberClient.Reply.Nack);
                }
                return(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
            });

            //// Run for 3 seconds.
            //await Task.Delay(3000);
            //await subscription.StopAsync(CancellationToken.None);

            return(0);
        }
Exemplo n.º 11
0
        static async Task Main(string[] args)
        {
            var projectId = "376558168506";

            AuthImplicit(projectId);

            SubscriberServiceApiClient subsClient = SubscriberServiceApiClient.Create();
            SubscriptionName           subsName   = new SubscriptionName(projectId, "westworldSubscription");
            SubscriberClient           einstein   = await SubscriberClient.CreateAsync(subsName);

            bool acknowledge = false;
            await einstein.StartAsync(
                async (PubsubMessage pubSubMessage, CancellationToken cancel) =>
            {
                string msg = Encoding.UTF8.GetString(pubSubMessage.Data.ToArray());


                await Console.Out.WriteLineAsync($"{pubSubMessage.MessageId}: {msg}");

                if (msg.Length > 1)
                {
                    acknowledge = true;
                }
                return(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
            });

            Thread.Sleep(5000);
            einstein.StopAsync(CancellationToken.None).Wait();
        }
Exemplo n.º 12
0
        private async Task <object> PullMessagesAsync(string projectId, string subscriptionId, bool acknowledge)
        {
            var subscriptionName = new SubscriptionName(projectId, subscriptionId);
            var subscription     = await SubscriberClient.CreateAsync(subscriptionName);

            await subscription.StartAsync(
                async (PubsubMessage message, CancellationToken cancel) =>
            {
                string json = Encoding.UTF8.GetString(message.Data.ToArray());

                await Console.Out.WriteLineAsync($"Message {message.MessageId}: {json}");

                // Handle received message.
                Email email = JsonConvert.DeserializeObject <Email>(json);

                // Send mail
                _emailService.SendEmailAsync(email);

                return(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);
            });

            //// Run for 3 seconds.
            //await Task.Delay(3000);
            //await subscription.StopAsync(CancellationToken.None);

            // [END pubsub_subscriber_async_pull]
            return(0);
        }
        /// <summary>
        /// Set up the required receive pipeline, for the given message type, and return a reactive <see cref="T:System.IObservable`1" /> that you can subscribe to.
        /// </summary>
        /// <typeparam name="T">The type of the message returned by the observable.</typeparam>
        /// <param name="batchSize">The size of the batch when reading from a queue.</param>
        /// <returns>The typed <see cref="T:System.IObservable`1" /> that you subscribed to.</returns>
        /// <exception cref="InvalidOperationException">ReceiverConfig must be set to read messages.</exception>
        public IObservable <T> StartReceive <T>(int batchSize = 10) where T : class
        {
            // Ensure config is setup.
            if (Config.ReceiverConfig == null)
            {
                throw new InvalidOperationException("Receiver configuration must be set");
            }

            CreateIfNotExists();

            _receiverClient ??= SubscriberClient.CreateAsync(new SubscriptionName(Config.ProjectId, Config.ReceiverConfig.ReadFromErrorEntity
                    ? Config.ReceiverConfig.EntityDeadLetterName
                    : Config.ReceiverConfig.EntitySubscriptionName),
                                                             new SubscriberClient.ClientCreationSettings(credentials: GetCredentials())).GetAwaiter().GetResult();

            IObserver <T> messageIn = _messagesIn.AsObserver();

            _receiverClient.StartAsync((message, cancel) =>
            {
                if (!cancel.IsCancellationRequested)
                {
                    var typed = GetTypedMessageContent <T>(message);
                    messageIn.OnNext(typed);
                }

                return(Task.FromResult(SubscriberClient.Reply.Ack));
            });

            return(_messagesIn.OfType <T>());
        }
 private async Task <SubscriberClient> CreateSubscriber(SubscriptionName subscriptionName)
 {
     // Pull messages from the subscription using SubscriberClient.
     return(string.IsNullOrEmpty(_pubsubEmulatorHost)
         ? await SubscriberClient.CreateAsync(subscriptionName)
         : await SubscriberClient.CreateAsync(subscriptionName, clientCreationSettings : new SubscriberClient.ClientCreationSettings(credentials: ChannelCredentials.Insecure, serviceEndpoint: _pubsubEmulatorHost)));
 }
Exemplo n.º 15
0
        public async Task StartSubscriberAsync(string subscription, Func <PubsubMessage, CancellationToken, Task <SubscriberClient.Reply> > handlerAsync)
        {
            SubscriberClient client = await TryGetSubscriberClient(subscription);

            _ = client.StartAsync(handlerAsync);
            m_logger.Info($"Started subscriber for subscription {subscription}.");
        }
        /// <summary>
        /// Initializes the pull worker.
        /// </summary>
        /// <returns></returns>
        public bool InitializePullWorker()
        {
            bool bStatus = false;

            _redis   = ConnectionMultiplexer.Connect(DefaultRedisServerAddress);
            _redisDB = _redis.GetDatabase();

            CurrentTopicName = new TopicName(DefaultProjectId, DefaultTopicName);
            _subscriber      = SubscriberClient.Create();
            //String  subscriptionId = "projects/pushnotificationpoc-19baf/subscriptions/myTestReader";

            _publisher        = PublisherClient.Create();
            _subscriptionName = new SubscriptionName(DefaultProjectId, DefaultSubscriptionId);
            String t = _subscriptionName.ToString();

            try
            {
                _subscriber.CreateSubscription(_subscriptionName, CurrentTopicName, pushConfig: null, ackDeadlineSeconds: 60);
            }
            catch (RpcException e)
            {
                if (e.Status.StatusCode == StatusCode.AlreadyExists)
                {
                    // Already exists.  That's fine.
                    _bSubscriptionAllreadyExists = true;
                }
            }

            return(bStatus);
        }
Exemplo n.º 17
0
        public async Task <Task> StartAsync()
        {
            var builder        = new SubscriberServiceApiClientBuilder();
            var subscriber     = builder.Build();
            var projectId      = "second-impact-574";
            var subscriptionId = "test-subscription";
            var topicId        = "sync_client_commands";

            TopicName        topicName        = new TopicName(projectId, topicId);
            SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);

            try
            {
                Subscription subscription = subscriber.CreateSubscription(
                    subscriptionName, topicName, pushConfig: null,
                    ackDeadlineSeconds: 60);
            }
            catch (RpcException e)
                when(e.Status.StatusCode == StatusCode.AlreadyExists)
                {
                    Console.Out.WriteLine("subscription already exists...");
                }

            client = await SubscriberClient.CreateAsync(subscriptionName);

            return(client.StartAsync(async(message, token) =>
            {
                await Task.Delay(5 * 1000);
                return Reply.Ack;
            }));
        }
    public void Pull()
    {
        // <Pull>
        SubscriberClient client = SubscriberClient.Create();

        // Alternative: use an existing subscription resource name:
        // projects/{PROJECT_ID}/subscriptions/{SUBSCRIPTION_ID}
        string subscriptionName = SubscriberClient.GetSubscriptionName("{PROJECT_ID}", "{SUBSCRIPTION_ID}");

        PullResponse pullResponse = client.Pull(subscriptionName, returnImmediately: false, maxMessages: 100);

        foreach (ReceivedMessage message in pullResponse.ReceivedMessages)
        {
            // Messages can contain any data. We'll assume that we know this
            // topic publishes UTF-8-encoded text.
            Console.WriteLine($"Message text: {message.Message.Data.ToStringUtf8()}");
        }

        // Acknowledge the messages after pulling them, so we don't pull them
        // a second time later. The ackDeadlineSeconds parameter specified when
        // the subscription is created determines how quickly you need to acknowledge
        // successfully-pulled messages before they will be redelivered.
        var ackIds = pullResponse.ReceivedMessages.Select(rm => rm.AckId);

        client.Acknowledge(subscriptionName, ackIds);
        // </Pull>
    }
Exemplo n.º 19
0
    public async Task <int> PullMessagesWithFlowControlAsync(string projectId, string subscriptionId, bool acknowledge)
    {
        SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);
        int messageCount            = 0;
        SubscriberClient subscriber = await SubscriberClient.CreateAsync(subscriptionName,
                                                                         settings : new SubscriberClient.Settings()
        {
            AckExtensionWindow  = TimeSpan.FromSeconds(4),
            AckDeadline         = TimeSpan.FromSeconds(10),
            FlowControlSettings = new FlowControlSettings(maxOutstandingElementCount: 100, maxOutstandingByteCount: 10240)
        });

        // SubscriberClient runs your message handle function on multiple
        // threads to maximize throughput.
        Task startTask = subscriber.StartAsync((PubsubMessage message, CancellationToken cancel) =>
        {
            string text = System.Text.Encoding.UTF8.GetString(message.Data.ToArray());
            Console.WriteLine($"Message {message.MessageId}: {text}");
            Interlocked.Increment(ref messageCount);
            return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack));
        });
        // Run for 5 seconds.
        await Task.Delay(5000);

        await subscriber.StopAsync(CancellationToken.None);

        // Lets make sure that the start task finished successfully after the call to stop.
        await startTask;

        return(messageCount);
    }
Exemplo n.º 20
0
 public HomeController(IOptions<PubsubOptions> options,
     PublisherClient publisher, SubscriberClient subscriber)
 {
     _options = options.Value;
     _publisher = publisher;
     _subscriber = subscriber;
 }
        public void SubscribeArray <T, TH>(Func <TH> handler, string topicName, int maxConcurrent = 1)
            where T : Event
            where TH : IArrayEventHandler <T>
        {
            new Thread(async() => {
                try {
                    string subscription = _busSettings.SubscriptionName.ToLower() + "-" + topicName.ToLower();
                    SubscriberClient subscriber;
                    CreateSubscription(topicName, subscription);
                    // Pull messages from the subscription using SimpleSubscriber.
                    SubscriptionName subscriptionName = new SubscriptionName(_busSettings.ProjectId, subscription);
                    subscriber = await SubscriberClient.CreateAsync(
                        subscriptionName,
                        null,
                        new SubscriberClient.Settings {
                        FlowControlSettings = new Google.Api.Gax.FlowControlSettings(maxConcurrent, null)
                    }
                        );

                    await subscriber.StartAsync(async(PubsubMessage message, CancellationToken token) => {
                        T[] events;
                        if ((_busSettings.Token != null && _busSettings.Token != "") && (!message.Attributes.ContainsKey("token") || message.Attributes["token"] != _busSettings.Token))
                        {
                            return(SubscriberClient.Reply.Ack);
                        }
                        try{
                            events = JsonConvert.DeserializeObject <T[]>(message.Data.ToStringUtf8());
                        }catch (JsonException ex) {
                            Console.WriteLine(ex.Message);
                            return(SubscriberClient.Reply.Ack);
                        }
                        for (int i = 0; i < events.Length; i++)
                        {
                            events[i].EventId   = message.MessageId;
                            events[i].Timestamp = message.PublishTime.Seconds * 1000;
                        }

                        var invoke         = handler.DynamicInvoke();
                        var concreteType   = typeof(IArrayEventHandler <>).MakeGenericType(typeof(T));
                        EventResult result = await(Task <EventResult>) concreteType.GetMethod("Handle").Invoke(invoke, new object[] { events, null });
                        if (result == EventResult.Success)
                        {
                            return(SubscriberClient.Reply.Ack);
                        }
                        else
                        {
                            return(SubscriberClient.Reply.Nack);
                        }
                    });
                    new Thread(() => SubscribeArray <T, TH>(handler, topicName, maxConcurrent)).Start();
                }
                // Restart when connection fail
                catch (RpcException ex)
                {
                    Console.WriteLine(ex.Message);
                    new Thread(() => SubscribeArray <T, TH>(handler, topicName, maxConcurrent)).Start();
                    return;
                }
            }).Start();
        }
    public async Task <List <PubsubMessage> > PullMessagesWithCustomAttributesAsync(string projectId, string subscriptionId, bool acknowledge)
    {
        SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);

        SubscriberClient subscriber = await SubscriberClient.CreateAsync(subscriptionName);

        var  messages  = new List <PubsubMessage>();
        Task startTask = subscriber.StartAsync((PubsubMessage message, CancellationToken cancel) =>
        {
            messages.Add(message);
            string text = System.Text.Encoding.UTF8.GetString(message.Data.ToArray());
            Console.WriteLine($"Message {message.MessageId}: {text}");
            if (message.Attributes != null)
            {
                foreach (var attribute in message.Attributes)
                {
                    Console.WriteLine($"{attribute.Key} = {attribute.Value}");
                }
            }
            return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack));
        });
        // Run for 7 seconds.
        await Task.Delay(7000);

        await subscriber.StopAsync(CancellationToken.None);

        // Lets make sure that the start task finished successfully after the call to stop.
        await startTask;

        return(messages);
    }
Exemplo n.º 23
0
        public async Task SubscribeAsync(string projectId, string subscriptionId, Action <object, CancellationToken> doWork)
        {
            var subscriptionName = new SubscriptionName(projectId, subscriptionId);

            var subscription = await SubscriberClient.CreateAsync(subscriptionName,
                                                                  new SubscriberClient.ClientCreationSettings(credentials: this.channelCredentials)
                                                                  );

            //Task startTask = subscription.StartAsync(ProcMessage);
            await subscription.StartAsync((pubSubMessage, cancellationToken) =>
                                          OnReceivePubsubMessage(pubSubMessage, cancellationToken, doWork)
                                          );


            //Task startTask = subscription.StartAsync((pubSubMessage, cancellationToken) =>
            //{
            //    bool acknowledge = true;
            //    string text = System.Text.Encoding.UTF8.GetString(pubSubMessage.Data.ToArray());
            //    Console.WriteLine($"Message {pubSubMessage.MessageId}: {text}");
            //    Interlocked.Increment(ref messageCount);
            //    return Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack);

            //});

            //await subscription.StopAsync(CancellationToken.None);
            //// Lets make sure that the start task finished successfully after the call to stop.
            //await startTask;
            //return messageCount;
        }
Exemplo n.º 24
0
        private static async Task Subscribe(SubscriberClient client, string projectId, string topicId)
        {
            string topicName        = TopicTemplate.Expand(projectId, topicId);
            var    subscriptionName = SubscriptionTemplate.Expand(projectId, "sub-" + Guid.NewGuid());
            var    subscription     = await client.CreateSubscriptionAsync(subscriptionName, topicName, null, 10);

            try
            {
                bool keepGoing = true;
                while (keepGoing)
                {
                    var pullResponse = await client.PullAsync(subscription.Name, false, 10);

                    await client.AcknowledgeAsync(subscription.Name, pullResponse.ReceivedMessages.Select(resp => resp.AckId));

                    foreach (var message in pullResponse.ReceivedMessages)
                    {
                        var text = message.Message.Data.ToStringUtf8();
                        if (text == "QUIT")
                        {
                            keepGoing = false; // Stop *after* the end of this set of messages
                        }
                        Console.WriteLine(text);
                    }
                }
            }
            finally
            {
                await client.DeleteSubscriptionAsync(subscription.Name);
            }
        }
Exemplo n.º 25
0
    public async Task <int> PullMessagesAsyncWithDeliveryAttempts(string projectId, string subscriptionId, bool acknowledge)
    {
        // This is an existing subscription with a dead letter policy.
        SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);

        SubscriberClient subscriber = await SubscriberClient.CreateAsync(subscriptionName);

        int  deliveryAttempt = 0;
        Task startTask       = subscriber.StartAsync((PubsubMessage message, CancellationToken cancel) =>
        {
            string text = Encoding.UTF8.GetString(message.Data.ToArray());
            System.Console.WriteLine($"Delivery Attempt: {message.GetDeliveryAttempt()}");
            if (message.GetDeliveryAttempt() != null)
            {
                deliveryAttempt = message.GetDeliveryAttempt().Value;
            }
            return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack));
        });
        // Run for 7 seconds.
        await Task.Delay(7000);

        await subscriber.StopAsync(CancellationToken.None);

        // Lets make sure that the start task finished successfully after the call to stop.
        await startTask;

        return(deliveryAttempt);
    }
        static async Task <object> PullMessagesAsync(string projectId,
                                                     string subscriptionId, bool acknowledge)
        {
            // [START pubsub_subscriber_async_pull]
            // [START scc_receive_notifications]
            SubscriptionName subscriptionName = new SubscriptionName(projectId,
                                                                     subscriptionId);
            SubscriberClient subscriber = await SubscriberClient.CreateAsync(
                subscriptionName);

            // SubscriberClient runs your message handle function on multiple
            // threads to maximize throughput.
            Task startTask = subscriber.StartAsync(
                async(PubsubMessage message, CancellationToken cancel) =>
            {
                string text =
                    Encoding.UTF8.GetString(message.Data.ToArray());
                await Console.Out.WriteLineAsync(
                    $"Message {message.MessageId}: {text}");
                return(acknowledge ? SubscriberClient.Reply.Ack
                        : SubscriberClient.Reply.Nack);
            });
            // Run for 3 seconds.
            await Task.Delay(3000);

            await subscriber.StopAsync(CancellationToken.None);

            // [END pubsub_subscriber_async_pull]
            // [END scc_receive_notifications]
            return(0);
        }
Exemplo n.º 27
0
    public async Task <int> PullMessagesAsync(string projectId, string subscriptionId, bool acknowledge)
    {
        SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);
        SubscriberClient subscriber       = await SubscriberClient.CreateAsync(subscriptionName);

        // SubscriberClient runs your message handle function on multiple
        // threads to maximize throughput.
        int  messageCount = 0;
        Task startTask    = subscriber.StartAsync((PubsubMessage message, CancellationToken cancel) =>
        {
            string text = System.Text.Encoding.UTF8.GetString(message.Data.ToArray());
            Console.WriteLine($"Message {message.MessageId}: {text}");
            Interlocked.Increment(ref messageCount);
            return(Task.FromResult(acknowledge ? SubscriberClient.Reply.Ack : SubscriberClient.Reply.Nack));
        });
        // Run for 5 seconds.
        await Task.Delay(5000);

        await subscriber.StopAsync(CancellationToken.None);

        // Lets make sure that the start task finished successfully after the call to stop.
        await startTask;

        return(messageCount);
    }
Exemplo n.º 28
0
        static object PullMessagesAsync(string projectId,
                                        string subscriptionId, bool acknowledge)
        {
            // [START pubsub_subscriber_async_pull]
            SubscriptionName subscriptionName = new SubscriptionName(projectId,
                                                                     subscriptionId);
            SubscriberServiceApiClient subscriberClient = SubscriberServiceApiClient.Create();
            SubscriberClient           subscriber       = SubscriberClient.Create(
                subscriptionName, new[] { subscriberClient });

            // SubscriberClient runs your message handle function on multiple
            // threads to maximize throughput.
            subscriber.StartAsync(
                async(PubsubMessage message, CancellationToken cancel) =>
            {
                string text =
                    Encoding.UTF8.GetString(message.Data.ToArray());
                await Console.Out.WriteLineAsync(
                    $"Message {message.MessageId}: {text}");
                return(acknowledge ? SubscriberClient.Reply.Ack
                        : SubscriberClient.Reply.Nack);
            });
            // Run for 3 seconds.
            Thread.Sleep(3000);
            subscriber.StopAsync(CancellationToken.None).Wait();
            // [END pubsub_subscriber_async_pull]
            return(0);
        }
Exemplo n.º 29
0
        //public Subscription CreatePushSubscription(string projectId, string topicId, string subscriptionId, string pushEndpoint)
        public async Task CreatePushSubscriptionAsync(string projectId, string topicId, string subscriptionId, string pushEndpoint)
        {
            var subscriptionName = new SubscriptionName(projectId, subscriptionId);

            // Pull messages from the subscription using SubscriberClient.
            SubscriberClient subscriber = await SubscriberClient.CreateAsync(subscriptionName);

            List <PubsubMessage> receivedMessages = new List <PubsubMessage>();
            // Start the subscriber listening for messages.
            await subscriber.StartAsync((msg, cancellationToken) =>
            {
                receivedMessages.Add(msg);
                Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
                // Stop this subscriber after one message is received.
                // This is non-blocking, and the returned Task may be awaited.
                subscriber.StopAsync(TimeSpan.FromSeconds(15));
                // Return Reply.Ack to indicate this message has been handled.
                return(Task.FromResult(SubscriberClient.Reply.Ack));
            });

            //SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create();
            //TopicName topicName = TopicName.FromProjectTopic(projectId, topicId);
            //SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId);

            //PushConfig pushConfig = new PushConfig { PushEndpoint = pushEndpoint };

            //// The approximate amount of time in seconds (on a best-effort basis) Pub/Sub waits for the
            //// subscriber to acknowledge receipt before resending the message.
            //var ackDeadlineSeconds = 60;
            //var subscription = subscriber.CreateSubscription(subscriptionName, topicName, pushConfig, ackDeadlineSeconds);
            //return subscription;
        }
Exemplo n.º 30
0
        public async Task InitializeAsync(CancellationToken ct)
        {
            var subcriptionName = new SubscriptionName(options.ProjectId, $"{options.Prefix}{topicId}");

            subscriberClient = await SubscriberClient.CreateAsync(subcriptionName);

            subscriberClient.StartAsync(async(pubSubMessage, subscriberToken) =>
            {
                try
                {
                    var message = serializer.Deserialize <T>(pubSubMessage.Data.Span);

                    await consumer.HandleAsync(message, subscriberToken);
                }
                catch (JsonException ex)
                {
                    log.LogError(ex, w => w
                                 .WriteProperty("action", "ConsumeMessage")
                                 .WriteProperty("system", "GooglePubSub")
                                 .WriteProperty("status", "Failed")
                                 .WriteProperty("reason", "Failed to deserialize from JSON."));
                }
                catch (Exception ex)
                {
                    log.LogError(ex, w => w
                                 .WriteProperty("action", "ConsumeMessage")
                                 .WriteProperty("system", "GooglePubSub")
                                 .WriteProperty("status", "Failed"));
                }

                return(SubscriberClient.Reply.Ack);
            }).Forget();
        }
Exemplo n.º 31
0
 public void SubscriberRemoteGetHeartBeat_OK()
 {
     ISubscriberProxy subscriberProxy = new SubscriberClient();
     subscriberProxy.InitiateUsingPort(8767);
     var heartBeatResponse = subscriberProxy.GetHeartbeat();
     Assert.IsNotNull(heartBeatResponse);
     Assert.True(heartBeatResponse.Status);
 }
Exemplo n.º 32
0
 public void SubscriberUpAndAccessible()
 {
     ISubscriberProxy subscriberProxy = new SubscriberClient();
     subscriberProxy.InitiateUsingPort(8765);
     Assert.True(subscriberProxy.TryOpenChannel(new TryOpenChannelRequest { SourceOfDataPort = 8765 }).Status);
     Assert.False(
         subscriberProxy.ApplyChangePushItem(new ApplyChangePushItemRequest
                                                 {
                                                     ChangePushItem = new ChangePushItem(),
                                                     SourceRootName = string.Empty
                                                 }).Success);
 }
        public BookDetailLookup(string projectId, Options options = null, ISimpleLogger logger = null)
        {
            options = options ?? new Options();

            _logger = logger ?? new DebugLogger();
            // [START pubsubpaths]
            _topicName = $"projects/{projectId}/topics/{options.TopicId}";
            _subscriptionName = $"projects/{projectId}/subscriptions/{options.SubscriptionId}";
            // [END pubsubpaths]
            _pub = PublisherClient.Create();
            _sub = SubscriberClient.Create();
        }
 public void AcknowledgeTopicMessage(
     string subscriptionId,
     SubscriberClient subscriber,
     PullResponse response)
 {
     string subscriptionName =
         SubscriberClient.FormatSubscriptionName(_projectId,
         subscriptionId);
     // [START pull_messages]
     subscriber.Acknowledge(subscriptionName,
         response.ReceivedMessages.Select(m => m.AckId));
     // [END pull_messages]
 }
 // [END retry]
 public PubsubTest()
 {
     // [START create_publisher_client]
     // By default, the Google.Pubsub.V1 library client will authenticate
     // using the service account file (created in the Google Developers
     // Console) specified by the GOOGLE_APPLICATION_CREDENTIALS
     // environment variable and it will use the project specified by
     // the GOOGLE_PROJECT_ID environment variable. If you are running on
     // a Google Compute Engine VM, authentication is completely
     // automatic.
     _projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
     // [END create_publisher_client]
     _publisher = CreatePublisherClient();
     _subscriber = CreateSubscriberClient();
 }
Exemplo n.º 36
0
        static void Main(string[] args)
        {
            //IPublisherProxy publisherProxy = new PublisherClient();
            //publisherProxy.InitiateUsingEndpoint("PublisherSciendoLaptop");
            //publisherProxy.PublishPackage();

            ISourceOfDataProxy sourceOfDataProxy = new SourceOfDataClient();
            sourceOfDataProxy.InitiateUsingEndpoint("SourceOfDataSciendoLaptop");
            //var data = sourceOfDataProxy.GetData(new RemoteRequest {FileName = @"C:\MySynch.Source.Test.Root\img001.jpg"});

            ISubscriberProxy subscriberProxy = new SubscriberClient();
            subscriberProxy.InitiateUsingEndpoint("SubscriberSciendoLaptop");
            CopyStrategy copyStrategy= new CopyStrategy();
            copyStrategy.Initialize(sourceOfDataProxy);
            subscriberProxy.ApplyChangePackage(new ChangePushPackage(), "SourceOfDataSciendoLaptop");
        }
Exemplo n.º 37
0
 public void SubscriberApplyChanges_Ok()
 {
     if (File.Exists(@"C:\MySynch.Dest.Test.Root\File1.xml"))
         File.Delete(@"C:\MySynch.Dest.Test.Root\File1.xml");
     ISubscriberProxy subscriberProxy = new SubscriberClient();
     subscriberProxy.InitiateUsingPort(8765);
     ApplyChangePushItemRequest applyChangePushItemRequest = new ApplyChangePushItemRequest
     {
         SourceRootName = @"C:\MySynch.Source.Test.Root\",
         ChangePushItem =
                                                              new ChangePushItem
                                                                  {
                                                                      AbsolutePath =
                                                                          @"C:\MySynch.Source.Test.Root\File1.xml",
                                                                      OperationType = OperationType.Insert
                                                                  }
     };
     Assert.True(subscriberProxy.TryOpenChannel(new TryOpenChannelRequest { SourceOfDataPort = 8765 }).Status);
     subscriberProxy.ApplyChangePushItem(applyChangePushItemRequest);
     Assert.True(File.Exists(@"C:\MySynch.Dest.Test.Root\File1.xml"));
 }
        public GoogleCloudPubSubSinkTests(GoogleCloudPubsubFixture fixture)
        {
            this._fixture = fixture;

            //---

            this._projectId = this._fixture.ProjectId;
            this._topicId = this._fixture.CreateTopicId();
            this._subscriptionId = this._fixture.CreateSubscriptionId(this._topicId);
            this._subscriptionIdFull = this._fixture.GetProjectSubsFull(this._subscriptionId);

            //---

            // It is necessary to specify a correct Deadline time (in UTC). If not then it is thrown
            // the following error: Status(StatusCode=DeadlineExceeded, Detail="Deadline Exceeded")
            ServiceEndpoint sep = null;
            SubscriberSettings settings = new SubscriberSettings();
            settings.PullSettings = new CallSettings();
            settings.PullSettings.Timing = CallTiming.FromDeadline(DateTime.UtcNow.AddMinutes(5)); // 5 minutes deadline time.
            // Client to access PubSub for subscriptions.
            this._subscriberClient = SubscriberClient.Create(sep, settings);
        }
 public void DeleteSubscription(string subscriptionId, SubscriberClient
     subscriber)
 {
     // [START delete_subscription]
     string subscriptionName =
         SubscriberClient.FormatSubscriptionName(_projectId,
         subscriptionId);
     subscriber.DeleteSubscription(subscriptionName);
     // [END delete_subscription]
 }
 public Subscription GetSubscription(string subscriptionId,
     SubscriberClient subscriber)
 {
     string subscriptionName =
         SubscriberClient.FormatSubscriptionName(_projectId,
         subscriptionId);
     Subscription subscription = _subscriber.GetSubscription(
         subscriptionName);
     return subscription;
 }
 public IEnumerable<Subscription> ListSubscriptions(SubscriberClient
     subscriber)
 {
     // [START list_subscriptions]
     string projectName = PublisherClient.FormatProjectName(_projectId);
     IEnumerable<Subscription> subscriptions =
         subscriber.ListSubscriptions(projectName);
     // [END list_subscriptions]
     return subscriptions;
 }
 public PullResponse PullTopicMessage(string subscriptionId,
     SubscriberClient subscriber)
 {
     // [START pull_messages]
     string subscriptionName =
         SubscriberClient.FormatSubscriptionName(_projectId,
         subscriptionId);
     PullResponse response = subscriber.Pull(subscriptionName,
         returnImmediately: true, maxMessages: 10);
     // [END pull_messages]
     return response;
 }
 // [START retry]
 public void RpcRetry(string topicId, string subscriptionId,
     PublisherClient publisher, SubscriberClient subscriber)
 {
     string topicName = PublisherClient.FormatTopicName(_projectId,
         topicId);
     string subscriptionName =
     // Create Subscription.
     SubscriberClient.FormatSubscriptionName(_projectId, subscriptionId);
     // Create Topic
     try
     {
         // This may fail if the Topic already exists.
         // Don't retry in that case.
         publisher.CreateTopic(topicName, newRetryCallSettings(3,
             StatusCode.AlreadyExists));
     }
     catch (RpcException e)
     when (e.Status.StatusCode == StatusCode.AlreadyExists)
     {
         // Already exists.  That's fine.
     }
     try
     {
         // Subscribe to Topic
         // This may fail if the Subscription already exists or
         // the Topic has not yet been created.  In those cases, don't
         // retry, because a retry would fail the same way.
         subscriber.CreateSubscription(subscriptionName, topicName,
             pushConfig: null, ackDeadlineSeconds: 60,
             callSettings: newRetryCallSettings(3, StatusCode.AlreadyExists,
                 StatusCode.NotFound));
     }
     catch (RpcException e)
     when (e.Status.StatusCode == StatusCode.AlreadyExists)
     {
         // Already exists.  That's fine.
     }
 }
 public void CreateSubscription(string topicId, string subscriptionId,
     SubscriberClient subscriber)
 {
     // [START create_subscription]
     string topicName = PublisherClient.FormatTopicName(_projectId,
         topicId);
     string subscriptionName =
         SubscriberClient.FormatSubscriptionName(_projectId,
         subscriptionId);
     try
     {
         Subscription subscription = subscriber.CreateSubscription(
             subscriptionName, topicName, pushConfig: null,
             ackDeadlineSeconds: 60);
     }
     catch (RpcException e)
     when (e.Status.StatusCode == StatusCode.AlreadyExists)
     {
         // Already exists.  That's fine.
     }
     // [END create_subscription]
 }