• rabbitMQ的简单使用


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using RabbitMQ.Client;
    using RabbitMQ.ServiceModel;
    using RabbitMQ.Util;
    using System.Threading;
    namespace rabbitMQ的使用
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            // 发送消息
            private void button1_Click(object sender, EventArgs e)
            {
                var factory = new ConnectionFactory();
                factory.HostName = "localhost";
                factory.UserName = "zheng";
                factory.Password = "4421707";
                Task t = Task.Run(() => {
                    try
                    {
                        using (var connection = factory.CreateConnection())
                        {
                            using (var channel = connection.CreateModel())
                            {
                                channel.QueueDeclare("routKey", false, false, false, null);
                                var propertiies = channel.CreateBasicProperties();
                                propertiies.DeliveryMode = 2;
                                for (int i = 0; i < 10; i++)
                                {
                                    var body = Encoding.UTF8.GetBytes("hello,zheng,hao,nan");
                                    channel.BasicPublish("", "routKey", propertiies, body);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }                                           
                });           
            }
            //接收消息
            private void button2_Click(object sender, EventArgs e)
            {
                var factory = new ConnectionFactory();
                factory.HostName = "localhost";
                factory.UserName = "zheng";
                factory.Password = "4421707";              
                Task t = Task.Run(() => {
                    using (var connection = factory.CreateConnection())
                    {
                        using (var channel = connection.CreateModel())
                        {
                            while (true)
                            {
                                BasicGetResult msgResponse = channel.BasicGet(queue: "routKey", noAck: true);
                                if (msgResponse != null)
                                {
                                    string msgBody = Encoding.UTF8.GetString(msgResponse.Body);
                                   //异步更新UI
                                    string[] s = msgBody.Split(',');
                                    this.Invoke((MethodInvoker)(()=>{
                                        listBox1.Items.Add(s[0]);
                                        listBox1.Items.Add(s[1]);
                                        listBox1.Items.Add(s[2]+s[3]);
                                    }));                                             
                                }                         
                            }
                        }
                    }                         
                });               
            }
        }
    
    }
    

      

  • 相关阅读:
    华为S12700 NQA配置
    斐讯K1 K2 开启Telnet
    存储的一些基本概念(HBA,LUN)
    华为AR配置内部服务器示例(只有1个公网IP)
    使用nginx 做kbmmw REST 服务的负载均衡
    第一个 macOS 64位 kbmmw 服务器
    使用FMXlinux 开发linux 桌面应用
    推荐一套免费跨平台的delphi 哈希及加密算法库
    使用kbmmw 的调度事件动态显示时间
    提高sqlite 的运行性能(转载)
  • 原文地址:https://www.cnblogs.com/hnzheng/p/12627072.html
Copyright © 2020-2023  润新知