WinForm实现Rabbitmq官网6个案例-Hello World
先上代码
namespace RabbitMQDemo
{
public partial class HelloWorld : Form
{
string queueName1 = "hello_queue1";//消费者1
string queueName2 = "hello_queue2";//消费者2
Action<string> SetText;
/// <summary>
/// 单线程实例
/// </summary>
private static readonly HelloWorld _helloWorld;
static HelloWorld()
{
_helloWorld = new HelloWorld();
}
/// <summary>
/// 单例模式
/// </summary>
public static HelloWorld SingleForm
{ get { return _helloWorld; } }
private HelloWorld()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
ReseiveMsg(queueName1);
ReseiveMsg(queueName2);
SetText += OnSetText;
} private void btnSendMsg_Click(object sender, EventArgs e)
{
SendMsg();
}
/// <summary>
/// 发送消息
/// </summary>
private void SendMsg()
{
string message = txtPublisher.Text;
if (message.Trim().Length <= )
{
MessageBox.Show("请输入要发送的消息");
}
string queueName = cbBoxQueues.SelectedValue.ToString();
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null); var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "",
routingKey: queueName,
basicProperties: null,
body: body);
}
} /// <summary>
/// 接收消息
/// </summary>
private void ReseiveMsg(string queueName)
{
//string queueName = cbBoxQueues.SelectedText;
try
{
var factory = new ConnectionFactory() { HostName = "localhost" }; //connection和channel不能使用using,否则会被dispose掉
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
//声明队列 生产者和消费者都需要QueueDeclare
channel.QueueDeclare(queue: queueName,
durable: false,
exclusive: false,
autoDelete: false,
arguments: null); var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body); txtConsumer1.Invoke(SetText, message);
};
channel.BasicConsume(queue: queueName,
noAck: true,
consumer: consumer);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} private void OnSetText(string txtContent)
{
string queueName = cbBoxQueues.SelectedValue.ToString();
if (queueName == queueName1)
txtConsumer1.Text += string.Format("{0}\r\n", txtPublisher.Text);
if (queueName == queueName2)
txtConsumer2.Text += string.Format("{0}\r\n", txtPublisher.Text);
} private void HelloWorld_Load(object sender, EventArgs e)
{
List<DataSource> lst = new List<DataSource>();
lst.Add(new DataSource("消费者1", "hello_queue1"));
lst.Add(new DataSource("消费者2", "hello_queue2")); cbBoxQueues.DataSource = lst;
cbBoxQueues.DisplayMember = "DisplayMember";
cbBoxQueues.ValueMember = "DisplayValue";
} private class DataSource
{
public DataSource(string displayMember,string displayValue)
{
DisplayMember = displayMember;
DisplayValue = displayValue;
}
public string DisplayMember { get; set; }
public string DisplayValue { get; set; }
}
}
}
界面如下:

大致流程是
生产者发送消息到队列,然后队列(rabbitmq)把消息发送给消费者(消费者向rabbitmq索取消息)
两个消费者:



WinForm实现Rabbitmq官网6个案例-Hello World的更多相关文章
- WinForm实现Rabbitmq官网6个案例-RPC
获取源码 客户端代码: namespace RabbitMQDemo { public partial class RPC : Form { private readonly static RPC _ ...
- WinForm实现Rabbitmq官网6个案例-Publishe/Subscribe
代码: namespace RabbitMQDemo { public partial class PublishSubscribe : Form { private string exchangeN ...
- WinForm实现Rabbitmq官网6个案例-Work Queues
代码: namespace RabbitMQDemo { public partial class WorkQueues : Form { private string queueName = &qu ...
- WinForm实现Rabbitmq官网6个案例-Topics
代码: namespace RabbitMQDemo { public partial class Topics : Form { private string exchangeName = &quo ...
- WinForm实现Rabbitmq官网6个案例-Routing
代码: namespace RabbitMQDemo { public partial class Routing : Form { private string exchangeName = &qu ...
- 官网英文版学习——RabbitMQ学习笔记(一)认识RabbitMQ
鉴于目前中文的RabbitMQ教程很缺,本博主虽然买了一本rabbitMQ的书,遗憾的是该书的代码用的不是java语言,看起来也有些不爽,且网友们不同人学习所写不同,本博主看的有些地方不太理想,为此本 ...
- 2022年官网下安装RabbitMQ最全版与官网查阅方法
目录 一.Erlang环境部署 1.百度搜索"Erlang",或者访问网址:https://www.erlang.org/,找到DOWNLOAD双击进入. 2.找到支持的windo ...
- Yeoman 官网教学案例:使用 Yeoman 构建 WebApp
STEP 1:设置开发环境 与yeoman的所有交互都是通过命令行.Mac系统使用terminal.app,Linux系统使用shell,windows系统可以使用cmder/PowerShell/c ...
- MXNet官网案例分析--Train MLP on MNIST
本文是MXNet的官网案例: Train MLP on MNIST. MXNet所有的模块如下图所示: 第一步: 准备数据 从下面程序可以看出,MXNet里面的数据是一个4维NDArray. impo ...
随机推荐
- SCOI2019 游记
写在前面 其实冬令营之后就有一些想说的内容,由于心情原因没有写出来.PKUWC 失误频频,唯一可能还有点价值的就是 Day2T3 计算几何推了 76 分出来.NOIWC 更是无心再谈,感觉是被提答送走 ...
- FZU_1894 志愿者选拔 【单调队列】
1 题面 FZU1894 2 分析 单调队列的典型引用 需要注意的是在用维护辅助队列的时候,$L$和$R$的初始化都是0时,队列第一个数就是$L$,最后一个数就是$R-1$. 3 AC代码 #incl ...
- Groovy 反序列化漏洞分析(CVE-2015-3253)
0x00 前言 Java反序列化的漏洞爆发很久了,此前一直想学习一下.无奈Java体系太过于复杂,单是了解就花了我很久的时间,再加上懒,就一直拖着,今天恰好有空,参考@iswin大佬两年前的分析, ...
- 让 Linux grep 的输出不换行
在Redhat中亲测. 本来ps -ef的输出是不会换行的,但是 ps -ef | grep java 就换行了. 如果想让grep完的结果不要换行,找到两种方法. 1. 在后面拼接 less -S: ...
- SpringMVC初写(一)SpringMVC的配置方式
1.Spring概述a)SpringMVC是什么?SpringMVC是Spring框架内置的MVC实现.SpringMVC就是一个Spring内置的MVC子框架MVC:Model-View-Contr ...
- AES对称加解密
简介设计思想加密模式ECB模式(电子密码本模式:Electronic codebook)CBC模式(密码分组链接:Cipher-block chaining)CFB模式(密文反馈:Cipher fee ...
- 使用InstallUtil安装及卸载Windows服务的具体操作 Visual Studio 2012版本
关于Visual Studio 2012中使用InstallUtil对Windows服务进行安装与卸载的文章,在MSDN中的http://msdn.microsoft.com/en-us/librar ...
- (转)MySQL出现同步延迟有哪些原因?如何解决?
http://oldboy.blog.51cto.com/2561410/1682147----MySQL出现同步延迟有哪些原因?如何解决? 原文:http://www.zjian.me/mysql/ ...
- 什么是O/RMapping?为什么要用O/R Mapping?
什么是O/R Mapping ? O/R Mapping 就是有一大堆的类库,我们调用它的时候用面向对象的方式来调,它帮我们翻译成为面向关系的方式. 为什么要用O/R Mapping? 我们编程会更加 ...
- 看过ruby相关书籍
<ruby编程语言> <crafting rails applications> <agile web developemnt> <build awesome ...