赞
踩
Task.Run(() => RbtMQHelp.Start());
public class RbtMQHelp { static bool stopflag = true; public RbtMQHelp() { // // TODO: Add constructor logic here // } public static void Stop() { stopflag = false; } public static void Start() { var factory = new ConnectionFactory() { HostName = "127.0.0.1", UserName = "admin", Password = "6666m06" }; var connection = factory.CreateConnection(); var channel = connection.CreateModel(); channel.QueueDeclare(queue: "DC", durable: true, exclusive: false, autoDelete: false, arguments: null); channel.BasicQos(0, 1, false); var consumer = new EventingBasicConsumer(channel); channel.BasicConsume(queue: "DC", autoAck: false, consumer: consumer); consumer.Received += (model, ea) => { string response = null; var body = ea.Body; var props = ea.BasicProperties; var replyProps = channel.CreateBasicProperties(); replyProps.CorrelationId = props.CorrelationId; try { var message = Encoding.UTF8.GetString(body); response = Control.SetResponse(message); } catch (Exception e) { response = e.ToString(); log.Error(e.ToString()); } finally { var responseBytes = Encoding.UTF8.GetBytes(response); channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes); channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); } }; while (stopflag) { Thread.Sleep(500000); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。