zoukankan      html  css  js  c++  java
  • WinForm实现Rabbitmq官网6个案例-Publishe/Subscribe

    代码:

    namespace RabbitMQDemo
    {
        public partial class PublishSubscribe : Form
        {
            private string exchangeName = "logs";
            private string exchangeType = ExchangeType.Fanout;//广播模式
            Action<string, TextBox> SetText;
            private readonly static PublishSubscribe _PublishSubscribe;
            static PublishSubscribe()
            {
                _PublishSubscribe = new PublishSubscribe();
            }
            /// <summary>
            /// 单例模式
            /// </summary>
            public static PublishSubscribe SingleForm { get { return _PublishSubscribe; } }
            private PublishSubscribe()
            {
                CheckForIllegalCrossThreadCalls = false;
                InitializeComponent();
                ReceiveMsg(txtConsumer1);//消费者1
                ReceiveMsg(txtConsumer2);//消费者2
                SetText += OnSetText;
            }
    
            private void btnSendMsg_Click(object sender, EventArgs e)
            {
                SendMsg();
            }
            /// <summary>
            /// 发送消息
            /// </summary>
            private void SendMsg()
            {
                string message = txtPublisher.Text;
                if (message.Trim().Length <= 0)
                {
                    MessageBox.Show("请输入要发送的消息");
                }
                var factory = new ConnectionFactory() { HostName = "localhost" };
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(
                        exchange: exchangeName,
                        type: exchangeType);
    
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: exchangeName,
                                         routingKey: "",
                                         basicProperties: null,
                                         body: body);
                }
            }
            /// <summary>
            /// 接收消息
            /// </summary>
            private void ReceiveMsg(TextBox box)
            {
                try
                {
                    var factory = new ConnectionFactory() { HostName = "localhost" };
                    var connection = factory.CreateConnection();
                    var channel = connection.CreateModel();
    
                    //声明交换机
                    channel.ExchangeDeclare(
                        exchange: exchangeName,
                        type: exchangeType);
    
                    //rabbitmq随机生成队列名 durable=false exclusive=true, autodelete=true
                    //exchangeName发来的消息在接收端永远都是新的队列在接收
                    var queueName = channel.QueueDeclare().QueueName;
    
                    //绑定队列和交换机
                    //必须绑定了名为exchangeName的queueName队列才能收到消息
                    channel.QueueBind(
                        queue: queueName,
                        exchange: exchangeName,
                        routingKey: "");
    
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                      {
                          var msg = Encoding.UTF8.GetString(ea.Body);
    
                          txtConsumer1.Invoke(SetText, msg, box);
                      };
                    channel.BasicConsume(
                        queue: queueName,
                        noAck: true,
                        consumer: consumer);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    
            private void OnSetText(string text, TextBox box)
            {
                box.Text += string.Format("{0}
    ", text);
            }
        }
    }
    View Code

    界面:

    大概流程:

    生产者发送一条消息通过exchange交换机绑定到2个队列上,两个队列上都会有这一条消息,消费者1,2向队列取出消息并做处理

    测试结果:

  • 相关阅读:
    SharePoint 2013 图文开发系列之自定义字段
    SharePoint 2013 图文开发系列之Visual Studio 创建母版页
    SharePoint 2013 图文开发系列之代码定义列表
    SharePoint 2013 图文开发系列之计时器任务
    SharePoint 2013 图文开发系列之应用程序页
    SharePoint 2013 图文开发系列之事件接收器
    SharePoint 2013 图文开发系列之可视化WebPart
    SharePoint 2013 图文开发系列之WebPart
    SharePoint 2013 对二进制大型对象(BLOB)进行爬网
    SharePoint 2013 状态机工作流之日常报销示例
  • 原文地址:https://www.cnblogs.com/zhyue93/p/rabbitmq-publishsubscribe.html
Copyright © 2011-2022 走看看