RabbitMQ|RabbitMQ EasyNetq 用法
EasyNETQ帮助类
public class MQHelper
{
///
/// 发送消息
///
public static void Publish(Message msg)
{
创建消息bus
IBus bus = BusBuilder.CreateMessageBus();
try
{
bus.Publish(msg, x => x.WithTopic(msg.MessageRouter));
}
catch (EasyNetQException ex)
{
System.Console.WriteLine("发送消息:" + ex.Message);
//处理连接消息服务器异常
}bus.Dispose();
//与数据库connection类似,使用后记得销毁bus对象
}///
/// 接收消息
///
///
public static void Subscribe(Message msg, IProcessMessage ipro)
{
创建消息bus
IBus bus = BusBuilder.CreateMessageBus();
try
{
bus.Subscribe(msg.MessageRouter, message => ipro.ProcessMsg(message), x => x.WithTopic(msg.MessageRouter));
System.Console.WriteLine("订阅消息成功");
}
catch (EasyNetQException ex)
{
System.Console.WriteLine("订阅消息失败:" + ex.Message);
}//与数据库connection类似,使用后记得销毁bus对象
}
}
public class BusBuilder
{
public static IBus CreateMessageBus()
{
//消息服务器连接字符串
var connectionString = ConfigurationManager.ConnectionStrings["RabbitMQ"];
if (connectionString == null || connectionString.ConnectionString == string.Empty)
{
throw new Exception("messageserver connection string is missing or empty");
}
return RabbitHutch.CreateBus(connectionString.ConnectionString);
}}
public interface IProcessMessage
{
void ProcessMsg(Message msg);
void Notice();
}
public class Message
{
public string MessageID { get;
set;
}public string MessageTitle { get;
set;
}public string MessageBody { get;
set;
}public string MessageRouter { get;
set;
}
}
【RabbitMQ|RabbitMQ EasyNetq 用法】转载于:https://www.cnblogs.com/l1pe1/p/7903727.html
推荐阅读
- canvas(一)基本用法
- 一般现在时
- 学习日记block众多用法中的一种用法
- vue的cli用法
- python之SimpleHTTPServer用法
- 不为人知的排序和筛选用法
- python|python UI自动化11- 常见定位元素用法(二)
- js|js async和await的用法
- Promise|Promise 高级用法对比
- 基于rabbitmq实现的延时队列(golang版)