Exemplo n.º 1
0
        /// <summary>
        ///     Called when the features is activated
        /// </summary>
        protected override void Setup(FeatureConfigurationContext context)
        {
            if (context.Settings.TryGet("ResultingSupportedStorages", out List <Type> supportedStorages))
            {
                if (!supportedStorages.Contains(typeof(StorageType.GatewayDeduplication)))
                {
                    throw new Exception("The selected persistence doesn't have support for gateway deduplication storage. Please configure one that supports gateway deduplication storage.");
                }
            }
            else
            {
                throw new Exception("No persistence configured, please configure one that supports gateway deduplication storage.");
            }

            ConfigureTransaction(context);

            var channelManager = CreateChannelManager(context.Settings);

            RegisterChannels(context, channelManager, out var channelSenderFactory, out var channelReceiverFactory);

            var gatewayInputAddress = context.Settings.GetTransportAddress(context.Settings.LogicalAddress().CreateQualifiedAddress("gateway"));

            var retryPolicy = context.Settings.Get <Func <IncomingMessage, Exception, int, TimeSpan> >("Gateway.Retries.RetryPolicy");

            var sender = new GatewayMessageSender(
                gatewayInputAddress,
                channelManager,
                new MessageNotifier(),
                context.Settings.LocalAddress(),
                GetConfigurationBasedSiteRouter(context));

            context.AddSatelliteReceiver("Gateway", gatewayInputAddress, PushRuntimeSettings.Default,
                                         (config, errorContext) => GatewayRecoverabilityPolicy.Invoke(errorContext, retryPolicy, config),
                                         (builder, messageContext) => sender.SendToDestination(messageContext, builder.Build <IDispatchMessages>(), CreateForwarder(channelSenderFactory, builder.BuildAll <IDataBus>()?.FirstOrDefault())));

            context.Pipeline.Register("RouteToGateway", new RouteToGatewayBehavior(gatewayInputAddress), "Reroutes gateway messages to the gateway");
            context.Pipeline.Register("GatewayIncomingBehavior", new GatewayIncomingBehavior(), "Extracts gateway related information from the incoming message");
            context.Pipeline.Register("GatewayOutgoingBehavior", new GatewayOutgoingBehavior(), "Puts gateway related information on the headers of outgoing messages");

            context.RegisterStartupTask(b => new GatewayReceiverStartupTask(channelManager, channelReceiverFactory, GetEndpointRouter(context), b.Build <IDispatchMessages>(), b.Build <IDeduplicateMessages>(), b.BuildAll <IDataBus>()?.FirstOrDefault(), gatewayInputAddress));
        }
        /// <summary>
        /// Called when the features is activated
        /// </summary>
        protected override void Setup(FeatureConfigurationContext context)
        {
            var storageConfiguration = context.Settings.Get <GatewayDeduplicationConfiguration>();

            storageConfiguration.Setup(context.Settings);

            var transportDefinition = context.Settings.Get <TransportDefinition>();

            ConfigureTransaction(context);

            var channelManager = CreateChannelManager(context.Settings);

            RegisterChannels(context, channelManager, out var channelSenderFactory, out var channelReceiverFactory);

            var logicalAddress      = new QueueAddress(context.Settings.EndpointQueueName(), null, null, "gateway");
            var gatewayInputAddress = transportDefinition.ToTransportAddress(logicalAddress);

            var retryPolicy = context.Settings.Get <Func <IncomingMessage, Exception, int, TimeSpan> >("Gateway.Retries.RetryPolicy");

            var sender = new GatewayMessageSender(
                gatewayInputAddress,
                channelManager,
                new MessageNotifier(),
                context.Settings.LocalAddress(),
                GetConfigurationBasedSiteRouter(context));

            context.AddSatelliteReceiver("Gateway", gatewayInputAddress, PushRuntimeSettings.Default,
                                         (config, errorContext) => GatewayRecoverabilityPolicy.Invoke(errorContext, retryPolicy, config),
                                         (builder, messageContext, cancellationToken) => sender.SendToDestination(messageContext, builder.GetRequiredService <IMessageDispatcher>(), CreateForwarder(channelSenderFactory, builder.GetServices <IDataBus>()?.FirstOrDefault()), cancellationToken));

            var configuredSitesKeys = GatewaySettings.GetConfiguredSites(context.Settings)
                                      .Select(s => s.Key)
                                      .ToList();

            context.Pipeline.Register("RouteToGateway", new RouteToGatewayBehavior(gatewayInputAddress, configuredSitesKeys), "Reroutes gateway messages to the gateway");
            context.Pipeline.Register("GatewayIncomingBehavior", new GatewayIncomingBehavior(), "Extracts gateway related information from the incoming message");
            context.Pipeline.Register("GatewayOutgoingBehavior", new GatewayOutgoingBehavior(), "Puts gateway related information on the headers of outgoing messages");

            context.RegisterStartupTask(b => new GatewayReceiverStartupTask(
                                            channelManager,
                                            channelReceiverFactory,
                                            GetEndpointRouter(context),
                                            b.GetRequiredService <IMessageDispatcher>(),
                                            storageConfiguration.CreateStorage(b),
                                            b.GetServices <IDataBus>()?.FirstOrDefault(),
                                            gatewayInputAddress,
                                            transportDefinition.TransportTransactionMode));
        }