예제 #1
0
        /// <inheritdoc />
        void IPublicationBuilder <T> .Configure(
            JustSayingBus bus,
            IAwsClientFactoryProxy proxy,
            ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger <TopicPublicationBuilder <T> >();

            logger.LogInformation("Adding SNS publisher for message type '{MessageType}'.",
                                  typeof(T));

            var config = bus.Config;

            var readConfiguration  = new SqsReadConfiguration(SubscriptionType.ToTopic);
            var writeConfiguration = new SnsWriteConfiguration();

            ConfigureWrites?.Invoke(writeConfiguration);
            readConfiguration.ApplyTopicNamingConvention <T>(config.TopicNamingConvention);

            bus.SerializationRegister.AddSerializer <T>();

            foreach (var region in config.Regions)
            {
                // TODO pass region down into topic creation for when we have foreign topics so we can generate the arn
                var eventPublisher = new SnsTopicByName(
                    readConfiguration.TopicName,
                    proxy.GetAwsClientFactory().GetSnsClient(RegionEndpoint.GetBySystemName(region)),
                    bus.SerializationRegister,
                    loggerFactory,
                    writeConfiguration,
                    config.MessageSubjectProvider)
                {
                    MessageResponseLogger = config.MessageResponseLogger
                };

                async Task StartupTask()
                {
                    if (writeConfiguration.Encryption != null)
                    {
                        await eventPublisher.CreateWithEncryptionAsync(writeConfiguration.Encryption)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await eventPublisher.CreateAsync().ConfigureAwait(false);
                    }

                    await eventPublisher.EnsurePolicyIsUpdatedAsync(config.AdditionalSubscriberAccounts)
                    .ConfigureAwait(false);
                }

                bus.AddStartupTask(StartupTask());

                bus.AddMessagePublisher <T>(eventPublisher, region);
            }

            logger.LogInformation(
                "Created SNS topic publisher on topic '{TopicName}' for message type '{MessageType}'.",
                readConfiguration.TopicName,
                typeof(T));
        }
예제 #2
0
        private IHaveFulfilledPublishRequirements AddSnsMessagePublisher <T>(Action <SnsWriteConfiguration> configBuilder) where T : Message
        {
            _log.LogInformation("Adding SNS publisher for message type '{MessageType}'.",
                                typeof(T));

            var snsWriteConfig = new SnsWriteConfiguration();

            configBuilder?.Invoke(snsWriteConfig);

            _subscriptionConfig.Topic = typeof(T).ToTopicName();
            var namingStrategy = GetNamingStrategy();

            Bus.SerializationRegister.AddSerializer <T>(_serializationFactory.GetSerializer <T>());

            var topicName = namingStrategy.GetTopicName(_subscriptionConfig.BaseTopicName, typeof(T));

            foreach (var region in Bus.Config.Regions)
            {
                // TODO pass region down into topic creation for when we have foreign topics so we can generate the arn
                var eventPublisher = new SnsTopicByName(
                    topicName,
                    _awsClientFactoryProxy.GetAwsClientFactory().GetSnsClient(RegionEndpoint.GetBySystemName(region)),
                    Bus.SerializationRegister,
                    _loggerFactory, snsWriteConfig,
                    Bus.Config.MessageSubjectProvider)
                {
                    MessageResponseLogger = Bus.Config.MessageResponseLogger
                };

                eventPublisher.CreateAsync().GetAwaiter().GetResult();

                eventPublisher.EnsurePolicyIsUpdatedAsync(Bus.Config.AdditionalSubscriberAccounts).GetAwaiter().GetResult();

                Bus.AddMessagePublisher <T>(eventPublisher, region);
            }

            _log.LogInformation("Created SNS topic publisher on topic '{TopicName}' for message type '{MessageType}'.",
                                _subscriptionConfig.Topic, typeof(T));

            return(this);
        }