Esempio n. 1
0
        /// <summary>
        /// Start listening for events
        /// </summary>
        public virtual void Start()
        {
            if (IsListening)
            {
                throw new BusConfigurationException("Attempted to start the MicroserviceHost, but it has already started.");
            }

            OnHostStarted();
            IsListening = true;

            foreach (MicroserviceCommandListener callback in CommandListeners)
            {
                Logger.LogInformation($"Registering command queue {callback.Queue}");

                ICommandReceiver receiver = Context.CreateCommandReceiver(callback.Queue);
                receiver.DeclareCommandQueue();
                receiver.StartReceivingCommands(callback.Callback);
                CommandReceivers.Add(receiver);
            }

            MessageReceiver = Context.CreateMessageReceiver(QueueName, Topics);
            MessageReceiver.StartReceivingMessages();
            MessageReceiver.StartHandlingMessages(eventMessage =>
            {
                OnEventMessageReceived(eventMessage);
                foreach (MicroserviceListener microserviceListener in Listeners)
                {
                    foreach (Regex expression in microserviceListener.TopicRegularExpressions)
                    {
                        if (expression.IsMatch(eventMessage.Topic))
                        {
                            microserviceListener.Callback.Invoke(eventMessage);
                        }
                    }
                }
                OnEventMessageHandled(eventMessage);
            });
        }
 public void StartListening(IMicroserviceHost host)
 {
     Host = host;
     _receiver.StartReceivingCommands(Handle);
 }