zoukankan      html  css  js  c++  java
  • RabbitMQ EasyNetq 用法

    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; }
        }
    
  • 相关阅读:
    Linux 自定义快捷命令
    Linux 用户登陆提示This account is currently not available
    Linux 切换用户提示Permission denied
    Netty核心组件之ChannelPipeline
    Netty核心组件之ChannelHandler
    Netty核心组件之Channel
    Netty核心组件之ChannlFuture
    Error creating bean with name 'eurekaAutoServiceRegistration'
    关于mysql数据库时间与java后台时间类型
    rabbitMQ-helloWorld
  • 原文地址:https://www.cnblogs.com/l1pe1/p/7903727.html
Copyright © 2011-2022 走看看