public async Task <IBusSubscriber> Subscribe <T>(Func <IServiceProvider, T, object, Task> handle) where T : class { var entries = await _amazonSQSClient.ReceiveMessageAsync <T>(_conventions.Get <T>().Queue, 1); var message = entries.FirstOrDefault().Message; await handle(_serviceProvider, message, null); return(this); }
public async Task <IBusConsumer> Consume <T>(Func <IServiceProvider, IEnumerable <T>, Task <IEnumerable <T> > > handle) where T : IIdentifiable <string> { var queue = _conventions.Get <T>().Queue; int itemsToConsume = _options.ItemsToConsume; if (itemsToConsume <= 0) { throw new Exception("Items to consume is invalid, please configure a valid number."); } var entryMessages = new List <MessageEntry <T> >(itemsToConsume); IEnumerable <MessageEntry <T> > messages; do { messages = await _amazonSQSClient.ReceiveMessageAsync <T>(queue, 10); entryMessages.AddRange(messages); } while (entryMessages.Count < itemsToConsume && messages.Any()); var incomingMessages = entryMessages.Select(x => x.Message); var results = await handle(_serviceProvider, incomingMessages); var successMessageIds = new HashSet <string>(results.Select(x => x.Id)); IDictionary <string, MessageEntry <T> > entryMessageDictionary = entryMessages.ToDictionary(x => x.UniqueKey); var succeededMessages = new List <MessageEntry <T> >(successMessageIds.Count()); foreach (var id in successMessageIds) { MessageEntry <T> message = entryMessageDictionary[id]; succeededMessages.Add(message); } await _amazonSQSClient.DeleteMessagesAsync(succeededMessages, queue); return(this); }