public async Task <HostReceiveEndpointHandle> ConnectReceiveEndpoint(string queueName, Action <IReceiveEndpointConfigurator> configure = null)
        {
            var handle = _host.ConnectReceiveEndpoint(queueName, configurator => configure?.Invoke(configurator));

            ActiveReceiveEndpoints.Add(handle);
            return(handle);
        }
        HostReceiveEndpointHandle IReceiveConnector <IRabbitMqReceiveEndpointConfigurator> .ConnectReceiveEndpoint(IEndpointDefinition definition,
                                                                                                                   IEndpointNameFormatter endpointNameFormatter, Action <IRabbitMqReceiveEndpointConfigurator> configureEndpoint)
        {
            if (_host == null)
            {
                throw new InvalidOperationException("The host is not ready.");
            }

            return(_host.ConnectReceiveEndpoint(definition, endpointNameFormatter, configureEndpoint));
        }
 static HostReceiveEndpointHandle ConnectResponseEndpoint(IRabbitMqHost host)
 {
     return(host.ConnectReceiveEndpoint(host.Topology.CreateTemporaryResponseQueueName(), x =>
     {
         x.Durable = false;
         x.AutoDelete = true;
     }));
 }
Exemple #4
0
        public void AddUpdateConsumer(DocumentInfo i)
        {
            host.ConnectReceiveEndpoint(i.Type, e =>
            {
                e.BindMessageExchanges = false;
                e.Consumer(() => new UpdateDocument(documentRepository));

                //document is queue name
                e.Bind("updatedoc", s =>
                {
                    s.RoutingKey   = i.Type;
                    s.ExchangeType = ExchangeType.Direct;
                });
                e.Durable    = true;
                e.AutoDelete = true;
            });
        }
        public override async Task ConnectConsumerAsync(string queueName, ICommandService commandService,
                                                        CancellationToken cancellationToken)
        {
            if (!(commandService is RabbitMqCommandService commandServiceCasted))
            {
                throw new Exception("Unsupported service");
            }

            if (_handles.ContainsKey(queueName))
            {
                throw new Exception("Cant add second consumer with the same queueName");
            }

            var handle = _host.ConnectReceiveEndpoint(queueName,
                                                      x => { x.Consumer(() => commandServiceCasted); });

            await handle.Ready;

            _handles.Add(queueName, handle);
        }
Exemple #6
0
        /// <summary>
        /// Creates a request client that uses the bus to publish a request.
        /// </summary>
        /// <typeparam name="TRequest">The request type</typeparam>
        /// <typeparam name="TResponse">The response type</typeparam>
        /// <param name="timeout">The timeout before the request is cancelled</param>
        /// <param name="callback">Callback when the request is sent</param>
        /// <param name="ttl">The time that the request will live for</param>
        /// <param name="host"></param>
        /// <param name="publishEndpoint"></param>
        /// <returns></returns>
        public static async Task <IRequestClient <TRequest, TResponse> > CreatePublishRequestClient <TRequest, TResponse>(this IRabbitMqHost host,
                                                                                                                          IPublishEndpoint publishEndpoint, TimeSpan timeout, TimeSpan?ttl = default(TimeSpan?), Action <SendContext <TRequest> > callback = null)
            where TRequest : class
            where TResponse : class
        {
            var endpoint = await host.ConnectReceiveEndpoint(host.GetTemporaryQueueName("response")).ConfigureAwait(false);

            var ready = await endpoint.Ready.ConfigureAwait(false);

            return(new PublishRequestClient <TRequest, TResponse>(publishEndpoint, ready.ReceiveEndpoint, ready.InputAddress, timeout, ttl, callback));
        }
        /// <summary>
        /// Creates a request client factory which can be used to create a request client per message within a consume context.
        /// </summary>
        /// <typeparam name="TRequest"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="host">The host for the response endpoint</param>
        /// <param name="address">The service address</param>
        /// <param name="timeout">The request timeout</param>
        /// <param name="timeToLive">The request time to live</param>
        /// <param name="callback">Customize the send context</param>
        /// <returns></returns>
        public static async Task <IRequestClientFactory <TRequest, TResponse> > CreateRequestClientFactory <TRequest, TResponse>(this IRabbitMqHost host,
                                                                                                                                 Uri address, TimeSpan timeout, TimeSpan?timeToLive = default(TimeSpan?), Action <SendContext <TRequest> > callback = null)
            where TRequest : class
            where TResponse : class
        {
            var endpoint = host.ConnectReceiveEndpoint(host.Topology.CreateTemporaryQueueName("response"));

            var ready = await endpoint.Ready.ConfigureAwait(false);

            return(new MessageRequestClientFactory <TRequest, TResponse>(endpoint, ready.ReceiveEndpoint, ready.InputAddress, address, timeout, timeToLive, callback));
        }
        //public static void AddDocumentEndpoint<TConsumer>(this IRabbitMqHost host, string epName) where TConsumer : ReceiveDocument,  IConsumer, new()
        //{
        //    host.ConnectReceiveEndpoint(epName, e =>
        //    {
        //        e.BindMessageExchanges = false;
        //        e.Consumer<TConsumer>();

        //        //document is queue name
        //        e.Bind("document", s =>
        //        {
        //            s.RoutingKey = epName;
        //            s.ExchangeType = ExchangeType.Direct;
        //        });
        //        e.Durable = true;
        //        e.AutoDelete = true;


        //    });
        //}

        public static void AddDocumentEndpoint <TConsumer>(this IRabbitMqHost host, string epName, IDocumentRepository repo) where TConsumer : ReceiveDocument, IConsumer, new()
        {
            host.ConnectReceiveEndpoint(epName, e =>
            {
                e.BindMessageExchanges = false;
                e.Consumer <TConsumer>(() => Activator.CreateInstance(typeof(TConsumer), new object[] { repo }) as TConsumer);

                //document is queue name
                e.Bind("document", s =>
                {
                    s.RoutingKey   = epName;
                    s.ExchangeType = ExchangeType.Direct;
                });
                e.Durable    = true;
                e.AutoDelete = true;
            });
        }
Exemple #9
0
        static void Subscribe(IComponentContext context, string key)
        {
            var queuename = $"testcase-R-{key}";
            var handle    = _host.ConnectReceiveEndpoint(queuename, e =>
            {
                e.BindMessageExchanges = false;
                e.Consumer <MyConsumer>(context); //<-- autofac is used to more closely represent the realworld scenario
                e.Bind(ExchangeName, x =>
                {
                    x.RoutingKey   = RoutingKeyConvention(key);
                    x.ExchangeType = ExchangeType.Direct;
                    x.Durable      = true;
                });
                //this and SetEntityName are mutually exclusive, causes vague exceptions if you do.
                //Turn this on, and SetEntityName off. And you do receive messages. But routingkey constraints
                //are not enforced. So each consumer receives all published messages. Instead of only the ones belonging to its topic
                //e.Bind<MyMessage>();
            });

            handle.Ready.ConfigureAwait(false).GetAwaiter().GetResult();
        }