Esempio n. 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)
     {
         throw ex;
     }
 }
Esempio n. 2
0
        public static IConnection GetConnectionByServerInfo(string serverIp, int serverPort, string userName, string password)
        {
            lock (o)
            {
                IConnectionResource res = IConnectionResourceList.FirstOrDefault(x => x.ServerIp.Equals(serverIp) && x.ServerPort == serverPort);
                if (res == null)
                {
                    //创建一个连接工厂
                    ConnectionFactory factory = new ConnectionFactory
                    {
                        Port     = serverPort,
                        UserName = userName,
                        Password = password,
                        AutomaticRecoveryEnabled  = true,   //自动重连
                        TopologyRecoveryEnabled   = true,   //恢复拓扑结构
                        UseBackgroundThreadsForIO = true,   //后台处理消息
                        RequestedHeartbeat        = 60      //心跳超时时间
                    };

                    //支持集群扩展,集群主机IP地址以逗号','分隔
                    IConnection connection = null;
                    if (serverIp.Contains(","))
                    {
                        List <string> hosts = new List <string>();
                        serverIp.Split(',').ToList().ForEach(host =>
                        {
                            if (!string.IsNullOrWhiteSpace(host))
                            {
                                hosts.Add(host);
                            }
                        });
                        connection = factory.CreateConnection(hosts);
                    }//一个主机,单一主机
                    else
                    {
                        factory.HostName = serverIp;
                        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,
                        ReferenceCount = 1
                    };
                    IConnectionResourceList.Add(res);
                }
                else
                {
                    res.ReferenceCount += 1;
                }
                //
                return(res.Connection);
            }
        }