예제 #1
0
 /// <summary>
 /// 释放资源
 /// </summary>
 public void Dispose()
 {
     try
     {
         if (this.IsSubscriber)
         {
             eventintBasicConsumer.Received -= eventintBasicConsumer_Received;
         }
         //
         channel.Close();
         channel.Dispose();
         //
         lock (o)
         {
             IConnectionResource res = IConnectionResourceList.FirstOrDefault(x => x.ServerIp.Equals(this.ServerIp) && x.ServerPort == ServerPort);
             if (res != null)
             {
                 res.ReferenceCount--;
                 if (res.ReferenceCount < 1)
                 {
                     connection.Close();
                     connection.Dispose();
                     //
                     IConnectionResourceList.Remove(res);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         TrLog.Default.Error("资源释放异常:" + ex.Message);
     }
 }
예제 #2
0
        public static IConnection GetConnectionByServerInfo(string serverIp, int serverPort, string userName, string password, string virtualHost = "")
        {
            lock (o)
            {
                IConnectionResource res = IConnectionResourceList.FirstOrDefault(x => x.ServerIp.Equals(serverIp) && x.ServerPort == serverPort && x.VirtualHost == virtualHost);
                if (res == null)
                {
                    ConnectionFactory factory = new ConnectionFactory
                    {
                        HostName    = serverIp,
                        Port        = serverPort,
                        UserName    = userName,
                        Password    = password,
                        VirtualHost = virtualHost,
                        AutomaticRecoveryEnabled  = true,   //自动重连
                        TopologyRecoveryEnabled   = true,   //恢复拓扑结构
                        UseBackgroundThreadsForIO = true,   //后台处理消息
                        RequestedHeartbeat        = 60,     //心跳超时时间
                    };

                    //string tempStr = string.Format("serverIp:{0},serverPort:{1},userName:{2},password:{3}", serverIp, serverPort, userName, password);
                    //Log.TrLog.Default.Info(tempStr);
                    IConnection connection = factory.CreateConnection();

                    connection.ConnectionShutdown  += connection_ConnectionShutdown;
                    connection.CallbackException   += connection_CallbackException;
                    connection.ConnectionBlocked   += connection_ConnectionBlocked;
                    connection.ConnectionUnblocked += connection_ConnectionUnblocked;

                    res = new IConnectionResource()
                    {
                        ServerIp       = serverIp,
                        ServerPort     = serverPort,
                        Connection     = connection,
                        VirtualHost    = virtualHost,
                        ReferenceCount = 1
                    };
                    IConnectionResourceList.Add(res);
                }
                else
                {
                    res.ReferenceCount += 1;
                }
                //
                return(res.Connection);
            }
        }