public ServiceBusMessengerImpl(string connectionString, string[] addresses, int[] addressListenerCount, IApplicationLifetime applicationLifetime, IAMQPMessageProcessor messageProcessor)
        {
            _connectionString = connectionString;

            _addresses             = addresses;
            _addressListenerCounts = addressListenerCount;

            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken       = _cancellationTokenSource.Token;

            _messageProcessor = messageProcessor;

            applicationLifetime.ApplicationStopping.Register(OnShutdown);
        }
예제 #2
0
        public AMQP10MessengerImpl(string connectionString, string[] addresses, int[] addressListenerCount, IHostApplicationLifetime applicationLifetime, IAMQPMessageProcessor messageProcessor, ILogger <AMQP10MessengerImpl> logger)
        {
            _connectionString = connectionString;

            _addresses             = addresses;
            _addressListenerCounts = addressListenerCount;

            _connectionFactory = new ConnectionFactory();

            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken       = _cancellationTokenSource.Token;

            _messageProcessor = messageProcessor;

            _logger = logger;

            applicationLifetime.ApplicationStopping.Register(OnShutdown);
        }
예제 #3
0
        public static IServiceCollection AddAmqp(this IServiceCollection services, IConfiguration configuration)
        {
            Console.WriteLine("Initializing AMQP...");

            string implementation = configuration["Amqp:Implementation"];

            if (string.IsNullOrEmpty(implementation))
            {
                throw new Exception("AMQP implementation specification is empty");
            }

            bool useServiceBus = string.Equals(implementation, "ServiceBus");

            string connectionString = configuration["Amqp:ConnectionString"];

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new Exception("AMQP connection string is empty");
            }

            string addresses = configuration["Amqp:Addresses"];

            if (string.IsNullOrEmpty(addresses))
            {
                throw new Exception("AMQP addresses are missing");
            }

            string[] addressesSplit = addresses.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            if (addressesSplit.Length == 0)
            {
                throw new Exception("AMQP addresses are empty");
            }

            int[] amqpListenerCounts = new int[addressesSplit.Length];

            for (int i = 0; i < addressesSplit.Length; i++)
            {
                int?amqpListenerCount = configuration.GetValue <int?>($"Amqp:AddressDetails:{addressesSplit[i]}:ListenerCount");
                if (amqpListenerCount == null)
                {
                    throw new Exception($"AMQP listener count for address {addressesSplit[i]} is not defined");
                }

                amqpListenerCounts[i] = (int)amqpListenerCount;
            }

            services.AddSingleton(factory =>
            {
                IAMQPMessageProcessor messageProcessor = factory.GetRequiredService <IAMQPMessageProcessor>();

                IAMQPMessenger amqpMessenger;

                if (useServiceBus)
                {
                    // Service Bus
                    amqpMessenger = new ServiceBusMessengerImpl(connectionString, addressesSplit, amqpListenerCounts, factory.GetRequiredService <IApplicationLifetime>(), messageProcessor);
                }
                else
                {
                    // AMQP 1.0

                    amqpMessenger = new AMQP10MessengerImpl(connectionString, addressesSplit, amqpListenerCounts, factory.GetRequiredService <IApplicationLifetime>(), messageProcessor);
                }

                amqpMessenger.InitializeAsync().Wait();

                return(amqpMessenger);
            });

            Console.WriteLine("AMQP initialized successfully");

            return(services);
        }