Beispiel #1
0
        ///////////////////////////////////////////////////////////////////////
        //                    RabbitMQ Methods                               //
        ///////////////////////////////////////////////////////////////////////

        /// <summary>
        /// This method creates a connection with RabbitMQ queue
        /// </summary>
        /// <param name="rabbitMQHost">IP used by RabbitMQ (Without port number)</param>
        /// <param name="queueName"></param>
        /// <param name="maxNumberOfMessages">The maximum number of messages that will be received from RabbitMQ</param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        private bool OpenRabbitQueue(string queueName, int maxnumberofMessages, RabbitMQParameters parameters, int messageTTL = 1209600000)
        {
            bool result = false;

            if (parameters != null)
            {
                PersistMessage = parameters.Persistent;

                RabbitHost       = parameters.RabbitMQHost;
                RabbitBatchValue = Convert.ToUInt16(maxnumberofMessages);
                RabbitQueueName  = queueName;

                RabbitUserName = parameters.UserName;
                RabbitPassword = parameters.Password;

                ConnectionFactory factory = new ConnectionFactory();
                factory.HostName = RabbitHost;
                factory.UserName = parameters.UserName;
                factory.Password = parameters.Password;

                RabbitConnection = factory.CreateConnection(parameters.ConnectionName);
                RabbitChannel    = RabbitConnection.CreateModel();

                Dictionary <string, object> args = new Dictionary <string, object>();
                args.Add("x-message-ttl", messageTTL);

                try
                {
                    if (!String.IsNullOrWhiteSpace(RabbitQueueName))
                    {
                        // Check queue exists
                        QueueDeclareOk resultado = RabbitChannel.QueueDeclarePassive(queueName);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    ErrorCode    = e_Exception;
                    ErrorMessage = "Queue '" + RabbitQueueName + "' not found::" + ex.Message;
                    RabbitDispose();
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// The method opens the queue
        /// </summary>
        public bool OpenQueue(string queuename, int maxnumberofmessages, RegionEndpoint regionendpoint, String AWSAccessKey = "", String AWSSecretKey = "", bool useRabbitMQ = false, RabbitMQParameters rabbitParameters = null)
        {
            ClearErrorInfo();

            IsValid     = false;
            UseRabbitMQ = useRabbitMQ;

            if (UseRabbitMQ)
            {
                IsValid = OpenRabbitQueue(queuename, maxnumberofmessages, rabbitParameters);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(queuename))
                {
                    if (!String.IsNullOrEmpty(AWSAccessKey))
                    {
                        queue = AWSClientFactory.CreateAmazonSQSClient(AWSAccessKey, AWSSecretKey, regionendpoint);
                    }
                    else
                    {
                        queue = AWSClientFactory.CreateAmazonSQSClient(regionendpoint);
                    }
                    try
                    {
                        // Get queue url
                        GetQueueUrlRequest sqsRequest = new GetQueueUrlRequest();
                        sqsRequest.QueueName = queuename;
                        queueurl             = queue.GetQueueUrl(sqsRequest);

                        // Format receive messages request
                        rcvMessageRequest                     = new ReceiveMessageRequest();
                        rcvMessageRequest.QueueUrl            = queueurl.QueueUrl;
                        rcvMessageRequest.MaxNumberOfMessages = maxnumberofmessages;

                        // Format the delete messages request
                        delMessageRequest          = new DeleteMessageRequest();
                        delMessageRequest.QueueUrl = queueurl.QueueUrl;

                        IsValid = true;
                    }
                    catch (Exception ex)
                    {
                        ErrorCode    = e_Exception;
                        ErrorMessage = ex.Message;
                    }
                }
            }

            return(IsValid);
        }