首页 > 代码库 > RabbitMQ 学习初步(一)
RabbitMQ 学习初步(一)
//刚刚接触,如有不对还望不吝指正 public static void StartUp() { #region 前期准备工作 ConnectionFactory factory = new ConnectionFactory(); factory.Uri = "amqp://guest:guest@localhost:5672/"; //架起物理链路 IConnection conn = factory.CreateConnection(); //创建通信信道 IModel channel = conn.CreateModel(); string exchangename = "我是负责转发信号的路由1"; string queuename = "我是信号的接收器1"; //架设信号路由 channel.ExchangeDeclare(exchangename, ExchangeType.Direct); //架设信号接收器 channel.QueueDeclare(queuename, false, false, false, null); //连通信号路由器和接收器 channel.QueueBind(queuename, exchangename, "", null); #endregion byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!"); //channel.BasicPublish(exchangename,"",null,messageBodyBytes); IBasicProperties props = channel.CreateBasicProperties(); props.ContentType = "text/plain"; props.DeliveryMode = 2; channel.BasicPublish(exchangename,"",props,messageBodyBytes); ConnectionFactory service = new ConnectionFactory(); using (conn = service.CreateConnection()) { using (IModel servicechannle = conn.CreateModel()) { //ch.QueueDeclare(queuename, false, false, false, null); //服务端接收消息队列 bool noAck = false; BasicGetResult result = servicechannle.BasicGet(queuename, noAck); if (result == null) { Console.WriteLine("没有消息"); } else { servicechannle.BasicAck(result.DeliveryTag, false); Console.WriteLine("接收消息成功:" + result.BasicProperties.ContentType); Console.WriteLine("接收消息成功:" + result.BasicProperties.DeliveryMode); Console.WriteLine("接收消息成功:" + System.Text.Encoding.Default.GetString(result.Body)); } } } conn.AutoClose = true; }
RabbitMQ 学习初步(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。