示例#1
0
        public static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json")
                                .Build();

            var rabbitSettings = configuration.GetSection("rabbitmq-settings");

            var config = new QueueConfig
            {
                HostName  = rabbitSettings["hostName"],
                UserName  = rabbitSettings["userName"],
                Password  = rabbitSettings["password"],
                QueueName = rabbitSettings["sendQueue"]
            };

            DisplayRabbitSettings(config);
            Console.WriteLine("Starting School Service Queue Processor....");
            Console.WriteLine();


            var processor = new QueueProcessor(config);

            processor.Start();
        }
示例#2
0
 private static void DisplayRabbitSettings(QueueConfig config)
 {
     Console.WriteLine("*********************");
     Console.WriteLine("Host: {0}", config.HostName);
     Console.WriteLine("Username: {0}", config.UserName);
     Console.WriteLine("QueueName: {0}", config.QueueName);
     Console.WriteLine("*********************");
     Console.WriteLine();
 }
        public QueueProcessor(QueueConfig config)
        {
            _config = config;

            _connectionFactory = new ConnectionFactory
            {
                HostName = config.HostName,
                UserName = config.UserName,
                Password = config.Password
            };

            _connection = _connectionFactory.CreateConnection();
            _model      = _connection.CreateModel();
            _model.BasicQos(0, 1, false);

            _model.QueueDeclare(config.QueueName, false, false, true, null);

            _dataStore = new DataStore();
        }
        public QueueProcessor(QueueConfig config)
        {
            _config = config;

            _connectionFactory = new ConnectionFactory
            {
                HostName = config.HostName,
                UserName = config.UserName,
                Password = config.Password
            };

            _connection = _connectionFactory.CreateConnection("QueueProcessor Connection"); // 建立rabbitMQ 消息队列连接
            _model      = _connection.CreateModel();

            _model.BasicQos(0, 1, false); //这样RabbitMQ就会使得每个Consumer在同一个时间点最多处理一个Message。换句话说,在接收到该Consumer的ack前,他它不会将新的Message分发给它。
            //注意,这种方法可能会导致queue满。当然,这种情况下你可能需要添加更多的Consumer,或者创建更多的virtualHost来细化你的设计。

            _model.QueueDeclare(config.QueueName, false, false, true, null);             // 声明队列

            _dataStore = new DataStore();
        }