public async Task Handle(MqClient client, HorseMessage message, bool fromNode)
        {
            try
            {
                HorseQueue queue = _server.FindQueue(message.Target);

                //if auto creation active, try to create queue
                if (queue == null && _server.Options.AutoQueueCreation)
                {
                    QueueOptions options = QueueOptions.CloneFrom(_server.Options);
                    queue = await _server.CreateQueue(message.Target, options, message, _server.DeliveryHandlerFactory, true, true);
                }

                if (queue == null)
                {
                    if (!string.IsNullOrEmpty(message.MessageId))
                    {
                        await client.SendAsync(message.CreateResponse(HorseResultCode.NotFound));
                    }

                    return;
                }

                await HandlePullRequest(client, message, queue);
            }
            catch (Exception e)
            {
                _server.SendError("PULL_REQUEST", e, $"QueueName:{message.Target}");
            }
        }
示例#2
0
        /// <summary>
        /// Triggers event and sends message to subscribers
        /// </summary>
        protected void Trigger(object model)
        {
            try
            {
                if (_subscribers.Count == 0)
                {
                    return;
                }

                HorseMessage message = new HorseMessage(MessageType.Event, Target);
                message.SetSource(Name);

                if (model != null)
                {
                    message.Serialize(model, _server.MessageContentSerializer);
                }

                byte[] data = HmqWriter.Create(message);

                List <MqClient> removing = null;

                lock (_subscribers)
                    foreach (MqClient subscriber in _subscribers)
                    {
                        //if client is disconnected, add it into removing list
                        if (!subscriber.IsConnected)
                        {
                            //removing list is created when it's needed
                            if (removing == null)
                            {
                                removing = new List <MqClient>();
                            }

                            removing.Add(subscriber);
                        }
                        else
                        {
                            _ = subscriber.SendAsync(data);
                        }
                    }

                //if there are some removing clients from subscribers list, remove them
                if (removing != null)
                {
                    lock (_subscribers)
                        foreach (MqClient remove in removing)
                        {
                            _subscribers.Remove(remove);
                        }
                }
            }
            catch (Exception e)
            {
                _server.SendError("EVENT_TRIGGER", e, $"Name:{Name}, Type:{GetType().Name}");
            }
        }