예제 #1
0
        private void StockForm_Load(object sender, EventArgs e)
        {
            accountNameTextBox.Text          = DefaultAccountName;
            tradeQuantityNumericUpDown.Value = DefaultTradeRequestQuantity;
            txtRoutingKey.Text = DefaultRoutingKey;

            tradeOperationsGroupBox.Enabled = false;

            try
            {
                using (IConnectionFactory connectionFactory = new CachingConnectionFactory())
                {
                    IAmqpAdmin amqpAdmin = new RabbitAdmin(connectionFactory);

                    TopicExchange mktDataExchange = new TopicExchange("app.stock.marketdata", false, false);
                    amqpAdmin.DeclareExchange(mktDataExchange);
                    Spring.Messaging.Amqp.Core.Queue mktDataQueue = new Spring.Messaging.Amqp.Core.Queue("app.stock.marketdata");
                    amqpAdmin.DeclareQueue(mktDataQueue);

                    //Create the Exchange for MarketData Requests if it does not already exist.
                    //amqpAdmin.DeclareBinding(BindingBuilder.Bind(mktDataQueue).To(mktDataExchange).With(_currentBinding));
                    //Set up initial binding
                    RebindQueue(DefaultRoutingKey);
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Uncaught application exception.", ex);
            }
        }
예제 #2
0
        private void RebindQueue(string routingKey)
        {
            var ctx     = ContextRegistry.GetContext();
            var factory =
                ctx.GetObject("ConnectionFactory") as IConnectionFactory;

            try
            {
                IAmqpAdmin amqpAdmin = new RabbitAdmin(factory);

                TopicExchange mktDataExchange = new TopicExchange("APP.STOCK.MARKETDATA", false, false);
                Spring.Messaging.Amqp.Core.Queue mktDataQueue = new Spring.Messaging.Amqp.Core.Queue("APP.STOCK.MARKETDATA");

                if (!string.IsNullOrEmpty(_currentBinding))
                {
                    amqpAdmin.RemoveBinding(BindingBuilder.Bind(mktDataQueue).To(mktDataExchange).With(_currentBinding));
                }

                _currentBinding = routingKey;
                if (!string.IsNullOrEmpty(_currentBinding))
                {
                    amqpAdmin.DeclareBinding(BindingBuilder.Bind(mktDataQueue).To(mktDataExchange).With(_currentBinding));
                    txtRoutingKey.Text = _currentBinding;
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Uncaught application exception.", ex);
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            try
            {
                #region 20200507
                {
                    //发布订阅模式
                    //PublishSubscribeConsumer.Show();
                }
                #endregion

                #region 20200508
                {
                    //DirectExchangeConsumerLogError.Show();
                }
                {
                    //FanoutExchange.Show();
                }
                {
                    TopicExchange.Show();
                }
                #endregion

                #region 20200509
                ConsumptionACKConfirm.Show();
                #endregion
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
예제 #4
0
        public void TestSendAndReceiveWithTopicConsumeInBackground()
        {
            var admin    = new RabbitAdmin(this.connectionFactory);
            var exchange = new TopicExchange("topic");

            admin.DeclareExchange(exchange);
            this.template.Exchange = exchange.Name;

            admin.DeclareBinding(BindingBuilder.Bind(queue).To(exchange).With("*.end"));

            var template = new RabbitTemplate(new CachingConnectionFactory());

            template.Exchange = exchange.Name;

            var consumer = this.template.Execute(
                delegate
            {
                var consumerinside = this.CreateConsumer(template);
                var tag            = consumerinside.ConsumerTag;
                Assert.IsNotNull(tag);

                return(consumerinside);
            });

            template.ConvertAndSend("foo", "message");
            var result = this.GetResult(consumer);

            Assert.AreEqual(null, result);

            this.template.ConvertAndSend("foo.end", "message");
            result = this.GetResult(consumer);
            Assert.AreEqual("message", result);

            consumer.Stop();
        }
예제 #5
0
        public void TestSendAndReceiveWithNonDefaultExchange()
        {
            var admin    = new RabbitAdmin(this.connectionFactory);
            var exchange = new TopicExchange("topic");

            admin.DeclareExchange(exchange);

            admin.DeclareBinding(BindingBuilder.Bind(queue).To(exchange).With("*.end"));

            this.template.Execute <object>(
                delegate
            {
                var consumer = this.CreateConsumer(this.template);
                var tag      = consumer.ConsumerTag;
                Assert.IsNotNull(tag);

                this.template.ConvertAndSend("topic", "foo", "message");

                try
                {
                    var result = this.GetResult(consumer);
                    Assert.AreEqual(null, result);

                    this.template.ConvertAndSend("topic", "foo.end", "message");
                    result = this.GetResult(consumer);
                    Assert.AreEqual("message", result);
                }
                finally
                {
                    consumer.Channel.BasicCancel(tag);
                }

                return(null);
            });
        }
        public void TestSendAndReceiveWithTopicTwoCallbacks()
        {
            provider = services.BuildServiceProvider();
            var admin    = provider.GetRabbitAdmin();
            var exchange = new TopicExchange("topic");

            admin.DeclareExchange(exchange);
            var binding = BindingBuilder.Bind(queue).To(exchange).With("*.end");

            admin.DeclareBinding(binding);

            var template = provider.GetRabbitTemplate();

            template.DefaultSendDestination = new RabbitDestination(exchange.ExchangeName, string.Empty);
            try
            {
                template.Execute(c =>
                {
                    var consumer = CreateConsumer(template.ConnectionFactory);
                    var tag      = consumer.GetConsumerTags()[0];
                    Assert.NotNull(tag);
                    try
                    {
                        template.ConvertAndSend("foo", "message");
                        var result = GetResult(consumer, false);
                        Assert.Null(result);
                    }
                    finally
                    {
                        consumer.Stop();
                    }
                });

                template.Execute(c =>
                {
                    var consumer = CreateConsumer(template.ConnectionFactory);
                    var tag      = consumer.GetConsumerTags()[0];
                    Assert.NotNull(tag);
                    try
                    {
                        template.ConvertAndSend("foo.end", "message");
                        var result = GetResult(consumer, true);
                        Assert.Equal("message", result);
                    }
                    finally
                    {
                        consumer.Stop();
                    }
                });
            }
            finally
            {
                Assert.True(admin.DeleteExchange("topic"));
            }
        }
예제 #7
0
        static void Main(string[] args)
        {
            try
            {
                #region 20200507
                {
                    //生产者消费者
                    //ProductionConsumer.Show();
                }

                {
                    //多生产消费者
                    //Task.Run(() => { MultiProductionConsumer.Show01(); });
                    //Task.Run(() => { MultiProductionConsumer.Show02(); });
                    //Task.Run(() => { MultiProductionConsumer.Show03(); });
                }
                {
                    //互为生产消费者
                    //Task.Run(() => { MutualProductionConsumer.ShowProductio(); });
                    //Task.Run(() => { MutualProductionConsumer.ShowConsumer(); });
                }

                {
                    //发布订阅模式
                    //PublishSubscribeConsumer.Show();
                }
                {
                    //秒杀
                    //SeckillConsumer.Show();
                }
                #endregion

                #region 20200508
                {
                    //DirectExchangeConsumerLogAll.Show();
                }
                {
                    //FanoutExchange.Show();
                }
                {
                    TopicExchange.Show();
                }
                #endregion
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
        public void TestSendAndReceiveWithTopicConsumeInBackground()
        {
            provider = services.BuildServiceProvider();
            var admin    = provider.GetRabbitAdmin();
            var exchange = new TopicExchange("topic");

            admin.DeclareExchange(exchange);
            var template = provider.GetRabbitTemplate();

            template.DefaultSendDestination = new RabbitDestination(exchange.ExchangeName, string.Empty);
            var binding = BindingBuilder.Bind(queue).To(exchange).With("*.end");

            admin.DeclareBinding(binding);

            var cachingConnectionFactory = new CachingConnectionFactory("localhost");
            var template1 = new RabbitTemplate(cachingConnectionFactory)
            {
                DefaultSendDestination = new RabbitDestination(exchange.ExchangeName, string.Empty)
            };

            var consumer = template1.Execute(channel =>
            {
                var consumer1 = CreateConsumer(template1.ConnectionFactory);
                var tag       = consumer1.GetConsumerTags()[0];
                Assert.NotNull(tag);

                return(consumer1);
            });

            template1.ConvertAndSend("foo", "message");
            var result = GetResult(consumer, false);

            Assert.Null(result);

            template1.ConvertAndSend("foo.end", "message");
            result = GetResult(consumer, true);
            Assert.Equal("message", result);

            consumer.Stop();
            admin.DeleteExchange("topic");
            cachingConnectionFactory.Destroy();
        }
예제 #9
0
        static void Main(string[] args)
        {
            using (IConnectionFactory connectionFactory = new CachingConnectionFactory())
            {
                IAmqpAdmin amqpAdmin = new RabbitAdmin(connectionFactory);

                //Each queue is automatically bound to the default direct exchange.
                amqpAdmin.DeclareQueue(new Queue(ConfigurationManager.AppSettings["STOCK_REQUEST_QUEUE_NAME"]));
                amqpAdmin.DeclareQueue(new Queue(ConfigurationManager.AppSettings["STOCK_RESPONSE_QUEUE_NAME"]));

                TopicExchange mktDataExchange = new TopicExchange(ConfigurationManager.AppSettings["MARKET_DATA_EXCHANGE_NAME"], false, false);
                amqpAdmin.DeclareExchange(mktDataExchange);
                Queue mktDataQueue = new Queue(ConfigurationManager.AppSettings["MARKET_DATA_QUEUE_NAME"]);
                amqpAdmin.DeclareQueue(mktDataQueue);

                Console.WriteLine("Queues and exchanges have been declared.");
                Console.WriteLine("Press 'enter' to exit");
                Console.ReadLine();
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            try
            {
                #region 架构师VIP班-2
                {
                    //FanoutExchange.Show();
                }
                #endregion

                {
                    TopicExchange.Show();
                }

                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
예제 #11
0
        private void StockForm_Load(object sender, EventArgs e)
        {
            try
            {
                using (IConnectionFactory connectionFactory = new CachingConnectionFactory())
                {
                    IAmqpAdmin amqpAdmin = new RabbitAdmin(connectionFactory);

                    TopicExchange mktDataExchange = new TopicExchange("APP.STOCK.MARKETDATA", false, false);
                    amqpAdmin.DeclareExchange(mktDataExchange);
                    Spring.Messaging.Amqp.Core.Queue mktDataQueue = new Spring.Messaging.Amqp.Core.Queue("APP.STOCK.MARKETDATA");
                    amqpAdmin.DeclareQueue(mktDataQueue);

                    //Create the Exchange for MarketData Requests if it does not already exist.
                    //amqpAdmin.DeclareBinding(BindingBuilder.Bind(mktDataQueue).To(mktDataExchange).With(_currentBinding));
                    //Set up initial binding
                    RebindQueue("APP.STOCK.QUOTES.nasdaq.*");
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Uncaught application exception.", ex);
            }
        }
예제 #12
0
 /// <summary>
 /// #:匹配0-n个字符语句
 /// *:匹配一个字符语句
 /// </summary>
 /// <param name="dto"></param>
 /// <returns></returns>
 public Task <JsonResult> SendTopicMessage([FromBody] SendMessageDto dto)
 {
     topicExchange = new TopicExchange();
     topicExchange.SendMQ(dto);
     return(Task.FromResult(Json(true)));
 }
예제 #13
0
        static void Main(string[] args)
        {
            try
            {
                #region 架构师VIP班-1
                {
                    //生产者消费者
                    //ProductionConsumer.Show();
                }
                {
                    ////多生产者多消费者
                    //IConfigurationRoot config = new ConfigurationBuilder()
                    // .SetBasePath(Directory.GetCurrentDirectory())
                    // .AddCommandLine(args)//支持命令行参数
                    // .Build();


                    //string strMinute = config["minute"];  //什么时候开始执行
                    //string No = config["no"]; //生产者编号
                    //int minute = Convert.ToInt32(strMinute);

                    //bool flg = true;
                    //while (flg)
                    //{
                    //    if (DateTime.Now.Minute == minute)
                    //    {
                    //        Console.WriteLine($"到{strMinute}分钟,开始写入消息。。。");
                    //        flg = false;
                    //        MultiProductionConsumer.Show(No);
                    //    }
                    //}
                }
                {
                    ////互为生产者消费者
                    //Task.Run(() => { MutualProductionConsumer.ShowProductio(); });
                    //Task.Run(() => { MutualProductionConsumer.ShowConsumer(); });
                }
                {
                    ////秒杀
                    //IConfigurationRoot config = new ConfigurationBuilder()
                    // .SetBasePath(Directory.GetCurrentDirectory())
                    // .AddCommandLine(args)//支持命令行参数
                    // .Build();
                    //string strMinute = config["minute"];  //什么时候开始执行
                    //int minute = Convert.ToInt32(strMinute);

                    //bool flg = true;
                    //while (flg)
                    //{
                    //    Console.WriteLine($"到{strMinute}分钟,开始写入消息。。。");
                    //    if (DateTime.Now.Minute == minute)
                    //    {
                    //        flg = false;
                    //        SeckillConsumer.Show();
                    //    }
                    //}
                }
                {
                    ////优先级
                    //PriorityQueue.Show();
                }
                {
                    ////发布订阅模式
                    //PublishSubscribeConsumer.Show();
                }

                #endregion

                #region 架构师VIP班-2
                {
                    //DirectExchange.Show();
                }
                {
                    //FanoutExchange.Show();
                }
                {
                    TopicExchange.Show();
                }
                {
                    HeaderExchange.Show();
                }
                #endregion

                #region 架构师VIP班-3
                {
                    //ProductionMessageTx.Show();
                }
                {
                    //ProductionMessageConfirm.Show();
                }
                {
                    //ConsumptionACKConfirm.Show();
                }
                #endregion

                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
예제 #14
0
 public WorkersController(ILogger <WorkersController> logger, ICRUDService <Worker, WorkerDTO> workers, TopicExchange topicExchange)
 {
     this.logger        = logger;
     this.workers       = workers;
     this.topicExchange = topicExchange;
 }