分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

RabbitMQ EasyNetq 用法

发布时间:2023-09-06 01:27责任编辑:沈小雨关键词:暂无标签

EasyNETQ帮助类

public class MQHelper ???{ ???????/// <summary> ???????/// 发送消息 ???????/// </summary> ???????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对象 ???????} ???????/// <summary> ???????/// 接收消息 ???????/// </summary> ???????/// <param name="msg"></param> ???????public static void Subscribe(Message msg, IProcessMessage ipro) ???????{ ???????????//// 创建消息bus ???????????IBus bus = BusBuilder.CreateMessageBus(); ???????????try ???????????{ ???????????????bus.Subscribe<Message>(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 EasyNetq 用法

原文地址:http://www.cnblogs.com/l1pe1/p/7903727.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved