Beispiel #1
0
        public void Emulator()
        {
            // Sample: Emulator
            // [START pubsub_use_emulator]
            // For example, "localhost:8615"
            string emulatorHostAndPort = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");

            Channel channel = new Channel(emulatorHostAndPort, ChannelCredentials.Insecure);
            PublisherServiceApiClient client = PublisherServiceApiClient.Create(channel);

            client.CreateTopic(new TopicName("project", "topic"));
            foreach (var topic in client.ListTopics(new ProjectName("project")))
            {
                Console.WriteLine(topic.Name);
            }
            // [END pubsub_use_emulator]
            // End sample
        }
Beispiel #2
0
        public TopicName CreateTopic()
        {
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();

            Topic topic;

            try
            {
                var topicName = new TopicName(_projectId, "topicId");
                topic = publisher.CreateTopic(topicName.ToString());
                return(topicName);
            }
            catch (RpcException e)
                when(e.Status.StatusCode == StatusCode.AlreadyExists)
                {
                    return(null);
                }
        }
Beispiel #3
0
        public static object CreateTopic(string projectId, string topicId)
        {
            // [START pubsub_create_topic]
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();

            TopicName topicName = new TopicName(projectId, topicId);

            try
            {
                publisher.CreateTopic(topicName);
            }
            catch (RpcException e)
                when(e.Status.StatusCode == StatusCode.AlreadyExists)
                {
                    // Already exists.  That's fine.
                }
            // [END pubsub_create_topic]
            return(0);
        }
Beispiel #4
0
        public void ClientPerMethod()
        {
            string projectId = _fixture.ProjectId;
            string topicId   = _fixture.CreateTopicId();

            // Sample: ClientPerMethod
            // Create a default PublisherServiceApiSettings, with a custom header for calls
            // to the CreateTopic RPC method.
            PublisherServiceApiSettings publisherSettings = new PublisherServiceApiSettings();

            publisherSettings.CreateTopicSettings = publisherSettings.CreateTopicSettings.WithHeader("ClientVersion", "1.0.0");
            PublisherServiceApiClient client = PublisherServiceApiClient.Create(settings: publisherSettings);
            // Create a topic name from the projectId and topicId.
            TopicName topicName = new TopicName(projectId, topicId);
            // The custom 'ClientVersion' header will be included in the RPC call, due to
            // the client being configured with 'publishersettings' above.
            Topic topic = client.CreateTopic(topicName);
            // End sample
        }
Beispiel #5
0
        public void Client()
        {
            string projectId = _fixture.ProjectId;
            string topicId   = _fixture.CreateTopicId();

            // Sample: ClientWide
            // Create a default PublisherServiceApiSettings, with a custom header for calls to all RPC methods.
            PublisherServiceApiSettings publisherSettings = new PublisherServiceApiSettings
            {
                CallSettings = new CallSettings(null, null, null, metadata => metadata.Add("ClientVersion", "1.0.0"), null, null)
            };
            PublisherServiceApiClient client = PublisherServiceApiClient.Create(settings: publisherSettings);
            // Create a topic name from the projectId and topicId.
            TopicName topicName = new TopicName(projectId, topicId);
            // The custom 'ClientVersion' header will be included in the RPC call, due to
            // the client being configured with 'publishersettings' above.
            Topic topic = client.CreateTopic(topicName);
            // End sample
        }
        public PubsubGameBoardQueue(
            IOptions <PubsubGameBoardQueueOptions> options,
            ILogger <PubsubGameBoardQueue> logger)
        {
            _options      = options;
            _logger       = logger;
            _publisherApi = PublisherServiceApiClient.Create();
            var subscriberApi = SubscriberServiceApiClient.Create();

            _publisherClient = PublisherClient.Create(MyTopic,
                                                      new[] { _publisherApi });
            _subscriberClient = SubscriberClient.Create(MySubscription,
                                                        new[] { subscriberApi }, new SubscriberClient.Settings()
            {
                StreamAckDeadline = TimeSpan.FromMinutes(1)
            });

            // Create the Topic and Subscription.
            try
            {
                _publisherApi.CreateTopic(MyTopic);
                _logger.LogInformation("Created {0}.", MyTopic.ToString());
            }
            catch (RpcException e)
                when(e.Status.StatusCode == StatusCode.AlreadyExists)
                {
                    // Already exists.  That's fine.
                }

            try
            {
                subscriberApi.CreateSubscription(MySubscription, MyTopic,
                                                 pushConfig: null, ackDeadlineSeconds: 10);
                _logger.LogInformation("Created {0}.",
                                       MySubscription.ToString());
            }
            catch (RpcException e)
                when(e.Status.StatusCode == StatusCode.AlreadyExists)
                {
                    // Already exists.  That's fine.
                }
        }
Beispiel #7
0
        public async Task InitTopic(string topicID)
        {
            await Task.Run(() =>
            {
                _topicID          = topicID;
                _topicName        = new TopicName(_projectID, _topicID);
                _publisherService = PublisherServiceApiClient.Create();

                try
                {
                    _publisherService.CreateTopic(_topicName);
                }
                catch (Grpc.Core.RpcException e) when(e.Status.StatusCode == Grpc.Core.StatusCode.AlreadyExists)
                {
                    //Console.WriteLine("Topic existed.");
                    _logger.Debug($"[GCPFanout.InitTopic()] Topic: {_topicID} existed");
                }
                _publisher = PublisherClient.CreateAsync(_topicName).Result;
            });
        }
Beispiel #8
0
        public async Task PullAsync()
        {
            string projectId      = _fixture.ProjectId;
            string topicId        = _fixture.CreateTopicId();
            string subscriptionId = _fixture.CreateSubscriptionId();

            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            TopicName topicName = new TopicName(projectId, topicId);

            publisher.CreateTopic(topicName);
            PubsubMessage newMessage = new PubsubMessage {
                Data = ByteString.CopyFromUtf8("Simple text")
            };

            SubscriberServiceApiClient.Create().CreateSubscription(new SubscriptionName(projectId, subscriptionId), topicName, null, 60);
            publisher.Publish(topicName, new[] { newMessage });

            // Snippet: PullAsync(SubscriptionName,*,*,CallSettings)
            // Additional: PullAsync(SubscriptionName,*,*,CancellationToken)
            SubscriberServiceApiClient client = SubscriberServiceApiClient.Create();

            SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);

            PullResponse pullResponse = await client.PullAsync(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);
            await client.AcknowledgeAsync(subscriptionName, ackIds);

            // End snippet
        }
Beispiel #9
0
        public void Overrides()
        {
            string            projectId         = _fixture.ProjectId;
            string            topicId           = _fixture.CreateTopicId();
            DateTime          deadline          = DateTime.MaxValue;
            CancellationToken cancellationToken = new CancellationTokenSource().Token;

            // Sample: Overrides
            // Create a default PublisherServiceApiSettings, with customizations for CreateTopic RPCs:
            // * A custom "ClientVersion" header.
            // * A custom 5-second timeout Timing.
            // * No cancellation token.
            PublisherServiceApiSettings publisherSettings = new PublisherServiceApiSettings();

            publisherSettings.CreateTopicSettings = publisherSettings.CreateTopicSettings
                                                    .WithCancellationToken(CancellationToken.None)
                                                    .WithCallTiming(CallTiming.FromTimeout(TimeSpan.FromSeconds(5)))
                                                    .WithHeader("ClientVersion", "1.0.0");

            // Override the above Timing and CancellationToken in the client-wide CallSettings;
            // the Headers are not overridden.
            publisherSettings.CallSettings = CallSettings
                                             .FromCallTiming(CallTiming.FromDeadline(deadline))
                                             .WithCancellationToken(CancellationToken.None);

            // Create the client with the configured publisherSettings
            PublisherServiceApiClient client = PublisherServiceApiClient.Create(settings: publisherSettings);

            // Create a topic name from the projectId and topicId.
            TopicName topicName = new TopicName(projectId, topicId);

            // Call CreateTopic(). Override only the CancellationToken, using a per-RPC-method CallSettings.
            // The CallSettings used during this RPC invocation is:
            // * A custom "ClientVersion" header.
            // * A Timing deadline of 'deadline' (*not* the overridden 5-second timeout).
            // * The CancellationToken 'cancellationToken' (*not* CancellationToken.None).
            Topic topic = client.CreateTopic(topicName, CallSettings.FromCancellationToken(cancellationToken));
            // End sample
        }
Beispiel #10
0
        // [START retry]
        internal void RpcRetry(string topicId, string subscriptionId,
                               PublisherServiceApiClient publisher, SubscriberServiceApiClient subscriber)
        {
            TopicName topicName = new TopicName(_projectId, topicId);
            // Create Subscription.
            SubscriptionName subscriptionName = new SubscriptionName(_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.
                }
        }
Beispiel #11
0
 private bool EnsureTopicExistence(TopicName _TopicInstance, PublisherServiceApiClient _PublisherAPIClient = null, Action <string> _ErrorMessageAction = null)
 {
     try
     {
         if (_PublisherAPIClient == null)
         {
             _PublisherAPIClient = PublisherServiceApiClient.Create(PublishChannel);
         }
         _PublisherAPIClient.CreateTopic(_TopicInstance);
     }
     catch (Exception e)
     {
         if (e is RpcException && (e as RpcException).Status.StatusCode == StatusCode.AlreadyExists)
         {
             //That is fine.
         }
         else
         {
             _ErrorMessageAction?.Invoke("BPubSubServiceGC->EnsureTopicExistence: " + e.Message + ", Trace: " + e.StackTrace);
             return(false);
         }
     }
     return(true);
 }
Beispiel #12
0
        public async Task StreamingPull()
        {
            string projectId      = _fixture.ProjectId;
            string topicId        = _fixture.CreateTopicId();
            string subscriptionId = _fixture.CreateSubscriptionId();

            // Snippet: StreamingPull(*, *)
            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            TopicName topicName = new TopicName(projectId, topicId);

            publisher.CreateTopic(topicName);
            SubscriberServiceApiClient subscriber       = SubscriberServiceApiClient.Create();
            SubscriptionName           subscriptionName = new SubscriptionName(projectId, subscriptionId);

            subscriber.CreateSubscription(subscriptionName, topicName, null, 60);

            // If we don't see all the messages we expect in 10 seconds, we'll cancel the call.
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            CallSettings            callSettings            = CallSettings.FromCancellationToken(cancellationTokenSource.Token);

            SubscriberServiceApiClient.StreamingPullStream stream = subscriber.StreamingPull(callSettings);

            // The first request must include the subscription name and the stream ack deadline
            await stream.WriteAsync(new StreamingPullRequest { SubscriptionAsSubscriptionName = subscriptionName, StreamAckDeadlineSeconds = 20 });

            Task pullingTask = Task.Run(async() =>
            {
                int messagesSeen = 0;
                IAsyncEnumerator <StreamingPullResponse> responseStream = stream.ResponseStream;

                // Handle responses as we see them.
                while (await responseStream.MoveNext())
                {
                    StreamingPullResponse response = responseStream.Current;
                    Console.WriteLine("Received streaming response");
                    foreach (ReceivedMessage message in response.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 we've just seen
                    await stream.WriteAsync(new StreamingPullRequest {
                        AckIds = { response.ReceivedMessages.Select(rm => rm.AckId) }
                    });

                    // If we've seen all the messages we expect, we can complete the streaming call,
                    // and our next MoveNext call will return false.
                    messagesSeen += response.ReceivedMessages.Count;
                    if (messagesSeen == 3)
                    {
                        await stream.WriteCompleteAsync();
                    }
                }
            });

            publisher.Publish(topicName, new[] { new PubsubMessage {
                                                     Data = ByteString.CopyFromUtf8("Message 1")
                                                 } });
            publisher.Publish(topicName, new[] { new PubsubMessage {
                                                     Data = ByteString.CopyFromUtf8("Message 2")
                                                 } });
            publisher.Publish(topicName, new[] { new PubsubMessage {
                                                     Data = ByteString.CopyFromUtf8("Message 3")
                                                 } });

            await pullingTask;
            // End snippet
        }
Beispiel #13
0
        private static void Start()
        {
            SubscriberClient _subscriber;
            PublisherClient  _publisher;
            // Instantiates a client
            PublisherServiceApiClient publisherApi = PublisherServiceApiClient.Create();
            // Subscribe to the topic.
            TopicName                  pubsubTopicName  = new TopicName(projectId, topicName);
            SubscriptionName           subscriptionName = new SubscriptionName(projectId, subscriptionId);
            SubscriberServiceApiClient subscriberApi    = SubscriberServiceApiClient.Create();

            // Creates the new topic
            try
            {
                Topic topic = publisherApi.CreateTopic(pubsubTopicName);
                Console.WriteLine($"Topic {topic.Name} created.");
            }
            catch (Grpc.Core.RpcException e)
                when(e.Status.StatusCode == Grpc.Core.StatusCode.AlreadyExists)
                {
                    Console.WriteLine($"Topic {topicName} already exists.");
                }
            // Create the new subscription
            try
            {
                subscriberApi.CreateSubscription(subscriptionName, pubsubTopicName, null, 120);
                Console.WriteLine($"Subscription {subscriptionName.Kind} created.");
            }
            catch (Grpc.Core.RpcException e) when(e.Status.StatusCode == Grpc.Core.StatusCode.AlreadyExists)
            {
                // OK
                Console.WriteLine($"Subscription {subscriptionName.Kind} already exists");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            _subscriber = SubscriberClient.Create(subscriptionName, new[] { subscriberApi });

            _publisher = PublisherClient.Create(pubsubTopicName, new[] { publisherApi });

            _publisher.PublishAsync("Bla-Bla-Bla-Message.");

            _subscriber.StartAsync((message, token) =>
            {
                string data = message.Data.ToStringUtf8();
                try
                {
                    Console.WriteLine($"Pubsub message id={message.MessageId}, " +
                                      $"created at {message.PublishTime}, data{message.Data.ToStringUtf8()}");

                    // TODO: Replace with ACK
                    return(System.Threading.Tasks.Task.FromResult(SubscriberClient.Reply.Nack));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(System.Threading.Tasks.Task.FromResult(SubscriberClient.Reply.Nack));
                }
            });

            // VARIAN II :

            // Pull messages from the subscription.We're returning immediately, whether or not there
            // are messages; in other cases you'll want to allow the call to wait until a message arrives.
            PullResponse response = subscriberApi.Pull(subscriptionName, returnImmediately: true, maxMessages: 10);

            foreach (ReceivedMessage received in response.ReceivedMessages)
            {
                PubsubMessage msg = received.Message;
                Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
            }
            // Acknowledge that we've received the messages. If we don't do this within 60 seconds (as specified
            // when we created the subscription) we'll receive the messages again when we next pull.
            subscriberApi.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId));

            // Tidy up by deleting the subscription and the topic.
            subscriberApi.DeleteSubscription(subscriptionName);
            publisherApi.DeleteTopic(pubsubTopicName);
        }
    public async Task WithEmulatorAsync(string projectId, string topicId, string subscriptionId)
    {
        // Use EmulatorDetection.EmulatorOrProduction to create service clients that will
        // that will connect to the PubSub emulator if the PUBSUB_EMULATOR_HOST environment
        // variable is set, but will otherwise connect to the production environment.

        // Create the PublisherServiceApiClient using the PublisherServiceApiClientBuilder
        // and setting the EmulatorDection property.
        PublisherServiceApiClient publisherService = await new PublisherServiceApiClientBuilder
        {
            EmulatorDetection = EmulatorDetection.EmulatorOrProduction
        }.BuildAsync();

        // Use the client as you'd normally do, to create a topic in this example.
        TopicName topicName = new TopicName(projectId, topicId);

        publisherService.CreateTopic(topicName);

        // Create the SubscriberServiceApiClient using the SubscriberServiceApiClientBuilder
        // and setting the EmulatorDection property.
        SubscriberServiceApiClient subscriberService = await new SubscriberServiceApiClientBuilder
        {
            EmulatorDetection = EmulatorDetection.EmulatorOrProduction
        }.BuildAsync();

        // Use the client as you'd normally do, to create a subscription in this example.
        SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId);

        subscriberService.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60);

        // Create the PublisherClient using PublisherClient.ClientCreationSettings
        // and call the WithEmulatorDection method.
        PublisherClient publisher = await PublisherClient.CreateAsync(
            topicName,
            new PublisherClient.ClientCreationSettings()
            .WithEmulatorDetection(EmulatorDetection.EmulatorOrProduction));

        // Use the client as you'd normally do, to send a message in this example.
        await publisher.PublishAsync("Hello, Pubsub");

        await publisher.ShutdownAsync(TimeSpan.FromSeconds(15));

        // Create the SubscriberClient using SubscriberClient.ClientCreationSettings
        // and call the WithEmulatorDection method.
        SubscriberClient subscriber = await SubscriberClient.CreateAsync(
            subscriptionName,
            new SubscriberClient.ClientCreationSettings()
            .WithEmulatorDetection(EmulatorDetection.EmulatorOrProduction));

        List <PubsubMessage> receivedMessages = new List <PubsubMessage>();

        // Use the client as you'd normally do, to listen for messages in this example.
        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()}'");
            // In this example we stop the subscriber when the message is received.
            // You may leave the subscriber running, and it will continue to received published messages
            // if any.
            // 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));
        });
    }
        static void Main(string[] args)
        {
            GCPublisherService  gcPublisherService  = new GCPublisherService();
            GCSubscriberService gcSubscriberService = new GCSubscriberService();

            gcPublisherService.StartPeriodicallyPublish(2000);
            gcSubscriberService.StreamingPull();

            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            TopicName topicName = new TopicName("k8s-brewery", "sdk-example-test-topic-2");

            try
            {
                publisher.CreateTopic(topicName);
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to create topic");
            }

            // Publish a message to the topic.
            PubsubMessage message = new PubsubMessage
            {
                Data       = ByteString.CopyFromUtf8("Message "),
                Attributes =
                {
                    { "Description", "Simple text message " }
                }
            };

            publisher.Publish(topicName, new[] { message });

            SubscriberServiceApiClient subscriber       = SubscriberServiceApiClient.Create();
            SubscriptionName           subscriptionName = new SubscriptionName("k8s-brewery", "sdk-example-test-subscription-2");

            try
            {
                subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60);
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to create subscription");
            }

            PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 100);

            foreach (ReceivedMessage received in response.ReceivedMessages)
            {
                using (var span = CustomSpan.Create()
                                  .AsGCPubSubReceive(subscriptionName.SubscriptionId, subscriptionName.ProjectId)
                                  .AsChildOf(() => GCSubscriberService.GetDisInfo(received.Message)))
                {
                    span.WrapAction(() =>
                    {
                        PubsubMessage msg = received.Message;
                        Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
                        Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
                        Console.WriteLine($"Attributes: '{string.Join(",", msg.Attributes.Select(x => $"{x.Key}-{x.Value}"))}'");
                    }, true);
                }
            }
            if (response.ReceivedMessages.Count > 0)
            {
                subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId));
            }

            Console.WriteLine("Press any key to close ...");
            Console.ReadKey();
        }