Ejemplo n.º 1
0
        public UseCaseHandler <M> FromResult <M>(
            NextAction <M> action, OnCompleteCallback <M> onComplete = null, OnStartCallback onStart = null,
            OnErrorCallback onError = null)
        {
            M   result      = default;
            var nextHandler = new UseCaseHandler <M>();

            nextHandler.ExecutingTask = ExecutingTask?.ContinueWith(t =>
            {
                onStart?.Invoke();

                try
                {
                    result = action.Invoke(Result);

                    onComplete?.Invoke(result);
                }
                catch (Exception e)
                {
                    onError?.Invoke(e);
                }

                nextHandler.Result = result;
            });

            return(nextHandler);
        }
        public void Connect()
        {
            if (this.connection != null && this.connection.IsOpen)
            {
                this.connection.Close();
            }

            var factory = new ConnectionFactory()
            {
                HostName = this.BrokerHostname,
                UserName = this.BrokerUsername,
                Password = this.BrokerPassword,
                Port     = this.BrokerPort
            };

            this.connection = factory.CreateConnection();
            var rmqChannel = this.connection.CreateModel();

            rmqChannel.ExchangeDeclare(exchange: "shift_control_exchange", type: "fanout");

            var queueName = rmqChannel.QueueDeclare().QueueName;

            rmqChannel.QueueBind(queue: queueName,
                                 exchange: "shift_control_exchange",
                                 routingKey: "shift_control_key");

            var consumer = new EventingBasicConsumer(rmqChannel);

            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var data = Encoding.UTF8.GetString(body);

                JObject control_message = JObject.Parse(data);

                if (ea.BasicProperties.CorrelationId == "links_data")
                {
                    var is_master   = (bool)control_message["is_master"];
                    var routing_key = (string)control_message["routing_key"];

                    if (is_master && !string.IsNullOrWhiteSpace(routing_key))
                    {
                        if (this.StartingUp)
                        {
                            this.StartingUp         = false;
                            this.TopologyRoutingKey = routing_key;

                            OnStartCallback?.Invoke(routing_key);
                        }

                        if (routing_key != this.TopologyRoutingKey)
                        {
                            this.TopologyRoutingKey = routing_key;

                            OnTopologyQueueChangeCallback?.DynamicInvoke(routing_key);
                        }
                    }
                }
            };

            rmqChannel.BasicConsume(queue: queueName, consumer: consumer, autoAck: true);
        }