Example #1
0
        private void ConfigureRabbitMq(IServiceCollection services)
        {
            var config = new RabbitMqConfig();

            Configuration.GetSection("RabbitMq").Bind(config);
            var factory = RabbitMqFactory.Create(config);

            var publishers = new Dictionary <string, IPublisher>();

            publishers.Add(Queues.APIGateway, factory.CreatePublisher(Queues.APIGateway));
            publishers.Add(Queues.Transactions, factory.CreatePublisher(Queues.Transactions));
            var publishingRouter = new PublishingRouter(publishers);

            services.AddSingleton(new PublishingRouter(publishers));

            var servicesProvider = services.BuildServiceProvider();
            var logger           = servicesProvider.GetService <ILogger <IConsumer> >();
            var paymentsService  = servicesProvider.GetService <PaymentsService>();

            var consumingRouter = ConsumingRouter <PaymentsService> .Create(paymentsService, publishingRouter, "Payments", logger);

            var consumer = factory.CreateConsumer(Queues.Payments);

            consumingRouter.LinkConsumer(consumer);
            services.AddSingleton(consumingRouter);
        }
        private void ConfigureRabbitMq(IServiceCollection services, ServiceProvider loggerServicesProvier)
        {
            var config = new RabbitMqConfig();

            Configuration.GetSection("RabbitMq").Bind(config);
            var factory = RabbitMqFactory.Create(config);

            var cardsConsumer = factory.CreateConsumer(Queues.Cards);
            var awaiter       = new EventsAwaiter("Cards", loggerServicesProvier.GetService <ILogger <EventsAwaiter> >());

            awaiter.BindConsumer(cardsConsumer);
            services.AddSingleton(awaiter);

            var publishers = new Dictionary <string, IPublisher>();

            publishers.Add(Queues.APIGateway, factory.CreatePublisher(Queues.APIGateway));
            publishers.Add(Queues.Accounts, factory.CreatePublisher(Queues.Accounts));
            publishers.Add(Queues.Transactions, factory.CreatePublisher(Queues.Transactions));
            var publishingRouter = new PublishingRouter(publishers);

            services.AddSingleton(publishingRouter);

            var servicesProvider = services.BuildServiceProvider();
            var cardsService     = servicesProvider.GetService <CardsService>();

            var consumingRouter = ConsumingRouter <CardsService> .Create(cardsService, publishingRouter, "Cards", loggerServicesProvier.GetService <ILogger <IConsumer> >());

            consumingRouter.LinkConsumer(cardsConsumer);
            services.AddSingleton(consumingRouter);
        }
 public ProducerCompoment()
 {
     RabbitMqFactory.Start();
     RabbitMqFactory.GetChannel(ConfigHelper.QueueId);
     this.tr           = new Timer();
     this.tr.Interval  = ConfigHelper.ProducerInterval * 1000;
     this.tr.AutoReset = true;
     this.tr.Elapsed  += TimerElapsed;
 }
Example #4
0
        private void ConfigureRabbitMq(IServiceCollection services, ServiceProvider loggerServicesProvier)
        {
            var config = new RabbitMqConfig();

            Configuration.GetSection("RabbitMq").Bind(config);
            var rabbitMqFactory = RabbitMqFactory.Create(config);

            AddAwaiter(services, rabbitMqFactory, loggerServicesProvier);
            AddPublishing(services, rabbitMqFactory);
        }
Example #5
0
        private void AddAwaiter(IServiceCollection services, RabbitMqFactory factory, ServiceProvider loggerServicesProvier)
        {
            var awaiter = new EventsAwaiter("APIGateway", loggerServicesProvier.GetService <ILogger <EventsAwaiter> >());

            var consumer = factory.CreateConsumer(Queues.APIGateway);

            awaiter.BindConsumer(consumer);

            services.AddSingleton(awaiter);
        }
        public ConsumerCompoment()
        {
            RabbitMqFactory.Start();

            var consumer = new RabbitMqConsumer(
                ConfigHelper.SubQueueNames,
                new PubSubDispatcher <RabbitMqEventStream>(AutoFacConfig.Container),
                ConfigHelper.QueueId);

            consumer.Register();
        }
Example #7
0
        private PublishingRouter AddPublishing(IServiceCollection services, RabbitMqFactory factory)
        {
            //TODO przepisac na builder
            var publishers = new Dictionary <string, IPublisher>();

            publishers.Add(Queues.Accounts, factory.CreatePublisher(Queues.Accounts));
            publishers.Add(Queues.Transactions, factory.CreatePublisher(Queues.Transactions));
            publishers.Add(Queues.Cards, factory.CreatePublisher(Queues.Cards));
            publishers.Add(Queues.Loans, factory.CreatePublisher(Queues.Loans));
            publishers.Add(Queues.Payments, factory.CreatePublisher(Queues.Payments));
            publishers.Add(Queues.Users, factory.CreatePublisher(Queues.Users));
            var publishingRouter = new PublishingRouter(publishers);

            services.AddSingleton(publishingRouter);
            return(publishingRouter);
        }
Example #8
0
 /// <summary>
 /// 每次重启时,清空队列
 /// </summary>
 public static void DeleteQueue()
 {
     using (IConnection conn = RabbitMqFactory.CreateConnection())
     {
         using (IModel channel = conn.CreateModel())
         {
             try
             {
                 channel.QueueDelete(QueueName, true, false);
             }
             catch (Exception e)
             {
                 LogWrite.WriteLogInfo($"重启时,存在消费者,不删除队列,异常{e.Message}");
             }
         }
     }
 }
Example #9
0
        //测试方法
//        public Task Execute(IJobExecutionContext context)
//        {
//            Console.WriteLine("微信推送");
//
//            #region 测试
//            var stockSignalSingles = MongoService.GetAllStockSignalSingle();
//            var model1 = stockSignalSingles.FirstOrDefault();
//            var model2 = stockSignalSingles.Last();
//            List<stock_signal_single> list = new List<stock_signal_single>
//            {
//                model1,model2
//            };
//            DirectExchangeSendMsg(list);
//            #endregion
//
//
//            return Task.FromResult(0);
//        }

        public static void DirectExchangeSendMsg(List <stock_signal_single> signalNowList)
        {
            using (IConnection conn = RabbitMqFactory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
                    channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
                    channel.QueueBind(QueueName, ExchangeName, routingKey: "signalList");
                    var props = channel.CreateBasicProperties();

                    props.Persistent = true;
                    var listToJson = JsonConvert.SerializeObject(signalNowList);
                    var msgBody    = BinarySerializeHelper.SerializeObject(listToJson);

                    channel.BasicPublish(exchange: ExchangeName, routingKey: "signalList", basicProperties: props, body: msgBody);
                }
            }
        }
Example #10
0
        public static void DirectExchangeSendMsg()
        {
            using (IConnection conn = RabbitMqFactory.CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
                    channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
                    channel.QueueBind(QueueName, ExchangeName, routingKey: "key1");
//                    channel.QueueDeclare(QueueName2, durable: true, autoDelete: false, exclusive: false, arguments: null);
//                    channel.QueueBind(QueueName2, ExchangeName, routingKey: "key2");

                    var props = channel.CreateBasicProperties();
                    props.Persistent = true;
                    var chgModel = Service.GetChgData();
                    var msgBody  = BinarySerializeHelper.SerializeObject(chgModel);
//                    var msgBody = Encoding.UTF8.GetBytes(bytes);
                    channel.BasicPublish(exchange: ExchangeName, routingKey: "key1", basicProperties: props, body: msgBody);
                }
            }
        }
 public void ProcessStop()
 {
     RabbitMqFactory.Stop();
 }
Example #12
0
 protected void SetupRabbitMqFactory()
 {
     RabbitMqFactory = MockRepository.GenerateStub<RabbitMqFactory>();
     RabbitMqFactory
         .Stub(x => x.GetConnectionFactory(QueueEndpoint))
         .Return(CreateFactoryMock(QueueEndpoint, BasicProperties, MessageData));
     RabbitMqFactory
         .Stub(x => x.GetBasicConsumer(Model, Connection))
         .Return(ConsumerWrapper);
 }
 public void UserShouldBeSet()
 {
     var factory = new RabbitMqFactory();
     var connectionFactory = factory.GetConnectionFactory(_endpoint);
     connectionFactory.UserName.Should().Be("un");
 }
 public void PasswordShouldBeSet()
 {
     var factory = new RabbitMqFactory();
     var connectionFactory = factory.GetConnectionFactory(_endpoint);
     connectionFactory.Password.Should().Be("pw");
 }
 public void VhostShouldBeSet()
 {
     var factory = new RabbitMqFactory();
     var connectionFactory = factory.GetConnectionFactory(_endpoint);
     connectionFactory.VirtualHost.Should().Be("vhost");
 }
Example #16
0
 protected void SetupRabbitMqFactory(string exchange)
 {
     SetupExchange(exchange);
     Properties = MockRepository.GenerateStub<IBasicProperties>();
     SetupQueueEndpointProvider("testendpoint", "vhost", "testqueue", exchange);
     RabbitMqFactory = MockRepository.GenerateStub<RabbitMqFactory>();
     RabbitMqFactory
         .Stub(x => x.GetConnectionFactory(QueueEndpoint))
         .Return(CreateFactoryMock(QueueEndpoint, Properties, MessageData));
 }