public void TestAcknowledgeTopicMessage() { string topicId = "testTopicForMessageAcknowledgement"; string subscriptionId = "testSubscriptionForMessageAcknowledgement"; SubscriptionName subscriptionName = new SubscriptionName(_projectId, subscriptionId); CreateTopic(topicId, _publisher); CreateSubscription(topicId, subscriptionId, _subscriber); CreateTopicMessage(topicId, _publisher); //Pull the Message Eventually(() => { PullResponse response = PullTopicMessage(subscriptionId, _subscriber); Assert.True(response.ReceivedMessages.Count > 0); //Acknowledge the Message AcknowledgeTopicMessage(subscriptionId, _subscriber, response); }); Eventually(() => { //Pull the Message to confirm it is gone after it is acknowledged PullResponse response = PullTopicMessage(subscriptionId, _subscriber); Assert.True(IsEmptyResponse(response)); }); DeleteSubscription(subscriptionId, _subscriber); DeleteTopic(topicId, _publisher); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); if (CanConnectToPubSub()) { // Subscribe to the topic. SubscriberServiceApiClient subscriber = CreateSubscriber(); var subscriptionName = new SubscriptionName(GCP_PROJECT, GCP_SUBSCRIBER); PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 100); foreach (ReceivedMessage received in response.ReceivedMessages) { PubsubMessage msg = received.Message; _logger.LogInformation($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}"); _logger.LogInformation($"Text: '{msg.Data.ToStringUtf8()}'"); } if (response.ReceivedMessages.Any()) { subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId)); } } } catch (Exception e) { _logger.LogError($"{e.Message} - {e.StackTrace}"); } await Task.Delay(5000, stoppingToken); } }
public int PullMessagesSync(string projectId, string subscriptionId, bool acknowledge) { SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(projectId, subscriptionId); SubscriberServiceApiClient subscriberClient = SubscriberServiceApiClient.Create(); int messageCount = 0; try { // Pull messages from server, // allowing an immediate response if there are no messages. PullResponse response = subscriberClient.Pull(subscriptionName, returnImmediately: false, maxMessages: 20); // Print out each received message. foreach (ReceivedMessage msg in response.ReceivedMessages) { string text = System.Text.Encoding.UTF8.GetString(msg.Message.Data.ToArray()); Console.WriteLine($"Message {msg.Message.MessageId}: {text}"); Interlocked.Increment(ref messageCount); } // If acknowledgement required, send to server. if (acknowledge && messageCount > 0) { subscriberClient.Acknowledge(subscriptionName, response.ReceivedMessages.Select(msg => msg.AckId)); } } catch (RpcException ex) when(ex.Status.StatusCode == StatusCode.Unavailable) { // UNAVAILABLE due to too many concurrent pull requests pending for the given subscription. } return(messageCount); }
public async Task <IEnumerable <ReceivedMessage> > GetMessages(int count = 1) { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace("Getting {Count} message(s) from Google PubSub topic {TopicId}", count, TopicName.TopicId); } PullResponse response = null; try { //According to Google, no more than 1000 messages can be published/received response = await _subscriber.PullAsync(SubscriptionName, true, count < 1?MAX_PULLED_MESSAGES : count); } catch (Exception exc) { ReportErrorAndRethrow(exc, "GetMessages", GoogleErrorCode.GetMessages); } if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace("Received {Count} message(s) from Google PubSub topic {TopicId}", response.ReceivedMessages.Count, TopicName.TopicId); foreach (var received in response.ReceivedMessages) { _logger.LogTrace( "Received message {MessageId} published {PublishedTime} from Google PubSub topic {TopicId}", received.Message.MessageId, received.Message.PublishTime.ToDateTime(), TopicName.TopicId); } } return(response.ReceivedMessages); }
private static PullResponse ShowMessagesForSubscription() { var subscriptionName = new SubscriptionName(ProjectId, SubscriptionId); var subscription = SubscriberServiceApiClient.Create(); try { PullResponse response = subscription.Pull(subscriptionName, true, 2); var all = response.ReceivedMessages; Console.WriteLine("Inside subscription" + all.Count); foreach (ReceivedMessage message in all) { string id = message.Message.MessageId; string publishDate = message.Message.PublishTime.ToDateTime().ToString("dd-M-yyyy HH:MM:ss"); string data = message.Message.Data.ToStringUtf8(); Console.WriteLine($"{id} {publishDate} - {data}"); Console.Write(" Acknowledging..."); subscription.Acknowledge(subscriptionName, new string[] { message.AckId }); Console.WriteLine("done"); } return(response); } catch (RpcException e) { Console.WriteLine("Something went wrong: {0}", e.Message); return(null); } }
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> }
public static object PullMessagesSync(string projectId, string subscriptionId, bool acknowledge) { // [START pubsub_subscriber_sync_pull] SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId); SubscriberServiceApiClient subscriberClient = SubscriberServiceApiClient.Create(); // Pull messages from server, // allowing an immediate response if there are no messages. PullResponse response = subscriberClient.Pull( subscriptionName, returnImmediately: true, maxMessages: 20); // Print out each received message. foreach (ReceivedMessage msg in response.ReceivedMessages) { string text = Encoding.UTF8.GetString(msg.Message.Data.ToArray()); Console.WriteLine($"Message {msg.Message.MessageId}: {text}"); } // If acknowledgement required, send to server. if (acknowledge) { subscriberClient.Acknowledge(subscriptionName, response.ReceivedMessages.Select(msg => msg.AckId)); } // [END pubsub_subscriber_sync_pull] return(0); }
/// <summary> /// Internals the receive batch. Always returns list of messages back but will be empty if none where returned from the server. /// </summary> /// <typeparam name="T">Type of messages to serialize to.</typeparam> /// <param name="subscriptionName">Name of the subscription.</param> /// <param name="batchSize">Size of the batch.</param> /// <returns>System.Threading.Tasks.Task<System.Collections.Generic.List<Cloud.Core.IMessageEntity<T>>>.</returns> private async Task <List <IMessageEntity <T> > > InternalReceiveBatch <T>(string subscriptionName, int batchSize) where T : class { // Ensure the receiver topic/subscription is setup first of all before trying to receive. CreateIfNotExists(); var batch = new List <IMessageEntity <T> >(); // Make the read request to Gcp PubSub. PullResponse response = await ManagementClient.PullAsync(new SubscriptionName(Config.ProjectId, subscriptionName), false, batchSize); var messages = response.ReceivedMessages; // Return empty batch if no messages where returned. if (!messages.Any()) { return(batch); } // Loop through each message and get the typed equivalent. foreach (var message in messages) { var typedContent = GetTypedMessageContent <T>(message.Message); // Keep track of the read messages so they can be completed. Messages.TryAdd(typedContent, message); var props = InternalReadProperties(message.Message); batch.Add(new PubSubMessageEntity <T> { Body = typedContent, Properties = props }); } return(batch); }
public static object PullTopicMessages(string projectId, string subscriptionId, bool acknowledge) { SubscriberClient subscriber = SubscriberClient.Create(); // [START pull_messages] SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId); PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 10); // [END pull_messages] if (response.ReceivedMessages.Count > 0) { foreach (ReceivedMessage message in response.ReceivedMessages) { Console.WriteLine($"Message {message.AckId}: " + $"{message.Message}"); } if (acknowledge) { AcknowledgeTopicMessage(projectId, subscriptionId, response); Console.WriteLine($"# of Messages received and acknowledged:" + $" {response.ReceivedMessages.Count}"); } else { Console.WriteLine($"# of Messages received:" + $" {response.ReceivedMessages.Count}"); } } return(0); }
// [END retry] private static bool IsEmptyResponse(PullResponse response) { foreach (var result in response.ReceivedMessages) { return(false); } return(true); }
public void Overview() { string projectId = _fixture.ProjectId; string topicId = _fixture.CreateTopicId(); string subscriptionId = _fixture.CreateSubscriptionId(); // Sample: Overview // First create a topic. PublisherClient publisher = PublisherClient.Create(); TopicName topicName = new TopicName(projectId, topicId); publisher.CreateTopic(topicName); // Subscribe to the topic. SubscriberClient subscriber = SubscriberClient.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 IEnumerable <T> PullOnce() { PullResponse pullResponse = PullNextBatch(_context, 100, new Selector[] {}); _context = pullResponse.EnumerationContext; if (pullResponse.Items == null) { return(new T[] {}); } return(pullResponse.Items.Items.Select(x => x.ObjectValue).Cast <T>()); }
public Result(Session session, PullResponse response) { this.Objects = response.NamedObjects.ToDictionary( pair => pair.Key, pair => session.Get(long.Parse(pair.Value))); this.Collections = response.NamedCollections.ToDictionary( pair => pair.Key, pair => pair.Value.Select(v => session.Get(long.Parse(v))).ToArray()); this.Values = response.NamedValues.ToDictionary( pair => pair.Key, pair => pair.Value); }
public PullResponse PullTopicMessage(string subscriptionId, SubscriberClient subscriber) { // [START pull_messages] SubscriptionName subscriptionName = new SubscriptionName(_projectId, subscriptionId); PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 10); // [END pull_messages] return(response); }
private void Update(PullResponse response) { this.Objects = new Indexer <SessionObject>(response.namedObjects.ToDictionary( pair => pair.Key, pair => this.Session.Get(long.Parse(pair.Value)))); this.Collections = new Indexer <SessionObject[]>(response.namedCollections.ToDictionary( pair => pair.Key, pair => pair.Value.Select(v => this.Session.Get(long.Parse(v))).ToArray())); this.Values = new Indexer <object>(response.namedValues.ToDictionary( pair => pair.Key, pair => pair.Value)); }
public static object AcknowledgeTopicMessage(string projectId, string subscriptionId, PullResponse response) { SubscriberClient subscriber = SubscriberClient.Create(); SubscriptionName subscriptionName = new SubscriptionName(projectId, subscriptionId); // [START pull_messages] subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId)); // [END pull_messages] return(0); }
public void AcknowledgeTopicMessage( string subscriptionId, SubscriberClient subscriber, PullResponse response) { SubscriptionName subscriptionName = new SubscriptionName(_projectId, subscriptionId); // [START pull_messages] subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId)); // [END pull_messages] }
public void Pull() { // Snippet: Pull(SubscriptionName,bool,int,CallSettings) // Create client SubscriberClient subscriberClient = SubscriberClient.Create(); // Initialize request argument(s) SubscriptionName subscription = new SubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); bool returnImmediately = false; int maxMessages = 0; // Make the request PullResponse response = subscriberClient.Pull(subscription, returnImmediately, maxMessages); // End snippet }
internal PullResponse Pull(EnumerationContextType context, int pageSize) { using (Message pullRequest = MessageComposer.GeneratePullMessage(context, pageSize)) { using (Message responseMessage = this.Invoke((c) => c.Pull(pullRequest))) { responseMessage.ThrowOnFault(); PullResponse pullResponseTyped = responseMessage.DeserializeMessageWithPayload <PullResponse>(); return(pullResponseTyped); } } }
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] }
//subscription pulls from the topic(queue) and send email with info gathered from the topic to the recepient. public void DownloadEmailFromQueueAndSend() { SubscriberServiceApiClient client = SubscriberServiceApiClient.Create(); Subscription s = CreateGetSubscription(); //you must getSubscription before being able to read messages from Topic/Queue PullResponse pullResponse = client.Pull(s.SubscriptionName, true, 1); //Reading the message on top (You can read more than just 1 at a time) KeyRepository kr = new KeyRepository(); if (pullResponse != null) { if (pullResponse.ReceivedMessages.Count > 0) { //extracting the first message since in the previous line it was specified to read one at a time. (Loop required to read more than 1 item(The amount set in maxMessages from the Pull method)) string encryptedData = pullResponse.ReceivedMessages[0].Message.Data.ToStringUtf8(); string decryptedData = kr.Decrypt(encryptedData); File deserialized = JsonSerializer.Deserialize <File>(decryptedData); //Deserializing since it was serialized when added to the queue string htmlString = $"<html><b>{deserialized.FileOwner}</b> is sharing the file <b>{deserialized.FileTitle}</b> with you.<br/> <a href=\"{deserialized.Link}\">Click me to access file</a> </html>"; string encryptedRecepient = pullResponse.ReceivedMessages[0].Message.Attributes["recepient"]; string recepient = kr.Decrypt(encryptedRecepient); //Dummy email was created to act as a "Company email which will send emails to users." //Send Email with deserialized. Documentation: https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8 MailMessage message = new MailMessage(); SmtpClient smtp = new SmtpClient(); message.From = new MailAddress("*****@*****.**"); message.To.Add(new MailAddress(recepient)); message.Subject = "File"; message.IsBodyHtml = true; //to make message body as html message.Body = htmlString; smtp.Port = 587; smtp.Host = "smtp.gmail.com"; //for gmail host smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("*****@*****.**", "passForpfc1234"); //go on googleaccount > Security > search for lesssecureapps > turn it to on //Note: Google will automatically turn this setting OFF lesssecureapps if it’s not being used smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(message); List <string> acksIds = new List <string>(); //after the email is sent successfully you acknolwedge the message so it is confirmed that it was processed. (Message will be removed after acknowledge.) acksIds.Add(pullResponse.ReceivedMessages[0].AckId); client.Acknowledge(s.SubscriptionName, acksIds.AsEnumerable()); } } }
public async Task PullAsync() { // Snippet: PullAsync(string,bool,int,CallSettings) // Additional: PullAsync(string,bool,int,CancellationToken) // Create client SubscriberClient subscriberClient = SubscriberClient.Create(); // Initialize request argument(s) string formattedSubscription = SubscriberClient.FormatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); bool returnImmediately = false; int maxMessages = 0; // Make the request PullResponse response = await subscriberClient.PullAsync(formattedSubscription, returnImmediately, maxMessages); // End snippet }
/// <summary> /// Pushes the messages in to redis queue. /// </summary> /// <param name="response">The response.</param> protected static void PushMessagesInToRedisQueue(PullResponse response) { foreach (Google.Cloud.PubSub.V1.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()}'"); string jsonMsg = msg.Data.ToStringUtf8(); _redisDB.ListLeftPush(PendingQueue, jsonMsg); } var size = _redisDB.ListLength("Pending"); Console.WriteLine("redis length size = " + size); }
public void Pull_RequestObject() { // Snippet: Pull(PullRequest,CallSettings) // Create client SubscriberClient subscriberClient = SubscriberClient.Create(); // Initialize request argument(s) PullRequest request = new PullRequest { SubscriptionAsSubscriptionName = new SubscriptionName("[PROJECT]", "[SUBSCRIPTION]"), MaxMessages = 0, }; // Make the request PullResponse response = subscriberClient.Pull(request); // End snippet }
public SyncRequest Diff(PullResponse response) { var ctx = new ResponseContext(this, this.AccessControlById, this.PermissionById); var syncRequest = new SyncRequest { Objects = response.Objects .Where(v => { var id = long.Parse(v[0]); this.workspaceObjectById.TryGetValue(id, out var workspaceObject); var sortedAccessControlIds = v.Length > 2 ? ctx.ReadSortedAccessControlIds(v[2]) : null; var sortedDeniedPermissionIds = v.Length > 3 ? ctx.ReadSortedDeniedPermissionIds(v[3]) : null; if (workspaceObject == null) { return(true); } var version = long.Parse(v[1]); if (!workspaceObject.Version.Equals(version)) { return(true); } if (v.Length == 2) { return(false); } if (v.Length == 3) { if (workspaceObject.SortedDeniedPermissionIds != null) { return(true); } return(!Equals(workspaceObject.SortedAccessControlIds, sortedAccessControlIds)); } return(!Equals(workspaceObject.SortedAccessControlIds, sortedAccessControlIds) || !Equals(workspaceObject.SortedDeniedPermissionIds, sortedDeniedPermissionIds)); }) .Select(v => v[0]).ToArray(), }; return(syncRequest); }
/// <summary> /// Dequeue an item /// </summary> /// <returns>item from queue</returns> public async Task <QueueMetadata> Dequeue() { PullResponse response = await this.SubscriptionClient.PullAsync(this.SubscriptionName, returnImmediately : true, maxMessages : 1); foreach (ReceivedMessage msg in response.ReceivedMessages) { string text = Encoding.UTF8.GetString(msg.Message.Data.ToArray()); this.SubscriptionClient.Acknowledge(this.SubscriptionName, response.ReceivedMessages.Select(m => m.AckId)); Console.WriteLine($"Message {msg.Message.MessageId}: {text}"); // only process a single message at a time return(JsonConvert.DeserializeObject <QueueMetadata>(text)); } return(null); }
/// <summary> /// Gets the next page of search results from the Resource Management Service /// </summary> /// <returns>An enumeration of the ResourceObjects in the page</returns> public IEnumerable <ResourceObject> GetNextPage() { PullResponse r = this.searchClient.Pull(this.context, this.PageSize); if (r.EndOfSequence != null) { this.EndOfSequence = true; } if (r.EnumerationContext != null) { this.context = r.EnumerationContext; } return(this.EnumerateResultSet(r.Items)); }
public Result(Session session, PullResponse response) { this.Workspace = session.Workspace; this.Objects = response.namedObjects.ToDictionary( pair => pair.Key, pair => session.Get(long.Parse(pair.Value)), StringComparer.OrdinalIgnoreCase); this.Collections = response.namedCollections.ToDictionary( pair => pair.Key, pair => pair.Value.Select(v => session.Get(long.Parse(v))).ToArray(), StringComparer.OrdinalIgnoreCase); this.Values = response.namedValues.ToDictionary( pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase); }
public void TestCreateTopicMessage() { string topicId = "testTopicForMessageCreation"; string subscriptionId = "testSubscriptionForMessageCreation"; string subscriptionName = SubscriberClient.FormatSubscriptionName(_projectId, subscriptionId); CreateTopic(topicId, _publisher); CreateSubscription(topicId, subscriptionId, _subscriber); CreateTopicMessage(topicId, _publisher); //Pull the Message to confirm it is valid PullResponse response = PullTopicMessage(subscriptionId, _subscriber); Assert.False(IsEmptyResponse(response)); DeleteSubscription(subscriptionId, _subscriber); DeleteTopic(topicId, _publisher); }
public SyncRequest Diff(PullResponse response) { var userSecurityHash = response.userSecurityHash; var requireLoadIds = new SyncRequest { objects = response.objects.Where(v => { var id = long.Parse(v[0]); var version = long.Parse(v[1]); WorkspaceObject workspaceObject; this.workspaceObjectById.TryGetValue(id, out workspaceObject); return(workspaceObject == null || !workspaceObject.Version.Equals(version) || !workspaceObject.UserSecurityHash.Equals(userSecurityHash)); }).Select(v => v[0]).ToArray() }; return(requireLoadIds); }
public IEnumerable <T> Pull() { bool endOfSequence = false; while (!endOfSequence) { PullResponse pullResponse = PullNextBatch(_context, 100, new Selector[] { }); if (pullResponse.Items != null) { foreach (EnumerationItem item in pullResponse.Items.Items) { yield return((T)item.ObjectValue); } } endOfSequence = pullResponse.EndOfSequence != null; _context = pullResponse.EnumerationContext; } }
// [END retry] private static bool IsEmptyResponse(PullResponse response) { foreach (var result in response.ReceivedMessages) return false; return true; }
//---------------------------------------------------------------------------- private void AcknowledgeMessages(PullResponse response) { // Acknowledge the messages so we don't see them again. var ackIds = new string[response.ReceivedMessages.Count]; for (int i = 0; i < response.ReceivedMessages.Count; ++i) { ackIds[i] = response.ReceivedMessages[i].AckId; } //==================== this._subscriberClient.Acknowledge(this._subscriptionIdFull, ackIds); //==================== }