Example #1
0
        public void Refresh(ClientType clientType, string subject, string group)
        {
            var key = clientType + "$" + subject;

            if (!CanRefresh(key))
            {
                return;
            }
            _lastRefreshTime[key] = DateTime.Now;

            lock (lockHelper)
            {
                if (_client == null || _client.IsClose)
                {
                    _client?.Close();

                    var hostAndPort = _addressResolver.Resolve();
                    _client = new TransportClient(hostAndPort.Host, hostAndPort.Port, new NewQmqEncoder(), new NewQmqDecoder(), new MetaInfoKeepliveStrategy(), false, 1024)
                    {
                        Processor = this
                    };
                }
            }

            if (!_client.Writtable)
            {
                _client.StateChanged += (sender, e) =>
                {
                    if (e.State == State.Connected)
                    {
                        SendRequest(clientType, subject, group, RequestType.Online);
                    }
                };
                _client.Connect();
            }
            else
            {
                SendRequest(clientType, subject, group);
            }

            Scheduler.INSTANCE.Schedule(() =>
            {
                _clusterMap.TryGetValue(key, out var clusterInfoFuture);
                if (clusterInfoFuture == null || clusterInfoFuture.AlreadySet)
                {
                    return;
                }

                var emptyClusterInfo = new NewQmqClusterInfo(false)
                {
                    Subject    = subject,
                    ClientType = clientType
                };
                clusterInfoFuture.Result = emptyClusterInfo;
            }, DateTime.Now.AddSeconds(5));
        }
Example #2
0
        void Processor.Process(HandlerContext context, object msg)
        {
            if (!(msg is Datagram datagram))
            {
                return;
            }

            if (datagram.Header.Code != 0)
            {
                return;
            }

            var packet = new Packet(datagram.Body, 0, datagram.Body.Length);

            //skip ts
            packet.ReadInt64();
            var subject = packet.ReadUTF8();

            //skip consumerGroup
            packet.ReadUTF8();
            //skip online offline state
            packet.ReadByte();
            int clientTypeCode = packet.ReadByte();

            var clusterInfo = new NewQmqClusterInfo
            {
                Subject    = subject,
                ClientType = (ClientType)clientTypeCode
            };

            var brokerCount = packet.ReadInt16();

            for (var i = 0; i < brokerCount; ++i)
            {
                var groupName = packet.ReadUTF8();
                var master    = packet.ReadUTF8();
                var ts        = packet.ReadInt64();
                var state     = packet.ReadByte();

                var group = new BrokerGroup
                {
                    Name        = groupName,
                    Master      = master,
                    UpdateTs    = ts,
                    BrokerState = (BrokerState)state
                };

                clusterInfo.AddBrokerGroup(@group);
            }

            _clusterMap.TryGetValue(clusterInfo.ClientType.ToString() + '$' + clusterInfo.Subject, out var clusterInfoFuture);
            if (clusterInfoFuture != null)
            {
                clusterInfoFuture.Result = clusterInfo;
            }
        }