protected override Task ProduceOne(object message, Type type, CancellationToken cancellationToken) { var pubSubMessage = CreateMessage(message, type); return(_orderingKey == null ? _client.PublishAsync(pubSubMessage) : _client.PublishAsync(_orderingKey, pubSubMessage)); }
public async Task <IActionResult> Index(UploadViewModel model) { if (!ModelState.IsValid) { model.Errors = "All fields are required!"; return(View(model)); } var image = model.Image.OpenReadStream(); var fighterType = await _imageDescriptor.GetFighterTypeAsync(image); if (!fighterType.HasValue) { model.Errors = "Please upload an image of either a dog or a cat"; return(View(model)); } var fighterId = await _dataService.SaveFighterAsync(new Fighter { Name = model.Name }, image, model.Image.ContentType, fighterType.Value); TopicName topicName = new TopicName(_config.ProjectId, _config.Topic); PublisherClient publisher = await PublisherClient.CreateAsync(topicName); string messageId = await publisher.PublishAsync(fighterId); _logger.LogInformation($"Published message : {messageId}"); return(View(new UploadViewModel { IsSuccess = true })); }
public static async Task <object> PublishMessagesAsync(PublisherClient publisher, IEnumerable <string> messageTexts) { // [START pubsub_quickstart_publisher] // [START pubsub_publisher_batch_settings] // PublisherClient collects messages into appropriately sized // batches. // [START pubsub_publish] var publishTasks = messageTexts.Select(async text => { try { string message = await publisher.PublishAsync(text); await Console.Out.WriteLineAsync($"Published message {message}"); } catch (Exception exception) { await Console.Out.WriteLineAsync($"An error ocurred when publishing message {text}:"); await Console.Out.WriteLineAsync(exception.Message); } }); await Task.WhenAll(publishTasks); // [END pubsub_publish] // [END pubsub_publisher_batch_settings] // [END pubsub_quickstart_publisher] return(0); }
public static object PublishMessages(PublisherClient publisher, IEnumerable <string> messageTexts) { // [START pubsub_quickstart_publisher] // [START pubsub_publisher_batch_settings] var publishTasks = new List <Task <string> >(); // PublisherClient collects messages into appropriately sized // batches. foreach (string text in messageTexts) { // Record all publishing Tasks. When each Task completes // without error, the message has been successfully published. // In real use don't simply store all publish Tasks forever; // it is usually appropriate to keep a reference to the Task // only until it has completed. publishTasks.Add(publisher.PublishAsync(text)); } foreach (var task in publishTasks) { Console.WriteLine("Published message {0}", task.Result); } // [END pubsub_publisher_batch_settings] // [END pubsub_quickstart_publisher] return(0); }
public async Task <string> ScheduleAsync(EmailMessage message, CancellationToken cancellationToken) { var context = _httpContextAccessor.HttpContext; if (context is null) { throw new InvalidOperationException($"Email scheduler is being invoked outside of request context."); } if (!context.User.Identity.IsAuthenticated) { throw new UnauthorizedException(); } var owner = context.User.FindFirstValue(OAuth2ClaimTypes.Ownership); if (string.IsNullOrEmpty(owner)) { throw new ForbiddenException($"Current user does not specify ownership."); } var jsonMessage = JsonSerializer.SerializeToUtf8Bytes( new EmailMessageTask(message, owner), _jsonOptions.CurrentValue ); var messageId = await _client.PublishAsync(new PubsubMessage { Data = Google.Protobuf.ByteString.CopyFrom(jsonMessage) }); return(messageId); }
public async Task <IActionResult> Index(string SourceText) { // Look up the most recent 20 translations. var query = _translations.OrderByDescending("TimeStamp") .Limit(20); var snapshotTask = query.GetSnapshotAsync(); if (!string.IsNullOrWhiteSpace(SourceText)) { // Submit a new translation request. await _publisher.PublishAsync(new PubsubMessage() { Data = ByteString.CopyFromUtf8(SourceText) }); } // Render the page. var model = new HomeViewModel() { Translations = (await snapshotTask).Documents.Select( doc => doc.ConvertTo <Translation>()).ToList(), SourceText = SourceText }; return(View(model)); }
public async Task PublishAsync() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); // Snippet: PublishAsync(*,*,CallSettings) // Additional: PublishAsync(*,*,CancellationToken) PublisherClient client = PublisherClient.Create(); // Make sure we have a topic to publish to string topicName = PublisherClient.FormatTopicName(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 }
private static Task PublishMessage(PublisherClient client, string projectId, string topicId, string message) { var messages = new[] { new PubsubMessage { Data = ByteString.CopyFromUtf8(message) } }; return(client.PublishAsync(TopicTemplate.Expand(projectId, topicId), messages)); }
public async Task Send(byte[] message) { await Task.Run(() => { _publisher.PublishAsync(message); //Thread.Sleep(TimeSpan.FromMilliseconds(1000)); }); }
public async Task <int> PublishMessagesAsync(string projectId, string topicId, IEnumerable <string> messageTexts) { TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); PublisherClient publisher = await PublisherClient.CreateAsync(topicName, new PublisherClient.ClientCreationSettings(credentials: this.channelCredentials)); int publishedMessageCount = 0; var publishTasks = messageTexts.Select(async text => { try { string message = await publisher.PublishAsync(text); Console.WriteLine($"Published message {message}"); Interlocked.Increment(ref publishedMessageCount); } catch (Exception exception) { Console.WriteLine($"An error ocurred when publishing message {text}: {exception.Message}"); } }); await Task.WhenAll(publishTasks); return(publishedMessageCount); //clientCreationSettings.Credentials //PublisherServiceApiSettings publisherServiceApiSettings = new PublisherServiceApiSettings(); //clientCreationSettings.PublisherServiceApiSettings //clientCreationSettings.Credentials //Channel channel = new Channel( // PublisherClient.DefaultEndpoint.Host, // PublisherClient.DefaultEndpoint.Port, // googleCredential.ToChannelCredentials()); //PublisherClient publisher2 = await PublisherClient.CreateAsync(topicName, clientCreationSettings, settings); //PublisherServiceApiClientBuilder bld = new PublisherServiceApiClientBuilder(); //var credential = GoogleCredential.FromJson(@"{""your_credentials_json_itself"":""here""}"); //var channel = new Grpc.Core.Channel(PublisherServiceApiClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials()); //PublisherServiceApiClient.DefaultScopes //var publisher3 = bld. //PublisherServiceApiClientBuilder x = new PublisherServiceApiClientBuilder //{ // CredentialsPath = jsonPath //}; //PublisherServiceApiClient client = x.Build(); }
public async Task SimpleOverview() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); string subscriptionId = _fixture.CreateSubscriptionId(); // Sample: SimpleOverview // First create a topic. PublisherServiceApiClient publisherService = await PublisherServiceApiClient.CreateAsync(); TopicName topicName = new TopicName(projectId, topicId); publisherService.CreateTopic(topicName); // Subscribe to the topic. SubscriberServiceApiClient subscriberService = await SubscriberServiceApiClient.CreateAsync(); SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId); subscriberService.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60); // Publish a message to the topic using PublisherClient. PublisherClient publisher = await PublisherClient.CreateAsync(topicName); // PublishAsync() has various overloads. Here we're using the string overload. string messageId = await publisher.PublishAsync("Hello, Pubsub"); // PublisherClient instance should be shutdown after use. // The TimeSpan specifies for how long to attempt to publish locally queued messages. await publisher.ShutdownAsync(TimeSpan.FromSeconds(15)); // Pull messages from the subscription using SimpleSubscriber. 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)); }); // Tidy up by deleting the subscription and the topic. subscriberService.DeleteSubscription(subscriptionName); publisherService.DeleteTopic(topicName); // End sample Assert.Equal(1, receivedMessages.Count); Assert.Equal("Hello, Pubsub", receivedMessages[0].Data.ToStringUtf8()); }
async Task Produce( PublisherClient client, object message, Type type, PubSubProduceOptions?options ) { var pubSubMessage = CreateMessage(message, type, options); await client.PublishAsync(pubSubMessage).Ignore(); }
public async Task <IActionResult> Get([FromServices] PublisherClient publisherClient) { var stopwatch = new Stopwatch(); stopwatch.Start(); await publisherClient.PublishAsync("Hello everyone. It's great to be with you around this global campfire."); stopwatch.Stop(); return(Ok($"Publish took {stopwatch.ElapsedMilliseconds}ms")); }
public async Task <int> PublishAvroMessagesAsync(string projectId, string topicId, IEnumerable <AvroUtilities.State> messageStates) { TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); PublisherClient publisher = await PublisherClient.CreateAsync(topicName); PublisherServiceApiClient publishApi = PublisherServiceApiClient.Create(); var topic = publishApi.GetTopic(topicName); int publishedMessageCount = 0; var publishTasks = messageStates.Select(async state => { try { string messageId = null; switch (topic.SchemaSettings.Encoding) { case Encoding.Binary: using (var ms = new MemoryStream()) { var encoder = new BinaryEncoder(ms); var writer = new SpecificDefaultWriter(state.Schema); writer.Write(state, encoder); messageId = await publisher.PublishAsync(ms.ToArray()); } break; case Encoding.Json: var jsonMessage = AvroUtilities.StateUtils.StateToJsonString(state); messageId = await publisher.PublishAsync(jsonMessage); break; } Console.WriteLine($"Published message {messageId}"); Interlocked.Increment(ref publishedMessageCount); } catch (Exception exception) { Console.WriteLine($"An error ocurred when publishing message {state}: {exception.Message}"); } }); await Task.WhenAll(publishTasks); return(publishedMessageCount); }
public async Task Create(Entityframework.IEvent Profile) { var message = JsonConvert.SerializeObject(Profile); var pubsubMessage = new PubsubMessage() { Data = ByteString.CopyFromUtf8(message) }; // pubsubMessage.Attributes["token"] = _options.VerificationToken; await publisher.PublishAsync(pubsubMessage); }
public async static Task Main(string[] args) { Console.WriteLine("Hello World!"); Environment.SetEnvironmentVariable( "GOOGLE_APPLICATION_CREDENTIALS", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "My First Project-065d320bab2e.json")); CompanyServiceClient companyServiceClient = CompanyServiceClient.Create(); // GoogleCredential credential = GoogleCredential.GetApplicationDefault(); Google.Cloud.Talent.V4Beta1.Company c = new Google.Cloud.Talent.V4Beta1.Company { Name = "ADI", DisplayName = "ADI", ExternalId = "123" }; Google.Cloud.Talent.V4Beta1.CreateCompanyRequest request = new Google.Cloud.Talent.V4Beta1.CreateCompanyRequest { Company = c }; Google.Cloud.Talent.V4Beta1.Company response = companyServiceClient.CreateCompany(request); Console.WriteLine("Created Company"); Console.WriteLine($"Name: {response.Name}"); Console.WriteLine($"Display Name: {response.DisplayName}"); Console.WriteLine($"External ID: {response.ExternalId}"); //JobServiceClient jobServiceClient = JobServiceClient.Create(); // TenantName tenantName = TenantName.FromProjectTenant(projectId, tenantId); PublisherClient publisher = await PublisherClient.CreateAsync(topicName, null, null); var pubsubMessage = new PubsubMessage() { Data = ByteString.CopyFromUtf8("this is new sample message") }; string messageId = await publisher.PublishAsync(pubsubMessage); var subscriptionName = new SubscriptionName(ProjectId, SubscriptionId); var subscription = await SubscriberClient.CreateAsync(subscriptionName); try { // PullResponse response = subscription.(subscriptionName, true, 10); await subscription.StartAsync((pubsubMessage, cancellationToken) => { // Process the message here. var all = Task.FromResult(SubscriberClient.Reply.Ack); return(all); }); } catch (Exception e) { Console.WriteLine("Something went wrong: {0}", e.Message); } }
public async Task SendMessage(VehiclePositionEvent message) { var messageJson = JsonConvert.SerializeObject(message); var pubsubMessage = new PubsubMessage() { Data = ByteString.CopyFromUtf8(messageJson) }; await _client.PublishAsync(pubsubMessage); }
public async Task ProduceAsync(string key, T message) { var serialized = serializer.SerializeToBytes(message); var pubSubMessage = new PubsubMessage { Data = ByteString.CopyFrom(serialized) }; await publisherClient.PublishAsync(pubSubMessage); }
public static async Task <object> PublishMessagesAsync(PublisherClient publisher, IEnumerable <string> messageTexts) { var publishTasks = messageTexts.Select(text => publisher.PublishAsync(text)); foreach (Task <string> task in publishTasks) { string message = await task; await Console.Out.WriteLineAsync($"Published message {message}"); } return(0); }
public async Task <int> PublishProtoMessagesAsync(string projectId, string topicId, IEnumerable <Utilities.State> messageStates) { TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); PublisherClient publisher = await PublisherClient.CreateAsync(topicName); PublisherServiceApiClient publishApi = PublisherServiceApiClient.Create(); var topic = publishApi.GetTopic(topicName); int publishedMessageCount = 0; var publishTasks = messageStates.Select(async state => { try { string messageId = null; switch (topic.SchemaSettings.Encoding) { case Encoding.Binary: var binaryMessage = state.ToByteString(); messageId = await publisher.PublishAsync(binaryMessage); break; case Encoding.Json: var jsonMessage = JsonFormatter.Default.Format(state); messageId = await publisher.PublishAsync(jsonMessage); break; } Console.WriteLine($"Published message {messageId}"); Interlocked.Increment(ref publishedMessageCount); } catch (Exception exception) { Console.WriteLine($"An error ocurred when publishing message {state}: {exception.Message}"); } }); await Task.WhenAll(publishTasks); return(publishedMessageCount); }
private async Task <string> PublishAsync <T>(TopicName topicName, string emulatorHostAndPort, T message) { PublisherClient publisher = await PublisherClient.CreateAsync(topicName, new PublisherClient.ClientCreationSettings(null, null, ChannelCredentials.Insecure, emulatorHostAndPort)); // PublishAsync() has various overloads. Here we're using the string overload. string messageId = await publisher.PublishAsync(serializer(message), System.Text.Encoding.UTF8); // PublisherClient instance should be shutdown after use. // The TimeSpan specifies for how long to attempt to publish locally queued messages. await publisher.ShutdownAsync(TimeSpan.FromSeconds(15)); //? return(messageId); }
public async void PublishAuthor(Author author) { var topicName = new TopicName(_pubsubSettings.Value.ProjectId, _pubsubSettings.Value.PushTopicId); PublisherClient publisher = await PublisherClient.CreateAsync(topicName); var json = JsonConvert.SerializeObject(author); var message = new PubsubMessage() { Data = ByteString.CopyFromUtf8(json) }; // Publish it var response = publisher.PublishAsync(message); }
/// <summary> /// Internals the send batch. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="topic">The topic to send to.</param> /// <param name="messages">The messages to send.</param> /// <param name="properties">The properties for each message (if all will be the same).</param> /// <param name="setProps">The function for setting props for each method (allows messages to have individual props).</param> /// <param name="batchSize">Size of the batch to send.</param> /// <returns>System.Threading.Tasks.Task.</returns> private async Task InternalSendBatch <T>(string topic, IEnumerable <T> messages, KeyValuePair <string, object>[] properties, Func <T, KeyValuePair <string, object>[]> setProps, int batchSize) where T : class { CreateIfNotExists(); // Work around the PubSub max batch size limitation of 1000 messages. batchSize = batchSize > 1000 ? 1000 : batchSize; var isByteArray = typeof(T) == typeof(byte[]); var publishRequest = new PublishRequest { Topic = topic // "projects/{projectId}/topics/{topicName}" }; foreach (var msg in messages) { var byteStr = isByteArray ? ByteString.CopyFrom(msg as byte[]) : ByteString.CopyFromUtf8(JsonConvert.SerializeObject(msg)); var sendMsg = new PubsubMessage { Data = byteStr }; KeyValuePair <string, object>[] props = properties ?? setProps?.Invoke(msg); if (props != null) { foreach (var(key, value) in props) { sendMsg.Attributes.Add(key, value.ToString()); } } publishRequest.Messages.Add(sendMsg); if (publishRequest.Messages.Count >= batchSize) { // Publish a message to the topic using PublisherClient. await PublisherClient.PublishAsync(publishRequest); publishRequest.Messages.Clear(); } } // Catch any remaining messages. if (publishRequest.Messages.Count > 0) { await PublisherClient.PublishAsync(publishRequest); } }
public async Task SendMessage(T message) { var body = JsonConvert.SerializeObject(message); var attributes = new Dictionary <string, string> { { "Message.Type.FullName", message.GetType().FullName } }; var pubsubMessage = new PubsubMessage() { Data = ByteString.CopyFromUtf8(body) }; pubsubMessage.Attributes.Add(attributes); await _publisher.PublishAsync(pubsubMessage); }
public async Task Write(string eventData, HttpContext context) { PublisherClient publisher = null; foreach (var topicId in _topicIds) { var topicName = new TopicName(_projectId, topicId); _logger.LogInformation($"Publishing to topic '{topicId}' with data '{eventData}"); publisher = await PublisherClient.CreateAsync(topicName); await publisher.PublishAsync(eventData); } if (publisher != null) { await publisher.ShutdownAsync(TimeSpan.FromSeconds(10)); } }
public static async Task <object> PublishMessagesAsync(PublisherClient publisher, IEnumerable <string> messageTexts) { // [START pubsub_quickstart_publisher] // [START pubsub_publisher_batch_settings] // PublisherClient collects messages into appropriately sized // batches. var publishTasks = messageTexts.Select(text => publisher.PublishAsync(text)); foreach (Task <string> task in publishTasks) { string message = await task; await Console.Out.WriteLineAsync($"Published message {message}"); } // [END pubsub_publisher_batch_settings] // [END pubsub_quickstart_publisher] return(0); }
public async Task Write(object eventData, HttpContext context) { PublisherClient publisher = null; foreach (var topicId in _topicIds) { var topicName = new TopicName(_projectId, topicId); publisher = await PublisherClient.CreateAsync(topicName); var message = JsonConvert.SerializeObject(eventData); _logger.LogInformation($"Publishing to topic '{topicId}' with message '{message}'"); await publisher.PublishAsync(message); } if (publisher != null) { await publisher.ShutdownAsync(TimeSpan.FromSeconds(10)); } }
public async Task PublishMessageWithCustomAttributesAsync(string projectId, string topicId, string messageText) { TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); PublisherClient publisher = await PublisherClient.CreateAsync(topicName); var pubsubMessage = new PubsubMessage { // The data is any arbitrary ByteString. Here, we're using text. Data = ByteString.CopyFromUtf8(messageText), // The attributes provide metadata in a string-to-string dictionary. Attributes = { { "year", "2020" }, { "author", "unknown" } } }; string message = await publisher.PublishAsync(pubsubMessage); Console.WriteLine($"Published message {message}"); }
public string Messages() { Random r = new Random(); int messageAmount = r.Next(1, 5); for (int l = 0; l < messageAmount; l++) { var pubsubMessage = new PubsubMessage() { Data = ByteString.CopyFromUtf8(r.Next(0, 100).ToString()) }; pubsubMessage.Attributes["token"] = _options.VerificationToken; _publisher.PublishAsync(pubsubMessage); } totalCronCount = totalCronCount + 1; messageCount = messageCount + messageAmount; return("messages sent"); }
/*public Topic CreateTopic(string projectId, string topicId) * { * 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."); * } * return topic; * }*/ public async Task <int> PublishMessagesAsync(string topicId, string message) { TopicName topicName = TopicName.FromProjectTopic(projectId, topicId); PublisherClient publisher = await PublisherClient.CreateAsync(topicName); int publishedMessageCount = 0; try { string res = await publisher.PublishAsync(message); Console.WriteLine($"Published message {res}"); Interlocked.Increment(ref publishedMessageCount); } catch (Exception exception) { Console.WriteLine($"An error ocurred when publishing message: {exception.Message}"); } return(publishedMessageCount); }