public static int Main(string[] args) { // Instantiates a client PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); // Your Google Cloud Platform project ID string projectId = "YOUR-PROJECT-ID"; // [END pubsub_quickstart] if (projectId == "YOUR-PROJECT" + "-ID") { Console.WriteLine("Edit Program.cs and replace YOUR-PROJECT-ID with your project id."); return(-1); } // [START pubsub_quickstart] // The name for the new topic var topicName = new TopicName(projectId, "my-new-topic"); // Creates the new topic try { Topic topic = publisher.CreateTopic(topicName); 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."); } return(0); }
public void StartPeriodicallyPublish(int timeIntervalInSeconds) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = new TopicName("k8s-brewery", "sdk-example-test-topic"); try { publisher.CreateTopic(topicName); } catch (Exception) { Console.WriteLine("Failed to create topic"); } int j = 0; Task.Run(() => { while (!token.IsCancellationRequested) { // Publish a message to the topic. PubsubMessage message = new PubsubMessage { Data = ByteString.CopyFromUtf8("Test message number " + j), Attributes = { { "description", "Simple text message number " + j } } }; publisher.Publish(topicName, new[] { message }); j++; Thread.Sleep(timeIntervalInSeconds * 1000); } }); }
public async Task <string> EnviarMensagem(string mensagem) { var topicName = new TopicName("estudo-ci-cd", "tp-poc-ex"); string retornoIdMensagem = ""; PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); // Create a message var message = new PubsubMessage() { Data = ByteString.CopyFromUtf8(mensagem) }; //message.Attributes.Add("myattrib", "its value"); var messageList = new List <PubsubMessage>() { message }; var response = await publisher.PublishAsync(topicName, messageList); foreach (var item in response.MessageIds) { retornoIdMensagem = item; } return(retornoIdMensagem); }
public Topic CreateTopicWithSchema(string projectId, string topicId, string schemaName, Encoding encoding) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); var topicName = TopicName.FromProjectTopic(projectId, topicId); Topic topic = new Topic { Name = topicName.ToString(), SchemaSettings = new SchemaSettings { Schema = schemaName, Encoding = encoding } }; Topic receivedTopic = null; try { receivedTopic = publisher.CreateTopic(topic); Console.WriteLine($"Topic {topic.Name} created."); } catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists) { Console.WriteLine($"Topic {topicName} already exists."); } return(receivedTopic); }
private bool GetPublisher(out PublisherServiceApiClient Result, TopicName GoogleFriendlyTopicName, Action <string> _ErrorMessageAction = null) { lock (LockablePublisherTopicDictionaryObject(GoogleFriendlyTopicName.TopicId)) { if (PublisherTopicDictionary.ContainsKey(GoogleFriendlyTopicName.TopicId)) { Result = PublisherTopicDictionary[GoogleFriendlyTopicName.TopicId]; return(true); } try { Result = PublisherServiceApiClient.Create(PublishChannel); PublisherTopicDictionary[GoogleFriendlyTopicName.TopicId] = Result; } catch (Exception e) { Result = null; _ErrorMessageAction?.Invoke("BPubSubServiceGC->GetPublisher: " + e.Message + ", Trace: " + e.StackTrace); return(false); } if (!EnsureTopicExistence(GoogleFriendlyTopicName, Result, _ErrorMessageAction)) { Result = null; return(false); } return(true); } }
private static void PublishToTopic() { var topicName = new TopicName(ProjectId, TopicId); var publisher = PublisherServiceApiClient.Create(); // Create a message var message = new PubsubMessage() { Data = ByteString.CopyFromUtf8("hello world") }; message.Attributes.Add("myattrib", "its value"); // Add it to the list of messages to publish var messageList = new List <PubsubMessage>() { message }; // Publish it Console.WriteLine("Publishing..."); var response = publisher.Publish(topicName, messageList); // Get the message ids GCloud gave us Console.WriteLine(" Message ids published:"); foreach (string messageId in response.MessageIds) { Console.WriteLine($" {messageId}"); } }
public static object SetSubscriptionIamPolicy(string projectId, string subscriptionId, string role, string member) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); string roleToBeAddedToPolicy = $"roles/{role}"; // [START pubsub_set_subscription_policy] Policy policy = new Policy { Bindings = { new Binding { Role = roleToBeAddedToPolicy, Members ={ member } } } }; SetIamPolicyRequest request = new SetIamPolicyRequest { Resource = new SubscriptionName(projectId, subscriptionId).ToString(), Policy = policy }; Policy response = publisher.SetIamPolicy(request); Console.WriteLine($"Subscription IAM Policy updated: {response}"); // [END pubsub_set_subscription_policy] return(0); }
public Policy SetSubscriptionIamPolicy(string projectId, string subscriptionId, string role, string member) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); string roleToBeAddedToPolicy = $"roles/{role}"; Policy policy = new Policy { Bindings = { new Binding { Role = roleToBeAddedToPolicy, Members ={ member } } } }; SetIamPolicyRequest request = new SetIamPolicyRequest { ResourceAsResourceName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId), Policy = policy }; Policy response = publisher.SetIamPolicy(request); return(response); }
private static void InstantiateClient(out PublisherServiceApiClient publisher, out TopicName topicName) { publisher = PublisherServiceApiClient.Create(); //your Google Cloud Platform project id string projectId = "YOUR-PROJECT-ID"; //The name of topic topicName = new TopicName(projectId, "my-topic"); //creates a new topic Console.WriteLine("step-1 : creating Topic-"); try { Topic topic = publisher.CreateTopic(topicName); 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."); } catch (Exception ex) { Console.WriteLine("Add Error : " + ex.Message.ToString()); } }
public void CreatePubSubTopic(TopicName topicName) { var publisher = PublisherServiceApiClient.Create(); try { publisher.CreateTopic(topicName); } catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists) { } Policy policy = new Policy { Bindings = { new Binding { Role = "roles/pubsub.publisher", Members ={ ServiceAccount } } } }; SetIamPolicyRequest request = new SetIamPolicyRequest { Resource = $"projects/{ProjectId}/topics/{topicName.TopicId}", Policy = policy }; Policy response = publisher.SetIamPolicy(request); Console.WriteLine($"Topic IAM Policy updated: {response}"); }
public Topic CreateTopic(string projectId, string topicId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); var topicName = TopicName.FromProjectTopic(projectId, topicId); Topic topic = null; try { topic = publisher.CreateTopic(topicName); Console.WriteLine($"Topic {topic.Name} created."); } catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists) { Console.WriteLine($"Topic {topicName} already exists."); } PubsubMessage message = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8("Hello, Pubsub"), // The attributes provide metadata in a string-to-string dictionary. Attributes = { { "description", "Simple text message" } } }; publisher.Publish(topicName, new[] { message }); return(topic); }
public void PublishMessage(string message, string topicName) { PublisherServiceApiClient client = PublisherServiceApiClient.Create(); Topic MyTopic = GetOrCreateTopic(topicName); List <PubsubMessage> myMessages = new List <PubsubMessage>(); myMessages.Add(new PubsubMessage() { //create mailmessage object instead of string //serialize mailmessage and convert to binary by using binary formatter //and pass to the below: ByteString.CopyFrom <- array of bytes or use ByteString.FromStream MessageId = Guid.NewGuid().ToString(), PublishTime = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.UtcNow), Data = ByteString.CopyFromUtf8(message) }); try { client.Publish(MyTopic.TopicName, myMessages); } catch (Exception ex) { } }
/// <summary> /// Asynchronously adds a metadata payload to Google PubSub /// </summary> /// <param name="metadata">metadata to add to queue</param> /// <returns>success of operation</returns> public async Task <bool> Enqueue(QueueMetadata metadata) { try { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = new TopicName(this.cloud.GoogleProjectID, this.cloud.QueueName); await this.CreateTopicIfNotExistsAsync(publisher, topicName); var message = new PubsubMessage() { Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(metadata)), }; var messageList = new List <PubsubMessage>() { message }; await publisher.PublishAsync(topicName, messageList); } catch { return(false); } return(true); }
public Topic GetTopic(string topicId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = TopicName.FromProjectTopic(ProjectId, topicId); return(publisher.GetTopic(topicName)); }
public static void DeleteTopic(string projectId, string topicId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); publisher.DeleteTopic(topicName); }
public async Task PublishAsync() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Snippet: PublishAsync(*,*,CallSettings) // Additional: PublishAsync(*,*,CancellationToken) PublisherServiceApiClient client = PublisherServiceApiClient.Create(); // Make sure we have a topic to publish to TopicName topicName = new TopicName(projectId, topicId); await client.CreateTopicAsync(topicName); PubsubMessage message = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8("Hello, Pubsub"), // The attributes provide metadata in a string-to-string dictionary. Attributes = { { "description", "Simple text message" } } }; await client.PublishAsync(topicName, new[] { message }); // End snippet }
public static int Main(string[] args) { // Read projectId from args if (args.Length != 1) { Console.WriteLine("Usage: Project ID must be passed as first argument."); Console.WriteLine(); return(1); } string projectId = args[0]; // Create client PublisherServiceApiClient client = PublisherServiceApiClient.Create(); // Initialize request argument(s) ProjectName project = new ProjectName(projectId); // Call API method PagedEnumerable <ListTopicsResponse, Topic> pagedResponse = client.ListTopics(project); // Show the result foreach (var item in pagedResponse) { Console.WriteLine(item); } // Success Console.WriteLine("Smoke test passed OK"); return(0); }
public SpeedTestEvents(string projectId, string topicId) { var publisherClient = PublisherServiceApiClient.Create(); _publisher = PublisherClient.CreateAsync( new TopicName(projectId, topicId)) .GetAwaiter().GetResult(); }
public Policy GetSubscriptionIamPolicy(string projectId, string subscriptionId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId); Policy policy = publisher.GetIamPolicy(subscriptionName); return(policy); }
public Policy GetTopicIamPolicy(string projectId, string topicId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); Policy policy = publisher.GetIamPolicy(topicName); return(policy); }
public void Post([FromBody] string id) { var publisher = PublisherServiceApiClient.Create(); var topicName = TopicName.FromProjectTopic(ProjectId, id); publisher.CreateTopic(topicName); }
public static IEnumerable <Topic> ListProjectTopics(string projectId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); ProjectName projectName = ProjectName.FromProject(projectId); IEnumerable <Topic> topics = publisher.ListTopics(projectName); return(topics); }
public IEnumerable <string> ListSubscriptionsInTopic(string projectId, string topicId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); IEnumerable <string> subscriptions = publisher.ListTopicSubscriptions(topicName); return(subscriptions); }
public PublisherServiceApiClient CreatePublisherClient() { // [START create_publisher_client] PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); // [END create_publisher_client] return(publisher); }
public void Delete([FromQuery] string topicId) { var publisher = PublisherServiceApiClient.Create(); var topicName = TopicName.FromProjectTopic(ProjectId, topicId); publisher.DeleteTopic(topicName); }
public void PublishMessage(string projectId, string topicId, string message, Dictionary <string, string> attributes = null) { var publisherService = PublisherServiceApiClient.Create(); var topicName = new TopicName(projectId, topicId); var pubsubMessage = BuildPubsubMessage(message, attributes); publisherService.Publish(topicName, new[] { pubsubMessage }); }
public void Overview() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); string subscriptionId = _fixture.CreateSubscriptionId(); // Sample: Overview // First create a topic. PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = new TopicName(projectId, topicId); publisher.CreateTopic(topicName); // Subscribe to the topic. SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create(); SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId); subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60); // Publish a message to the topic. PubsubMessage message = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8("Hello, Pubsub"), // The attributes provide metadata in a string-to-string dictionary. Attributes = { { "description", "Simple text message" } } }; publisher.Publish(topicName, new[] { message }); // 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 = subscriber.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. subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId)); // Tidy up by deleting the subscription and the topic. subscriber.DeleteSubscription(subscriptionName); publisher.DeleteTopic(topicName); // End sample Assert.Equal(1, response.ReceivedMessages.Count); Assert.Equal("Hello, Pubsub", response.ReceivedMessages[0].Message.Data.ToStringUtf8()); Assert.Equal("Simple text message", response.ReceivedMessages[0].Message.Attributes["description"]); }
public static object GetTopic(string projectId, string topicId) { PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); TopicName topicName = new TopicName(projectId, topicId); Topic topic = publisher.GetTopic(topicName); Console.WriteLine($"Topic found: {topic.TopicName.ToString()}"); return(0); }
public IEnumerable <Topic> Get() { var publisher = PublisherServiceApiClient.Create(); var projectName = ProjectName.FromProject(ProjectId); var topics = publisher.ListTopics(projectName); return(topics.ToList()); }
public Topic Get(string id) { var publisher = PublisherServiceApiClient.Create(); var topicName = TopicName.FromProjectTopic(ProjectId, id); var topic = publisher.GetTopic(topicName); return(topic); }