Exemple #1
0
        private JustSayingBus CreateBus(IMessagingConfig config, ILoggerFactory loggerFactory)
        {
            IMessageSerializationRegister register =
                ServicesBuilder?.SerializationRegister?.Invoke() ?? ServiceResolver.ResolveService <IMessageSerializationRegister>();

            return(new JustSayingBus(config, register, loggerFactory));
        }
Exemple #2
0
    private static ISqsQueue TestQueue(
        IMessageSerializationRegister messageSerializationRegister,
        string queueName,
        Action spy = null)
    {
        var message = new TestJustSayingMessage
        {
            QueueName = queueName,
        };

        var messages = new List <Message>
        {
            new TestMessage {
                Body = messageSerializationRegister.Serialize(message, false)
            },
        };

        var queue = new FakeSqsQueue(async ct =>
        {
            spy?.Invoke();
            await Task.Delay(30, ct);
            return(messages);
        }, queueName);

        return(queue);
    }
        public async Task <SqsQueueByName> EnsureTopicExistsWithQueueSubscribedAsync(
            string region,
            IMessageSerializationRegister serializationRegister,
            SqsReadConfiguration queueConfig,
            IMessageSubjectProvider messageSubjectProvider)
        {
            var regionEndpoint = RegionEndpoint.GetBySystemName(region);
            var sqsClient      = _awsClientFactory.GetAwsClientFactory().GetSqsClient(regionEndpoint);
            var snsClient      = _awsClientFactory.GetAwsClientFactory().GetSnsClient(regionEndpoint);

            var queue = await EnsureQueueExistsAsync(region, queueConfig).ConfigureAwait(false);

            if (TopicExistsInAnotherAccount(queueConfig))
            {
                var arnProvider = new ForeignTopicArnProvider(regionEndpoint, queueConfig.TopicSourceAccount, queueConfig.PublishEndpoint);

                var topicArn = await arnProvider.GetArnAsync().ConfigureAwait(false);
                await SubscribeQueueAndApplyFilterPolicyAsync(snsClient, topicArn, sqsClient, queue.Uri, queueConfig.FilterPolicy).ConfigureAwait(false);
            }
            else
            {
                var eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint, snsClient, serializationRegister, _loggerFactory, messageSubjectProvider);
                await eventTopic.CreateAsync().ConfigureAwait(false);

                await SubscribeQueueAndApplyFilterPolicyAsync(snsClient, eventTopic.Arn, sqsClient, queue.Uri, queueConfig.FilterPolicy).ConfigureAwait(false);

                await SqsPolicy.SaveAsync(eventTopic.Arn, queue.Arn, queue.Uri, sqsClient).ConfigureAwait(false);
            }

            return(queue);
        }
Exemple #4
0
        public SqsNotificationListener(
            SqsQueueBase queue,
            IMessageSerializationRegister serializationRegister,
            IMessageMonitor messagingMonitor,
            ILoggerFactory loggerFactory,
            IMessageContextAccessor messageContextAccessor,
            Action <Exception, Amazon.SQS.Model.Message> onError = null,
            IMessageLockAsync messageLock = null,
            IMessageBackoffStrategy messageBackoffStrategy = null)
        {
            _queue            = queue;
            _messagingMonitor = messagingMonitor;
            onError           = onError ?? DefaultErrorHandler;
            _log = loggerFactory.CreateLogger("JustSaying");

            _messageProcessingStrategy = new DefaultThrottledThroughput(_messagingMonitor);
            _messageHandlerWrapper     = new MessageHandlerWrapper(messageLock, _messagingMonitor);

            _messageDispatcher = new MessageDispatcher(
                _queue,
                serializationRegister,
                messagingMonitor,
                onError,
                _handlerMap,
                loggerFactory,
                messageBackoffStrategy,
                messageContextAccessor);

            Subscribers = new Collection <ISubscriber>();

            if (messageBackoffStrategy != null)
            {
                _requestMessageAttributeNames.Add(MessageSystemAttributeName.ApproximateReceiveCount);
            }
        }
Exemple #5
0
 public SqsMessagePublisher(
     Uri queueUrl,
     IAmazonSQS client,
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory) : this(client, serializationRegister, loggerFactory)
 {
     QueueUrl = queueUrl;
 }
        private JustSayingBus CreateBus(IMessagingConfig config, ILoggerFactory loggerFactory)
        {
            IMessageSerializationRegister register = ServiceResolver.ResolveService <IMessageSerializationRegister>();
            IMessageMonitor monitor = ServiceResolver.ResolveOptionalService <IMessageMonitor>() ?? new NullOpMessageMonitor();

            var bus = new JustSayingBus(config, register, loggerFactory, monitor);

            return(bus);
        }
Exemple #7
0
 public SqsMessagePublisher(
     IAmazonSQS client,
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory)
 {
     _client = client;
     _serializationRegister = serializationRegister;
     _logger = loggerFactory.CreateLogger("JustSaying.Publish");
 }
 protected SnsTopicBase(
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory,
     IMessageSubjectProvider messageSubjectProvider)
 {
     _serializationRegister  = serializationRegister;
     _messageSubjectProvider = messageSubjectProvider;
     _logger = loggerFactory.CreateLogger("JustSaying");
 }
        public QueueWithAsyncStartup <SqsQueueByName> EnsureTopicExistsWithQueueSubscribed(
            string region,
            IMessageSerializationRegister serializationRegister,
            SqsReadConfiguration queueConfig,
            IMessageSubjectProvider messageSubjectProvider)
        {
            var regionEndpoint = RegionEndpoint.GetBySystemName(region);
            var sqsClient      = _awsClientFactory.GetAwsClientFactory().GetSqsClient(regionEndpoint);
            var snsClient      = _awsClientFactory.GetAwsClientFactory().GetSnsClient(regionEndpoint);

            var queueWithStartup = EnsureQueueExists(region, queueConfig);

            async Task StartupTask()
            {
                await queueWithStartup.StartupTask.ConfigureAwait(false);

                var queue = queueWithStartup.Queue;

                if (TopicExistsInAnotherAccount(queueConfig))
                {
                    var arnProvider = new ForeignTopicArnProvider(regionEndpoint,
                                                                  queueConfig.TopicSourceAccount,
                                                                  queueConfig.PublishEndpoint);

                    var topicArn = await arnProvider.GetArnAsync().ConfigureAwait(false);
                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  topicArn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);
                }
                else
                {
                    var eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint,
                                                        snsClient,
                                                        serializationRegister,
                                                        _loggerFactory,
                                                        messageSubjectProvider);
                    await eventTopic.CreateAsync().ConfigureAwait(false);

                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  eventTopic.Arn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);

                    await SqsPolicy
                    .SaveAsync(eventTopic.Arn, queue.Arn, queue.Uri, sqsClient)
                    .ConfigureAwait(false);
                }
            }

            // This StartupTask is intentionally not awaited, as it will be run when the bus is started.
            return(new QueueWithAsyncStartup <SqsQueueByName>(StartupTask(), queueWithStartup.Queue));
        }
 public MessageDispatcher(
     IMessageSerializationRegister serializationRegister,
     IMessageMonitor messagingMonitor,
     MiddlewareMap middlewareMap,
     ILoggerFactory loggerFactory)
 {
     _serializationRegister = serializationRegister;
     _messagingMonitor      = messagingMonitor;
     _middlewareMap         = middlewareMap;
     _logger = loggerFactory.CreateLogger("JustSaying");
 }
 protected SnsTopicBase(
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory,
     SnsWriteConfiguration snsWriteConfiguration,
     IMessageSubjectProvider messageSubjectProvider)
 {
     _serializationRegister = serializationRegister;
     _logger = loggerFactory.CreateLogger("JustSaying");
     _snsWriteConfiguration  = snsWriteConfiguration;
     _messageSubjectProvider = messageSubjectProvider;
 }
Exemple #12
0
 public SnsMessagePublisher(
     string topicArn,
     IAmazonSimpleNotificationService client,
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory,
     IMessageSubjectProvider messageSubjectProvider,
     Func <Exception, Message, bool> handleException = null)
     : this(client, serializationRegister, loggerFactory, messageSubjectProvider, handleException)
 {
     Arn = topicArn;
 }
 public SqsPublisher(
     RegionEndpoint region,
     string queueName,
     IAmazonSQS client,
     int retryCountBeforeSendingToErrorQueue,
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory)
     : base(region, queueName, client, retryCountBeforeSendingToErrorQueue, loggerFactory)
 {
     _client = client;
     _serializationRegister = serializationRegister;
 }
        private JustSayingBus CreateBus(IMessagingConfig config, ILoggerFactory loggerFactory)
        {
            IMessageSerializationRegister register =
                ServicesBuilder?.SerializationRegister?.Invoke() ?? ServiceResolver.ResolveService <IMessageSerializationRegister>();

            var bus = new JustSayingBus(config, register, loggerFactory);

            bus.Monitor = CreateMessageMonitor();
            bus.MessageContextAccessor = CreateMessageContextAccessor();

            return(bus);
        }
 public SnsTopicByName(
     string topicName,
     IAmazonSimpleNotificationService client,
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory,
     IMessageSubjectProvider messageSubjectProvider)
     : base(serializationRegister, loggerFactory, messageSubjectProvider)
 {
     TopicName = topicName;
     Client    = client;
     _log      = loggerFactory.CreateLogger("JustSaying");
 }
Exemple #16
0
 public SnsMessagePublisher(
     IAmazonSimpleNotificationService client,
     IMessageSerializationRegister serializationRegister,
     ILoggerFactory loggerFactory,
     IMessageSubjectProvider messageSubjectProvider,
     Func <Exception, Message, bool> handleException = null)
 {
     Client = client;
     _serializationRegister = serializationRegister;
     _logger                 = loggerFactory.CreateLogger("JustSaying.Publish");
     _handleException        = handleException;
     _messageSubjectProvider = messageSubjectProvider;
 }
        public JustSayingBus(IMessagingConfig config, IMessageSerializationRegister serializationRegister, ILoggerFactory loggerFactory)
        {
            _log = loggerFactory.CreateLogger("JustSaying");

            Config  = config;
            Monitor = new NullOpMessageMonitor();
            MessageContextAccessor = new MessageContextAccessor();

            _subscribersByRegionAndQueue = new Dictionary <string, Dictionary <string, INotificationSubscriber> >();
            _publishersByRegionAndTopic  = new Dictionary <string, Dictionary <string, IMessagePublisher> >();
            SerializationRegister        = serializationRegister;
            _publishers  = new HashSet <IPublisher>();
            _subscribers = new HashSet <ISubscriber>();
        }
 public MessageDispatcher(
     IMessageSerializationRegister serializationRegister,
     IMessageMonitor messagingMonitor,
     HandlerMap handlerMap,
     ILoggerFactory loggerFactory,
     IMessageBackoffStrategy messageBackoffStrategy,
     IMessageContextAccessor messageContextAccessor)
 {
     _serializationRegister = serializationRegister;
     _messagingMonitor      = messagingMonitor;
     _handlerMap            = handlerMap;
     _logger = loggerFactory.CreateLogger("JustSaying");
     _messageBackoffStrategy = messageBackoffStrategy;
     _messageContextAccessor = messageContextAccessor;
 }
 public MessageDispatcher(
     SqsQueueBase queue,
     IMessageSerializationRegister serializationRegister,
     IMessageMonitor messagingMonitor,
     Action <Exception, SQSMessage> onError,
     HandlerMap handlerMap,
     ILoggerFactory loggerFactory,
     IMessageBackoffStrategy messageBackoffStrategy,
     IMessageContextAccessor messageContextAccessor)
 {
     _queue = queue;
     _serializationRegister = serializationRegister;
     _messagingMonitor      = messagingMonitor;
     _onError                = onError;
     _handlerMap             = handlerMap;
     _logger                 = loggerFactory.CreateLogger("JustSaying");
     _messageBackoffStrategy = messageBackoffStrategy;
     _messageContextAccessor = messageContextAccessor;
 }
        private static ISqsQueue TestQueue(
            IMessageSerializationRegister messageSerializationRegister,
            string queueName,
            Action spy = null)
        {
            async Task <ReceiveMessageResponse> GetMessages()
            {
                spy?.Invoke();
                await Task.Delay(30);

                var message = new TestJustSayingMessage
                {
                    QueueName = queueName,
                };

                var messages = new List <Message>
                {
                    new TestMessage {
                        Body = messageSerializationRegister.Serialize(message, false)
                    },
                };

                return(new ReceiveMessageResponse {
                    Messages = messages
                });
            }

            IAmazonSQS sqsClientMock = Substitute.For <IAmazonSQS>();

            sqsClientMock
            .ReceiveMessageAsync(Arg.Any <ReceiveMessageRequest>(), Arg.Any <CancellationToken>())
            .Returns(_ => GetMessages());

            ISqsQueue sqsQueueMock = Substitute.For <ISqsQueue>();

            sqsQueueMock.Uri.Returns(new Uri("http://test.com"));
            sqsQueueMock.Client.Returns(sqsClientMock);
            sqsQueueMock.QueueName.Returns(queueName);
            sqsQueueMock.Uri.Returns(new Uri("http://foo.com"));

            return(sqsQueueMock);
        }
        public JustSayingBus(
            IMessagingConfig config,
            IMessageSerializationRegister serializationRegister,
            ILoggerFactory loggerFactory)
        {
            _loggerFactory = loggerFactory;
            _startupTasks  = new List <Func <Task> >();
            _log           = _loggerFactory.CreateLogger("JustSaying");

            Config  = config;
            Monitor = new NullOpMessageMonitor();
            MessageContextAccessor = new MessageContextAccessor();
            SerializationRegister  = serializationRegister;
            MiddlewareMap          = new MiddlewareMap();

            _publishersByType          = new Dictionary <Type, IMessagePublisher>();
            _subscriptionGroupSettings =
                new ConcurrentDictionary <string, SubscriptionGroupConfigBuilder>(StringComparer.Ordinal);
            _defaultSubscriptionGroupSettings = new SubscriptionGroupSettingsBuilder();
        }
Exemple #22
0
    public JustSayingBus(
        IMessagingConfig config,
        IMessageSerializationRegister serializationRegister,
        ILoggerFactory loggerFactory,
        IMessageMonitor monitor)
    {
        _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
        _monitor       = monitor ?? throw new ArgumentNullException(nameof(monitor));

        _startupTasks = new List <Func <CancellationToken, Task> >();
        _log          = _loggerFactory.CreateLogger("JustSaying");

        Config = config;
        SerializationRegister = serializationRegister;
        MiddlewareMap         = new MiddlewareMap();

        _publishersByType          = new Dictionary <Type, IMessagePublisher>();
        _subscriptionGroupSettings =
            new ConcurrentDictionary <string, SubscriptionGroupConfigBuilder>(StringComparer.Ordinal);
        _defaultSubscriptionGroupSettings = new SubscriptionGroupSettingsBuilder();
    }
Exemple #23
0
        protected virtual void Given()
        {
            LoggerFactory         = new LoggerFactory();
            Sqs                   = Substitute.For <IAmazonSQS>();
            SerializationRegister = Substitute.For <IMessageSerializationRegister>();
            Monitor               = Substitute.For <IMessageMonitor>();
            Handler               = Substitute.For <IHandlerAsync <SimpleMessage> >();
            LoggerFactory         = Substitute.For <ILoggerFactory>();

            var response = GenerateResponseMessage(MessageTypeString, Guid.NewGuid());

            Sqs.ReceiveMessageAsync(
                Arg.Any <ReceiveMessageRequest>(),
                Arg.Any <CancellationToken>())
            .Returns(
                x => Task.FromResult(response),
                x => Task.FromResult(new ReceiveMessageResponse()));

            DeserializedMessage = new SimpleMessage {
                RaisingComponent = "Component"
            };
            SerializationRegister.DeserializeMessage(Arg.Any <string>()).Returns(DeserializedMessage);
        }
 public TopicAddressPublisher(IAmazonSimpleNotificationService snsClient, ILoggerFactory loggerFactory, IMessageSubjectProvider subjectProvider, IMessageSerializationRegister serializationRegister, Func <Exception, Message, bool> handleException, TopicAddress topicAddress)
     : base(topicAddress.TopicArn, snsClient, serializationRegister, loggerFactory, subjectProvider, handleException)
 {
 }
        public QueueWithAsyncStartup EnsureTopicExistsWithQueueSubscribed(
            string region,
            IMessageSerializationRegister serializationRegister,
            SqsReadConfiguration queueConfig,
            IMessageSubjectProvider messageSubjectProvider)
        {
            var regionEndpoint = RegionEndpoint.GetBySystemName(region);
            var sqsClient      = _awsClientFactory.GetAwsClientFactory().GetSqsClient(regionEndpoint);
            var snsClient      = _awsClientFactory.GetAwsClientFactory().GetSnsClient(regionEndpoint);

            var queueWithStartup = EnsureQueueExists(region, queueConfig);

            async Task StartupTask()
            {
                await queueWithStartup.StartupTask.Invoke().ConfigureAwait(false);

                var queue = queueWithStartup.Queue;

                if (TopicExistsInAnotherAccount(queueConfig))
                {
                    var arnProvider = new ForeignTopicArnProvider(regionEndpoint,
                                                                  queueConfig.TopicSourceAccount,
                                                                  queueConfig.PublishEndpoint);

                    var topicArn = await arnProvider.GetArnAsync().ConfigureAwait(false);
                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  topicArn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);
                }
                else
                {
#pragma warning disable 618
                    var eventTopic = new SnsTopicByName(queueConfig.PublishEndpoint,
                                                        snsClient,
                                                        serializationRegister,
                                                        _loggerFactory,
                                                        messageSubjectProvider);
#pragma warning restore 618
                    await eventTopic.CreateAsync().ConfigureAwait(false);

                    await SubscribeQueueAndApplyFilterPolicyAsync(snsClient,
                                                                  eventTopic.Arn,
                                                                  sqsClient,
                                                                  queue.Uri,
                                                                  queueConfig.FilterPolicy).ConfigureAwait(false);

                    var sqsDetails = new SqsPolicyDetails
                    {
                        SourceArn = eventTopic.Arn,
                        QueueArn  = queue.Arn,
                        QueueUri  = queue.Uri
                    };
                    await SqsPolicy
                    .SaveAsync(sqsDetails, sqsClient)
                    .ConfigureAwait(false);
                }
            }

            return(new QueueWithAsyncStartup(StartupTask, queueWithStartup.Queue));
        }