• rabbitmq 发送 消费消息


    发布消息:到rabbitmq
    //rabbitmq 发送消息01
    string message = $"消息{i}";
    byte[] body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(exchange: "VTS_CSJ_TEST",
    routingKey: string.Empty,
    basicProperties: basicProperties,
    body: body);
    Console.WriteLine($"消息消息消息消息消息消息消息消息消息消息消息消息消息消息:{message} 已发送~");

    //发送日志信息02
    foreach (var log in logList)
    {
    channel.BasicPublish(exchange: "VTS_CSJ_TEST",
    routingKey: log.LogType,
    basicProperties: null,
    body: log.Msg);
    Console.WriteLine($"{Encoding.UTF8.GetString(log.Msg)} 已发送~~");
    }

    //基本发布03
    var body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(exchange: "FanoutExchange",
    routingKey: string.Empty,
    basicProperties: null,
    body: body);
    Console.WriteLine($"通知【{message}】已发送到队列");

    //基本发布04
    channel.QueueBind(queue: "ChinaQueue", exchange: "TopicExchange", routingKey: "China.#", arguments: null);
    channel.QueueBind(queue: "newsQueue", exchange: "TopicExchange", routingKey: "#.news", arguments: null);
    {
    string message = "来自中国的新闻消息。。。。";
    var body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(exchange: "TopicExchange", routingKey: "China.news", basicProperties: null, body: body);
    Console.WriteLine($"消息【{message}】已发送到队列");
    }

    {
    string message = "来自中国的天气消息。。。。";
    var body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(exchange: "TopicExchange", routingKey: "China.weather", basicProperties: null, body: body);
    Console.WriteLine($"消息【{message}】已发送到队列");
    }
    {
    string message = "来自美国的新闻消息。。。。";
    var body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(exchange: "TopicExchange", routingKey: "usa.news", basicProperties: null, body: body);
    Console.WriteLine($"消息【{message}】已发送到队列");
    }
    {
    string message = "来自美国的天气消息。。。。";
    var body = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(exchange: "TopicExchange", routingKey: "usa.weather", basicProperties: null, body: body);
    Console.WriteLine($"消息【{message}】已发送到队列");
    }

    消费队列种的消息:
    //消费01 队列中的所有消息;
    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
    var body = ea.Body;
    var message = Encoding.UTF8.GetString(body.ToArray());
    Console.WriteLine($"【{message}】,写入文本~~");
    };
    //处理消息
    channel.BasicConsume(queue: "DirectExchangeLogAllQueue",
    autoAck: true,
    consumer: consumer);


    //消费02
    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
    var body = ea.Body;
    var message = Encoding.UTF8.GetString(body.ToArray());
    //只是为了演示,并没有存入文本文件
    Console.WriteLine($"接收成功!【{message}】,邮件通知");
    };
    Console.WriteLine("通知服务准备就绪...");
    //处理消息
    channel.BasicConsume(queue: "FanoutExchangeZhaoxi001",
    autoAck: true,
    consumer: consumer);

  • 相关阅读:
    【23设计模式】总结
    【JAVA】内部类,内部接口
    【JAVA】接口
    【JAVA】抽象类,抽象方法
    【JAVA】类加载器
    【JAVA】枚举
    【JAVA】序列化
    【JAVA】异常笔记
    汇编笔记_第十一章
    汇编笔记_第十章
  • 原文地址:https://www.cnblogs.com/csj007523/p/13969631.html
Copyright © 2020-2023  润新知