/// <summary> /// 启动队列监听 /// </summary> public void Run() { IConnectionFactory factory = new ConnectionFactory() { HostName = "192.168.1.121", Port = 5672, UserName = "******", Password = "******" }; LogMessageBusiness logMessageBusiness = new LogMessageBusiness(); using (var connection = factory.CreateConnection()) { using (IModel channel = connection.CreateModel()) { channel.QueueDeclare(queue: "LogQueue", durable: true, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (sender, ea) => { // get text var body = ea.Body.ToArray(); var message = Encoding.UTF8.GetString(body); // insert log entity var log = logMessageBusiness.Deserialize(message); logMessageBusiness.SetTime(log); logMessageBusiness.Insert(log); Console.WriteLine(log.ToString()); }; channel.BasicConsume(queue: "LogQueue", autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } }
public void Setup() { logMessageBusiness = new LogMessageBusiness(); }