Esempio n. 1
0
        public async Task PublishAsync <T>(T message, string messageId          = null, string correlationId  = null,
                                           string spanContext                   = null, object messageContext = null,
                                           IDictionary <string, object> headers = null) where T : class
        {
            var entry = new MessageEntry <T>(message);

            string queue = _conventions.Get <T>().Queue;

            await _amazonSQSClient.EnqueueAsync(entry, queue);
        }
Esempio n. 2
0
        public Task PublishAsync <T>(T message, string messageId = null, string correlationId = null,
                                     object context = null) where T : class
        {
            _client.Send(message, _conventionsProvider.Get <T>(), messageId, correlationId, context);

            return(Task.CompletedTask);
        }
Esempio n. 3
0
 public async Task PublishAsync <T>(T message,
                                    string messageId   = null, string correlationId  = null,
                                    string spanContext = null, object messageContext = null,
                                    IDictionary <string, object> headers = null) where T : class
 {
     await _client.Send(message, _conventionsProvider.Get(message.GetType()), messageId, correlationId, spanContext, messageContext, headers);
 }
Esempio n. 4
0
        public Task PublishAsync <T>(T message, string messageId = null, string correlationId  = null,
                                     string spanContext          = null, object messageContext = null, IDictionary <string, object> headers = null,
                                     string userId = null)
            where T : class
        {
            _client.Send(message, _conventionsProvider.Get(message.GetType()), messageId, correlationId, spanContext,
                         messageContext, headers, userId);

            return(Task.CompletedTask);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        public Task PublishAsync <T>(T message, string messageId     = null, string correlationId  = null,
                                     string spanContext              = null, object messageContext = null, IDictionary <string, object> headers = null,
                                     IConventions messageConventions = null)
            where T : class
        {
            var _spanContextHeader = _options.GetSpanContextHeader();
            var _contextHeader     = _options.GetContextHeader();

            var _loggerEnabled  = _options.Logger?.Enabled ?? true;
            var _contextEnabled = _options.Context?.Enabled ?? true;

            return(_busClient.PublishAsync(message, ctx =>
                                           ctx.UsePublishConfiguration(cfg => cfg.WithProperties(properties =>
            {
                properties.MessageId = string.IsNullOrWhiteSpace(messageId)
                            ? Guid.NewGuid().ToString("N")
                            : messageId;
                properties.CorrelationId = string.IsNullOrWhiteSpace(correlationId)
                            ? Guid.NewGuid().ToString("N")
                            : correlationId;
                properties.Timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeSeconds());
                properties.Headers = new Dictionary <string, object>();

                if (!string.IsNullOrWhiteSpace(spanContext))
                {
                    properties.Headers.Add(_spanContextHeader, spanContext);
                }

                if (_contextEnabled)
                {
                    // add context to header
                    IncludeMessageContext(messageContext, properties);
                }

                if (headers is { })
                {
                    foreach (var(key, value) in headers)
                    {
                        if (string.IsNullOrWhiteSpace(key) || value is null)
                        {
                            continue;
                        }

                        properties.Headers.TryAdd(key, value);
                    }
                }

                var conventions = messageConventions ?? _conventionsProvider.Get <T>();

                if (messageConventions is { })
Esempio n. 7
0
        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);
        }