示例#1
0
        private static void SaveOnFile(RabbitHelper rabbitHelper)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + @"\" + "result.json";

            ConfigureConsumer(
                rabbitHelper,
                out IModel channel,
                out string queueName,
                out EventingBasicConsumer consumer
                );

            consumer.Received += (model, ea) =>
            {
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine("Received {0}", message);
                if (!File.Exists(path))
                {
                    File.Create(path);
                }
                if (File.ReadAllText(path).Length > 0)
                {
                    message += ",";
                }
                File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + @"\" + "result.json",
                                   message);
            };
            channel.BasicConsume(queue: queueName,
                                 autoAck: true,
                                 consumer: consumer);
        }
示例#2
0
 private static void Save()
 {
     using (var rabbitHelper = new RabbitHelper())
     {
         SaveOnDb(rabbitHelper);
         SaveOnFile(rabbitHelper);
         Console.WriteLine("Press a KEY to exit.");
         Console.ReadLine();
     }
 }
示例#3
0
        private static void ConfigureConsumer(RabbitHelper rabbitHelper, out IModel channel,
                                              out string queueName, out EventingBasicConsumer consumer)
        {
            channel = rabbitHelper.GetChannel();
            string exchange = ConfigurationManager.AppSettings["Exchange"];

            channel.ExchangeDeclare(
                exchange: exchange,
                type: "fanout"
                );
            queueName = channel.QueueDeclare().QueueName;
            channel.QueueBind(
                queue: queueName,
                exchange: exchange,
                routingKey: ""
                );

            consumer = new EventingBasicConsumer(channel);
        }
示例#4
0
        private static void SaveOnDb(RabbitHelper rabbitHelper)
        {
            ConfigureConsumer(
                rabbitHelper,
                out IModel channel,
                out string queueName,
                out EventingBasicConsumer consumer
                );

            consumer.Received += async(model, ea) =>
            {
                var body     = ea.Body;
                var message  = Encoding.UTF8.GetString(body);
                var behavior = JsonConvert.DeserializeObject <Behavior>(message);
                Console.WriteLine("Received {0}", behavior);
                var repository = new BehaviorRepository();
                await repository.Create(behavior);
            };
            channel.BasicConsume(queue: queueName,
                                 autoAck: true,
                                 consumer: consumer);
        }