コード例 #1
0
        public static RabbitMqEndpoint NewOutboundPublisher(string name, string type, string routingKeyOrTopicName, PublisherParameters p = null, MessageParameters defaultMessageParameters = null, CancellationToken token = default(CancellationToken))
        {
            var exchangeType = GetFullExchangeType(type);

            p ??= new PublisherParameters()
            {
                AcceptReplies = false, AutoDelete = true, Durable = false, EnableConfirmSelect = false, ReplyQueueTtl = 0, Ttl = 6000
            };
            defaultMessageParameters ??= new MessageParameters()
            {
                AutoAck = false, Durable = false, Mandatory = false, Persistent = false, Priority = 3, Resilient = false, TimeOut = 6000
            };

            var ret = new RabbitMqEndpoint
            {
                Channel                  = _RabbitOut.CreateModel(),
                EndpointType             = EndpointTypeEnum.Publisher,
                ExchangeName             = name,
                QueueName                = DefineQueueName(name, exchangeType, "", false),
                RoutingKeyOrTopicName    = routingKeyOrTopicName,
                ExchangeId               = Guid.NewGuid(),
                LocalCancellationToken   = _RabbitOutCts?.Token ?? token,
                ChannelParameters        = p,
                ExchangeType             = GetFullExchangeType(type),
                DefaultMessageParameters = defaultMessageParameters
            };

            VerboseLoggingHandler.Log($"Building a {exchangeType} outbound publisher, name='{name}', routingKeyOrTopicName='{routingKeyOrTopicName}', durable='{p.Durable}', ttl='{p.Ttl}', autoDelete='{p.AutoDelete}', acceptReplies='{p.AcceptReplies}', confirmSelect='{p.EnableConfirmSelect}'");
            ret.ConnectToExchange();

            if (p.EnableConfirmSelect)
            {
                ret.Channel.ConfirmSelect();
            }

            if (p.AcceptReplies)
            {
                VerboseLoggingHandler.Log($"Building a return consumer, ExchangeId='{ret.ExchangeId}', replyQueueTtl='{p.ReplyQueueTtl}'");
                ret.DefineRpcConsumer(ret.ExchangeName, ret.ExchangeId, p.ReplyQueueTtl);
            }

            VerboseLoggingHandler.Log("Exchange ready");
            return(ret);
        }
コード例 #2
0
        public static RabbitMqEndpoint NewInboundConsumer(string name, string type, string routingKeyOrTopicName = "", ConsumerParameters p = null, CancellationToken token = default(CancellationToken))
        {
            p ??= new ConsumerParameters();
            var exchangeType = GetFullExchangeType(type);
            var ret          = new RabbitMqEndpoint
            {
                Channel                = _RabbitIn.CreateModel(),
                EndpointType           = EndpointTypeEnum.Consumer,
                ExchangeName           = name,
                LocalCancellationToken = _RabbitInCts?.Token ?? token,
                QueueTtlValues         = new Dictionary <string, int>(),
                ChannelParameters      = p,
                ExchangeType           = GetFullExchangeType(type),
                QueueName              = DefineQueueName(name, exchangeType, routingKeyOrTopicName, true),
            };

            VerboseLoggingHandler.Log($"Building a {exchangeType} inbound consumer, name='{name}', routingKeyOrTopicName='{routingKeyOrTopicName}', durable='{p.Durable}', ttl='{p.Ttl}', autoDelete='{p.AutoDelete}', autoAckMode='{p.AutoAckMode}'");
            ret.ConnectToExchange();

            try
            {
                VerboseLoggingHandler.Log($"Binding queue '{ret.QueueName}'");
                ret.Channel.QueueBind(ret.QueueName, name, routingKeyOrTopicName, new Dictionary <string, object>());
            }
            catch (Exception e)
            {
                VerboseLoggingHandler.Log(e);
                throw e;
            }

            VerboseLoggingHandler.Log("Initiating the consumer");
            ret.Consumer = new AsyncEventingBasicConsumer(ret.Channel);

            ret.Consumer.Received += ret.OnIncomingMessageAsync;

            VerboseLoggingHandler.Log("Consumer ready");

            return(ret);
        }