コード例 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            //Configs
            services.AddScoped <IEventRepository>(s => new SQLEventRepository(Configuration.GetConnectionString("LocalEventDb")));
            services.AddScoped <IEventsManager>(s => new LogEventsManager(new SQLEventRepository(Configuration.GetConnectionString("LocalEventDb"))));
            services.AddScoped <IEventCreator, PlainEventCreator>();

            services.AddSingleton(Configuration.GetSection("RabbitMQConnectionSettings").Get <RabbitMQConnectionSettings>());
            services.AddSingleton <IConnectionFactory, ConnectionFactory>(sp =>
            {
                return(RabbitMQPersistentConnection.CreateConnectionFactory(sp.GetService <RabbitMQConnectionSettings>()));
            });
            services.AddSingleton <IRabbitMQPersistentConnection, RabbitMQPersistentConnection>();

            services.AddSingleton <IEventsReceiver, RabbitEventsReceiver>();

            services.Configure <FormOptions>(o =>
            {
                o.ValueLengthLimit         = int.MaxValue;
                o.MultipartBodyLengthLimit = int.MaxValue;
                o.MemoryBufferThreshold    = int.MaxValue;
            });
        }
コード例 #2
0
        public static IApplicationBuilder UseRabbitListener(this IApplicationBuilder app)
        {
            Listener = app.ApplicationServices.GetService <RabbitMQPersistentConnection>();
            var life = app.ApplicationServices.GetService <IApplicationLifetime>();

            life.ApplicationStarted.Register(OnStarted);

            life.ApplicationStopping.Register(OnStopping);
            return(app);
        }
コード例 #3
0
        public static IApplicationBuilder UseRabbitListener(this IApplicationBuilder app)
        {
            Listener = app.ApplicationServices.GetService <RabbitMQPersistentConnection>();
            var life = app.ApplicationServices.GetService <IApplicationLifetime>();

            life.ApplicationStarted.Register(OnStarted);

            //press Ctrl+C to reproduce if your app runs in Kestrel as a console app
            life.ApplicationStopping.Register(OnStopping);
            return(app);
        }
コード例 #4
0
 public MQ_ServerController(RabbitMQPersistentConnection mqConnection)
 {
     this._mqConnection = mqConnection;
 }
コード例 #5
0
        public static IFlashHostBuilder AddRabbitMQ(this IFlashHostBuilder hostBuilder, Action <RabbitMQOption> setup)
        {
            setup = setup ?? throw new ArgumentNullException(nameof(setup));

            var option = new RabbitMQOption();

            setup(option);

            hostBuilder.Services.AddSingleton <IConnectionFactory>(sp =>
            {
                var logger = sp.GetRequiredService <ILogger <IConnectionFactory> >();

                var factory         = new ConnectionFactory();
                factory.HostName    = option.HostName;
                factory.Port        = option.Port;
                factory.Password    = option.Password;
                factory.UserName    = option.UserName;
                factory.VirtualHost = option.VirtualHost;
                factory.AutomaticRecoveryEnabled  = true;
                factory.TopologyRecoveryEnabled   = true;
                factory.UseBackgroundThreadsForIO = true;
                return(factory);
            });

            hostBuilder.Services.AddSingleton <ILoadBalancerFactory <IRabbitMQPersistentConnection> >(sp =>
            {
                return(new DefaultLoadBalancerFactory <IRabbitMQPersistentConnection>());
            });

            hostBuilder.Services.AddSingleton <IBus, RabbitMQBus>(sp =>
            {
                var logger           = sp.GetRequiredService <ILogger <IBus> >();
                var loggerConnection = sp.GetRequiredService <ILogger <IRabbitMQPersistentConnection> >();
                var rabbitMQPersisterConnectionLoadBalancerFactory = sp.GetRequiredService <ILoadBalancerFactory <IRabbitMQPersistentConnection> >();
                var connectionFactory  = sp.GetRequiredService <IConnectionFactory>();
                var senderConnections  = new List <IRabbitMQPersistentConnection>();
                var receiveConnections = new List <IRabbitMQPersistentConnection>();

                //消费端连接池
                for (int i = 0; i < option.ReceiverMaxConnections; i++)
                {
                    var connection = new RabbitMQPersistentConnection(connectionFactory, loggerConnection, option.ReceiverAcquireRetryAttempts);
                    connection.TryConnect();
                    //消费端的连接池
                    receiveConnections.Add(connection);
                }

                //发送端连接池
                for (int i = 0; i < option.SenderMaxConnections; i++)
                {
                    var connection = new RabbitMQPersistentConnection(connectionFactory, loggerConnection, option.SenderAcquireRetryAttempts);
                    connection.TryConnect();
                    senderConnections.Add(connection);
                }

                var receiveLoadBlancer = rabbitMQPersisterConnectionLoadBalancerFactory.Resolve(() => receiveConnections, option.ReceiverLoadBalancer);
                var senderLoadBlancer  = rabbitMQPersisterConnectionLoadBalancerFactory.Resolve(() => senderConnections, option.SenderLoadBalancer);

                return(new RabbitMQBus(
                           receiveLoadBlancer,
                           senderLoadBlancer,
                           sp,
                           logger,
                           option.ReveiverMaxDegreeOfParallelism,
                           option.ReceiverAcquireRetryAttempts,
                           option.ReceiverHandlerTimeoutMillseconds,
                           option.SenderAcquireRetryAttempts,
                           option.PreFetch,
                           option.Exchange,
                           option.ExchangeType
                           ));
            });

            return(hostBuilder);
        }