WinForm实现Rabbitmq官网6个案例-Topics
代码:
namespace RabbitMQDemo
{
public partial class Topics : Form
{
private string exchangeName = "topic_logs"; //
private string exchangeType = ExchangeType.Topic;//交换机类型
private readonly static Topics _Topics;
Action<string, TextBox> SetText;
static Topics()
{
_Topics = new Topics();
}
/// <summary>
/// 单例模式
/// </summary>
public static Topics SingleForm { get { return _Topics; } }
private Topics()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
ReceiveMsg(txtConsumer1);
ReceiveMsg(txtConsumer2);
SetText += OnSetText;
} private void btnSendMsg_Click(object sender, EventArgs e)
{
SendMsg();
} private void SendMsg()
{
string message = txtPublisher.Text;
if (message.Trim().Length <= )
{
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 routingKey = cbBoxRoutingKey.Text;
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: exchangeName,
routingKey: routingKey,
basicProperties: null,
body: body);
}
} private void ReceiveMsg(TextBox box)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
var connection = factory.CreateConnection();
var channel = connection.CreateModel(); channel.ExchangeDeclare(exchange: exchangeName,
type: exchangeType);
var queueName = channel.QueueDeclare().QueueName; if (box.Name == "txtConsumer1")
{//消费者1
channel.QueueBind(queue: queueName,
exchange: exchangeName,
routingKey: "*.*.one");
channel.QueueBind(queue: queueName,
exchange: exchangeName,
routingKey: "one.#");
}
else if (box.Name == "txtConsumer2")
{//消费者2
channel.QueueBind(queue: queueName,
exchange: exchangeName,
routingKey: "*.*.two");
channel.QueueBind(queue: queueName,
exchange: exchangeName,
routingKey: "two.#");
} var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var msg = Encoding.UTF8.GetString(ea.Body); box.Invoke(SetText, msg, box);
}; channel.BasicConsume(queue: queueName,
noAck: true,
consumer: consumer);
}
private void OnSetText(string text, TextBox box)
{
box.Text += string.Format("{0}\r\n", text);
} private void Topics_Load(object sender, EventArgs e)
{
string[] dataSource = new string[] { "send.msg.one", "send.msg.two", "one.receive.msg", "two.receive.msg" };
cbBoxRoutingKey.DataSource = dataSource; cbBoxRoutingKey.DropDownStyle = ComboBoxStyle.DropDown;
}
}
}
界面:

大概流程:
生产者绑定一条消息和type='topic'的交换机以及路由键routingKey来发送到队列,消费者绑定同样的交换机和路由键来获取并处理消息
测试结果:





WinForm实现Rabbitmq官网6个案例-Topics的更多相关文章
- 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个案例-Hello World
先上代码 namespace RabbitMQDemo { public partial class HelloWorld : Form { string queueName1 = "hel ...
- 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 ...
随机推荐
- bzoj4998: 星球联盟(link-cut-tree)
题面 bzoj 题解 bzoj2959: 长跑的弱化版 产生了环就并查集维护一下 Code #include<bits/stdc++.h> #define LL long long #de ...
- axiso 生产环境跨域配置(可用)
1.npm install axios 后 在main.js中import import Axios from 'axios'Vue.prototype.$http = Axios 2.请求配置 th ...
- Mac 10.12安装XMind
下载: (链接: https://pan.baidu.com/s/1i4FmspJ 密码: ydc2)
- Linux 命令 su 和 sudo 比较
su : 可以用来切换到任何用户,但是要求输入目标用户的密码 比如:su - su - root (等价 su -) su - user su root 与 su - root 的区别在于,后者不切换 ...
- Java 中 String 的常用方法(二)
本文介绍剩下的一些常用的 String 中的方法. 1.replace 方法 .replaceFirst 方法和 replaceAll 方法 replace(char oldChar, char ne ...
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment avariable and mvn script match.
eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...
- Abschlussarbeit:Konstruktion und Implementierung von Dota2 Datenbank Intelligent Verwaltungsplatfom
1.Die Hintergrund und Bedeutung des Themas Dank nicht ausreichendes Erkenntnisse der Spielplanner un ...
- 【TCP协议】MTU和MSS详解
需要注意的是,区别两种帧封装格式:802标准帧和以太网帧 1,在802标准定义的帧格式中,长度字段是指它后续数据的字节长度,但不包括C R C检验码.RFC 1042(IEEE 802) 2,RFC ...
- CSS3动画积累+动画库+3d动画
一.animates.css animate.css是来自dropbox的工程师Daniel Eden开发的一款CSS3的动画效果小类库.包含了60多款不同类型的CSS3动画,包括:晃动,闪动,各种淡 ...
- Package.json中dependencies依赖包中^符号和~符号前缀的区别
刚git了webpack的包发现package.json里面dependencies依赖包的版本号前面的符号有两种,一种是~,一种是^,如下图标记: 然后搜了下在stackoverflow上找到一个比 ...