示例#1
0
        /// <inheritdoc />
        void ISubscriptionBuilder <T> .Configure(
            JustSayingBus bus,
            IHandlerResolver resolver,
            IVerifyAmazonQueues creator,
            ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger <TopicSubscriptionBuilder <T> >();

            var subscriptionConfig = new SqsReadConfiguration(SubscriptionType.ToTopic)
            {
                QueueName = TopicName
            };

            ConfigureReads?.Invoke(subscriptionConfig);

            var config = bus.Config;

            subscriptionConfig.ApplyTopicNamingConvention <T>(config.TopicNamingConvention);
            subscriptionConfig.ApplyQueueNamingConvention <T>(config.QueueNamingConvention);
            subscriptionConfig.SubscriptionGroupName ??= subscriptionConfig.QueueName;
            subscriptionConfig.PublishEndpoint = subscriptionConfig.TopicName;
            subscriptionConfig.Validate();

            var queueWithStartup = creator.EnsureTopicExistsWithQueueSubscribed(
                config.Region,
                bus.SerializationRegister,
                subscriptionConfig,
                config.MessageSubjectProvider);

            bus.AddStartupTask(queueWithStartup.StartupTask);

            bus.AddQueue(subscriptionConfig.SubscriptionGroupName, queueWithStartup.Queue);

            logger.LogInformation(
                "Created SQS topic subscription on topic '{TopicName}' and queue '{QueueName}'.",
                subscriptionConfig.TopicName,
                subscriptionConfig.QueueName);

            var resolutionContext = new HandlerResolutionContext(subscriptionConfig.QueueName);
            var proposedHandler   = resolver.ResolveHandler <T>(resolutionContext);

            if (proposedHandler == null)
            {
                throw new HandlerNotRegisteredWithContainerException(
                          $"There is no handler for '{typeof(T)}' messages.");
            }

            bus.AddMessageHandler(subscriptionConfig.QueueName,
                                  () => resolver.ResolveHandler <T>(resolutionContext));

            logger.LogInformation(
                "Added a message handler for message type for '{MessageType}' on topic '{TopicName}' and queue '{QueueName}'.",
                typeof(T),
                subscriptionConfig.TopicName,
                subscriptionConfig.QueueName);
        }
        /// <inheritdoc />
        void ISubscriptionBuilder <T> .Configure(
            JustSayingBus bus,
            IHandlerResolver resolver,
            IVerifyAmazonQueues creator,
            ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger <QueueSubscriptionBuilder <T> >();

            var subscriptionConfig = new SqsReadConfiguration(SubscriptionType.PointToPoint)
            {
                QueueName = QueueName
            };

            ConfigureReads?.Invoke(subscriptionConfig);

            var config = bus.Config;

            subscriptionConfig.ApplyTopicNamingConvention <T>(config.TopicNamingConvention);
            subscriptionConfig.ApplyQueueNamingConvention <T>(config.QueueNamingConvention);
            subscriptionConfig.SubscriptionGroupName ??= subscriptionConfig.QueueName;
            subscriptionConfig.Validate();

            var queue = creator.EnsureQueueExists(config.Region, subscriptionConfig);

            bus.AddStartupTask(
                queue.Queue.EnsureQueueAndErrorQueueExistAndAllAttributesAreUpdatedAsync(subscriptionConfig));

            bus.AddQueue(subscriptionConfig.SubscriptionGroupName, queue.Queue);

            logger.LogInformation(
                "Created SQS subscriber for message type '{MessageType}' on queue '{QueueName}'.",
                typeof(T),
                subscriptionConfig.QueueName);

            var resolutionContext = new HandlerResolutionContext(subscriptionConfig.QueueName);
            var proposedHandler   = resolver.ResolveHandler <T>(resolutionContext);

            if (proposedHandler == null)
            {
                throw new HandlerNotRegisteredWithContainerException(
                          $"There is no handler for '{typeof(T)}' messages.");
            }

            bus.AddMessageHandler(subscriptionConfig.QueueName,
                                  () => resolver.ResolveHandler <T>(resolutionContext));

            logger.LogInformation(
                "Added a message handler for message type for '{MessageType}' on topic '{TopicName}' and queue '{QueueName}'.",
                typeof(T),
                subscriptionConfig.TopicName,
                subscriptionConfig.QueueName);
        }