/// <summary>
 /// 订阅消息
 /// </summary>
 /// <typeparam name="T">消息数据类型</typeparam>
 /// <param name="queueName">队列名称</param>
 /// <param name="callback">回调方法</param>
 public void Subscribe <T>(string queueName, Func <T, Result> callback)
 {
     RpcServer.AddConsumers(queueName, (bytes) =>
     {
         var data = bytes.FromSerialization <T>();
         return(callback?.Invoke(data) ?? new Result {
             Success = true
         });
     });
 }
Beispiel #2
0
        /// <summary>
        /// 实例化<see cref="MessageQueue"/>
        /// <param name="logging">日志模块</param>
        /// </summary>
        public MessageQueue(ILogging logging)
        {
            Logging    = logging;
            Connection = Instance.Connection;
            Channel    = Connection.CreateModel();

            const string queueName = "rpc_channel";

            Channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false);
            RpcServer = new RpcServer(new Subscription(Channel, queueName));
            RpcClient = new SimpleRpcClient(Channel, new PublicationAddress(
                                                exchangeType: ExchangeType.Direct,
                                                exchangeName: ExchangeName,
                                                routingKey: queueName));

            //MainLoop方法会阻塞线程, 所以要放到Task中
            Task.Run(() => { RpcServer.MainLoop(); });
        }