Exemplo n.º 1
0
        public void Not_republish_message_without_autoAck_false_if_nack_is_sent()
        {
            var queueName        = "amqp-conn-it-spec-simple-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);
            var amqpSink         = AmqpSink.CreateSimple(AmqpSinkSettings.Create(_connectionSettings).WithRoutingKey(queueName).WithDeclarations(queueDeclaration));
            var input            = new[] { "one", "two", "three", "four", "five" };

            Source.From(input).Select(ByteString.FromString).RunWith(amqpSink, _mat).Wait();

            var amqpSource = AmqpSource.CommittableSource(
                NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(queueDeclaration), bufferSize: 10);

            var result1 = amqpSource
                          .SelectAsync(1, async cm =>
            {
                await cm.Nack(requeue: false);
                return(cm);
            })
                          .Take(input.Length)
                          .RunWith(Sink.Seq <CommittableIncomingMessage>(), _mat);

            result1.Wait(TimeSpan.FromSeconds(3)).Should().BeTrue();

            var result2 = amqpSource
                          .SelectAsync(1, async cm =>
            {
                await cm.Ack();
                return(cm);
            })
                          .Take(input.Length)
                          .RunWith(Sink.Seq <CommittableIncomingMessage>(), _mat);

            result2.Wait(TimeSpan.FromSeconds(1)).Should().BeFalse();
        }
Exemplo n.º 2
0
        public void Set_routing_key_per_message_and_consume_them_in_the_same_process()
        {
            string GetRoutingKey(string s) => $"key.{s}";

            var exchangeName        = "amqp.topic." + Environment.TickCount;
            var queueName           = "amqp-conn-it-spec-simple-queue-" + Environment.TickCount;
            var exchangeDeclaration = ExchangeDeclaration.Create(exchangeName, "topic");
            var queueDeclaration    = QueueDeclaration.Create(queueName);
            var bindingDeclaration  = BindingDeclaration.Create(queueName, exchangeName).WithRoutingKey(GetRoutingKey("*"));

            var amqpSink = AmqpSink.Create(
                AmqpSinkSettings.Create(_connectionSettings)
                .WithExchange(exchangeName)
                .WithDeclarations(exchangeDeclaration, queueDeclaration, bindingDeclaration));

            var amqpSource = AmqpSource.AtMostOnceSource(
                NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(exchangeDeclaration, queueDeclaration, bindingDeclaration),
                bufferSize: 10);

            var input       = new[] { "one", "two", "three", "four", "five" };
            var routingKeys = input.Select(GetRoutingKey);

            Source.From(input)
            .Select(s => new OutgoingMessage(ByteString.FromString(s), false, false, routingKey: GetRoutingKey(s)))
            .RunWith(amqpSink, _mat)
            .Wait();

            var result = amqpSource
                         .Take(input.Length)
                         .RunWith(Sink.Seq <IncomingMessage>(), _mat)
                         .Result;

            result.Select(x => x.Envelope.RoutingKey).Should().Equal(routingKeys);
            result.Select(x => x.Bytes.ToString()).Should().Equal(input);
        }
Exemplo n.º 3
0
        public void Publish_and_consume_elements_through_a_simple_queue_again_in_the_same_process()
        {
            ExchangeDeclaration.Create("logs", "topic");

            //queue declaration
            var queueName        = "amqp-conn-it-spec-simple-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName).WithDurable(false).WithAutoDelete(true);

            //create sink
            var amqpSink = AmqpSink.CreateSimple(
                AmqpSinkSettings.Create(_connectionSettings)
                .WithRoutingKey(queueName)
                .WithDeclarations(queueDeclaration));

            //create source
            var amqpSource = AmqpSource.AtMostOnceSource(
                NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(queueDeclaration),
                bufferSize: 10);

            //run sink
            var input = new[] { "one", "two", "three", "four", "five" };

            Source.From(input).Select(ByteString.FromString).RunWith(amqpSink, _mat).Wait();

            //run source
            var result =
                amqpSource.Select(m => m.Bytes.ToString(Encoding.UTF8))
                .Take(input.Length)
                .RunWith(Sink.Seq <string>(), _mat);

            result.Wait(TimeSpan.FromSeconds(3));
            Assert.Equal(input, result.Result);
        }
Exemplo n.º 4
0
        public void Publish_and_consume_elements_through_a_simple_queue_again_in_the_same_process_without_autoAck()
        {
            var queueName        = "amqp-conn-it-spec-simple-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);
            var amqpSink         = AmqpSink.CreateSimple(AmqpSinkSettings.Create(_connectionSettings).WithRoutingKey(queueName).WithDeclarations(queueDeclaration));

            //#create-source-withoutautoack
            var amqpSource = AmqpSource.CommittableSource(
                NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(queueDeclaration),
                bufferSize: 10);

            //#create-source-withoutautoack

            var input = new[] { "one", "two", "three", "four", "five" };

            Source.From(input).Select(ByteString.FromString).RunWith(amqpSink, _mat).Wait();

            //#run-source-withoutautoack
            var result = amqpSource
                         .SelectAsync(1, async cm =>
            {
                await cm.Ack();
                return(cm);
            })
                         .Take(input.Length)
                         .RunWith(Sink.Seq <CommittableIncomingMessage>(), _mat);

            //#run-source-withoutautoack
            result.Result.Select(x => x.Message.Bytes.ToString()).Should().Equal(input);
        }
Exemplo n.º 5
0
        static async Task Main(string[] args)
        {
            using var actorSystem = ActorSystem.Create("poc");
            using var mat         = actorSystem.Materializer();

            var connectionSettings = AmqpConnectionDetails.Create("localhost", 5672)
                                     .WithCredentials(AmqpCredentials.Create("mirero", "system"))
                                     .WithAutomaticRecoveryEnabled(true)
                                     .WithNetworkRecoveryInterval(TimeSpan.FromSeconds(1));

            var queueName = "Amqp_Test_1";

            var queueDeclation = QueueDeclaration.Create(queueName)
                                 .WithDurable(false)
                                 .WithAutoDelete(true);

            var amqpSink = AmqpSink.CreateSimple(AmqpSinkSettings.Create(connectionSettings)
                                                 .WithRoutingKey(queueName)
                                                 .WithDeclarations(queueDeclation));

            var amqpSource = AmqpSource.CommittableSource(NamedQueueSourceSettings.Create(connectionSettings, queueName)
                                                          .WithDeclarations(queueDeclation), 1);

            var input = new[] { "one", "two", "three", "four", "five" };

            await Source.From(input)
            .Select(ByteString.FromString)
            .RunWith(amqpSink, mat);

            await actorSystem.Terminate();
        }
Exemplo n.º 6
0
        public void Publish_from_one_source_and_consume_elements_with_multiple_sinks()
        {
            var queueName        = "amqp-conn-it-spec-work-queues-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);
            var amqpSink         = AmqpSink.CreateSimple(
                AmqpSinkSettings.Create(_connectionSettings)
                .WithRoutingKey(queueName)
                .WithDeclarations(queueDeclaration));

            var input = new[] { "one", "two", "three", "four", "five" };

            Source.From(input).Select(ByteString.FromString).RunWith(amqpSink, _mat);

            var mergedSources = Source.FromGraph(GraphDsl.Create(b =>
            {
                const int count = 3;
                var merge       = b.Add(new Merge <IncomingMessage>(count));
                for (var n = 0; n < count; n++)
                {
                    var source = b.Add(
                        AmqpSource.AtMostOnceSource(
                            NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(queueDeclaration),
                            bufferSize: 1));

                    b.From(source.Outlet).To(merge.In(n));
                }

                return(new SourceShape <IncomingMessage>(merge.Out));
            }));

            var result = mergedSources.Select(x => x.Bytes.ToString()).Take(input.Length).RunWith(Sink.Seq <string>(), _mat);

            result.Result.OrderBy(x => x).ToArray().Should().Equal(input.OrderBy(x => x).ToArray());
        }
        private void ConsumeQueue(object state)
        {
            var queueDeclaration = new QueueDeclaration
            {
                Name = _consumerSettings.ApplicationName,
            };

            try
            {
                var model = _consumer.GetModelInQueue(queueDeclaration);
                if (model != null && model.Any())
                {
                    Context.CurrentConfigurations = model.Select(x => x.Clone() as ConfigurationDto).ToList();
                    SerializeToFile(_consumerSettings.LocalSettingsPath, new LocalSettings {
                        Configurations = model
                    });
                }
                else
                {
                    var localSettings = DeserializeFileContent <LocalSettings>(_consumerSettings.LocalSettingsPath);
                    Context.CurrentConfigurations = localSettings?.Configurations?.Select(x => x.Clone() as ConfigurationDto).ToList();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemplo n.º 8
0
        public Task DeclareQueueAsync(QueueDeclaration queue)
        {
            if (IsDeclared(queue))
            {
                return(_completed);
            }

            var scheduled = new ScheduledQueueTask(queue);

            _topologyTasks.Enqueue(scheduled);
            EnsureWorker();
            return(scheduled.TaskCompletionSource.Task);
        }
        protected override Task DeclareQueueAsync(QueueDeclaration queue, IPipeContext context, CancellationToken token)
        {
            var policy = context.GetPolicy(PolicyKeys.QueueDeclare);

            return(policy.ExecuteAsync(
                       action: ct => base.DeclareQueueAsync(queue, context, ct),
                       cancellationToken: token,
                       contextData: new Dictionary <string, object>
            {
                [RetryKey.TopologyProvider] = Topology,
                [RetryKey.QueueDeclaration] = queue,
                [RetryKey.PipeContext] = context,
                [RetryKey.CancellationToken] = token,
            }));
        }
 private static Policy GetQueuePolicy()
 {
     return(Policy
            .Handle <OperationInterruptedException>()
            .RetryAsync(async(e, retryCount, context) =>
     {
         var defaultQueueCfg = context.GetPipeContext().GetClientConfiguration().Queue;
         var topology = context.GetTopologyProvider();
         var queue = new QueueDeclaration(defaultQueueCfg)
         {
             Name = context.GetQueueName()
         };
         await topology.DeclareQueueAsync(queue);
     }));
 }
Exemplo n.º 11
0
        public void Not_fail_on_a_fast_producer_and_a_slow_consumer()
        {
            var queueName        = "amqp-conn-it-spec-simple-queue-2-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);

            var amqpSource = AmqpSource.AtMostOnceSource(
                NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(queueDeclaration), bufferSize: 2);

            var amqpSink   = AmqpSink.CreateSimple(AmqpSinkSettings.Create(_connectionSettings).WithRoutingKey(queueName).WithDeclarations(queueDeclaration));
            var publisher  = this.CreatePublisherProbe <ByteString>();
            var subscriber = this.CreateSubscriberProbe <IncomingMessage>();

            amqpSink.AddAttributes(Attributes.CreateInputBuffer(1, 1)).RunWith(Source.FromPublisher(publisher), _mat);
            amqpSource.AddAttributes(Attributes.CreateInputBuffer(1, 1)).RunWith(Sink.FromSubscriber(subscriber), _mat);

            // note that this essentially is testing rabbitmq just as much as it tests our sink and source
            publisher.EnsureSubscription();
            subscriber.EnsureSubscription();

            publisher.ExpectRequest().Should().Be(1);
            publisher.SendNext(ByteString.FromString("one"));

            publisher.ExpectRequest();
            publisher.SendNext(ByteString.FromString("two"));

            publisher.ExpectRequest();
            publisher.SendNext(ByteString.FromString("three"));

            publisher.ExpectRequest();
            publisher.SendNext(ByteString.FromString("four"));

            publisher.ExpectRequest();
            publisher.SendNext(ByteString.FromString("five"));

            subscriber.Request(4);
            subscriber.ExpectNext().Bytes.ToString().Should().Be("one");
            subscriber.ExpectNext().Bytes.ToString().Should().Be("two");
            subscriber.ExpectNext().Bytes.ToString().Should().Be("three");
            subscriber.ExpectNext().Bytes.ToString().Should().Be("four");

            subscriber.Request(1);
            subscriber.ExpectNext().Bytes.ToString().Should().Be("five");

            subscriber.Cancel();
            publisher.SendComplete();
        }
Exemplo n.º 12
0
        public async Task Should_Use_Custom_Policy()
        {
            var defaultCalled = false;
            var customCalled  = false;
            var defaultPolicy = Policy
                                .Handle <Exception>()
                                .FallbackAsync(ct =>
            {
                defaultCalled = true;
                return(Task.FromResult(0));
            });
            var declareQueuePolicy = Policy
                                     .Handle <OperationInterruptedException>()
                                     .RetryAsync(async(e, retryCount, ctx) =>
            {
                customCalled        = true;
                var defaultQueueCfg = ctx.GetPipeContext().GetClientConfiguration().Queue;
                var topology        = ctx.GetTopologyProvider();
                var queue           = new QueueDeclaration(defaultQueueCfg)
                {
                    Name = ctx.GetQueueName(), Durable = false
                };
                await topology.DeclareQueueAsync(queue);
            });

            var options = new ZyRabbitOptions
            {
                Plugins = p => p.UsePolly(c => c
                                          .UsePolicy(defaultPolicy)
                                          .UsePolicy(declareQueuePolicy, PolicyKeys.QueueBind)
                                          )
            };

            using (var client = ZyRabbitFactory.CreateTestClient(options))
            {
                await client.SubscribeAsync <BasicMessage>(
                    message => Task.FromResult(0),
                    ctx => ctx.UseSubscribeConfiguration(cfg => cfg
                                                         .Consume(c => c
                                                                  .FromQueue("does_not_exist"))
                                                         ));
            }

            Assert.True(customCalled);
            Assert.False(defaultCalled, "The custom retry policy should be called");
        }
 private void PublishQueue(object state)
 {
     try
     {
         var getConfigurationsTask = _configurationRepository.GetListAsync(x => x.IsActive);
         getConfigurationsTask.Wait();
         var groupedConfigurations = getConfigurationsTask.Result.GroupBy(x => x.ApplicationName);
         foreach (var configurationGroup in groupedConfigurations)
         {
             var configurationsToQueue = configurationGroup.Select(config => new ConfigurationDto
             {
                 Key   = config.Key,
                 Value = config.Value,
                 Type  = config.Type,
             }).ToList();
             var queueDeclaration = new QueueDeclaration
             {
                 Name = configurationGroup.Key,
             };
             //PublisherProperties properties = new PublisherProperties
             //{
             //    Expiration = $"{1000 * _publisherInterval}"
             //};
             if (ConfigurationsSnapShot != null)
             {
                 if ((ConfigurationsSnapShot.ContainsKey(queueDeclaration.Name) && !configurationsToQueue.SequenceEqual(ConfigurationsSnapShot[queueDeclaration.Name])) ||
                     !ConfigurationsSnapShot.ContainsKey(queueDeclaration.Name))
                 {
                     ConfigurationsSnapShot[queueDeclaration.Name] = configurationsToQueue.Select(x => x.Clone() as ConfigurationDto).ToList();
                     _publisher.SendModelToQueue(configurationsToQueue, queueDeclaration);
                 }
             }
             else
             {
                 ConfigurationsSnapShot = new Dictionary <string, IList <ConfigurationDto> >();
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Exemplo n.º 14
0
        public void PublishAndConsume()
        {
            var connectionSettings = AmqpConnectionDetails.Create("localhost", 5672).WithAutomaticRecoveryEnabled(true).WithNetworkRecoveryInterval(TimeSpan.FromSeconds(1));

            var exchange = ExchangeDeclaration.Create("logs", "topic");

            //queue declaration
            var queueName        = "amqp-conn-it-spec-simple-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName).WithDurable(false).WithAutoDelete(true);



            //create sink
            var amqpSink = AmqpSink.CreateSimple(
                AmqpSinkSettings.Create(connectionSettings)
                .WithRoutingKey(queueName)
                .WithDeclarations(queueDeclaration));

            //create source
            int bufferSize = 10;
            var amqpSource = AmqpSource.Create(
                NamedQueueSourceSettings.Create(DefaultAmqpConnection.Instance, queueName)
                .WithDeclarations(queueDeclaration),
                bufferSize);

            //run sink
            var input = new List <string> {
                "one", "two", "three", "four", "five"
            };

            Source.From(input).Select(ByteString.FromString).RunWith(amqpSink, _materializer).Wait();

            //run source
            var result =
                amqpSource.Select(m => m.Bytes.ToString(Encoding.UTF8))
                .Take(input.Count)
                .RunWith(Sink.Seq <string>(), _materializer);

            result.Wait(TimeSpan.FromSeconds(3));

            Assert.Equal(input, result.Result);
        }
Exemplo n.º 15
0
        public static Source <CommittableIncomingMessage, Akka.NotUsed> CommittableQueue(Action <SimpleQueueOptions> opt)
        {
            var option = new SimpleQueueOptions();

            opt.Invoke(option);

            var connectionSettings = AmqpConnectionDetails.Create(option.HostAndPorts.First().Host, option.HostAndPorts.First().Port)
                                     .WithHostsAndPorts(option.HostAndPorts.First(), option.HostAndPorts.ToArray())
                                     .WithCredentials(AmqpCredentials.Create(option.UserName, option.Password))
                                     .WithAutomaticRecoveryEnabled(true)
                                     .WithNetworkRecoveryInterval(TimeSpan.FromSeconds(1))
                                     .WithVirtualHost(option.VirtualHost);

            var queueDeclaration = QueueDeclaration.Create(option.QueueName)
                                   .WithDurable(false)
                                   .WithAutoDelete(false);

            return(AmqpSource.CommittableSource(NamedQueueSourceSettings.Create(connectionSettings, option.QueueName)
                                                .WithDeclarations(queueDeclaration), 1));
        }
Exemplo n.º 16
0
        public void Keep_connection_open_if_downstream_closes_and_there_are_pending_acks()
        {
            var queueName        = "amqp-conn-it-spec-simple-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);

            var amqpSink = AmqpSink.CreateSimple(AmqpSinkSettings.Create(_connectionSettings).WithRoutingKey(queueName).WithDeclarations(queueDeclaration));

            var amqpSource = AmqpSource.CommittableSource(
                NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(queueDeclaration), bufferSize: 10);

            var input = new[] { "one", "two", "three", "four", "five" };

            Source.From(input).Select(ByteString.FromString).RunWith(amqpSink, _mat).Wait();
            var result = amqpSource.Take(input.Length).RunWith(Sink.Seq <CommittableIncomingMessage>(), _mat);

            foreach (var cm in result.Result)
            {
                cm.Ack().Wait();
            }
        }
        public async Task Should_Invoke_Queue_Declare_Policy_With_Correct_Context()
        {
            var     topology         = new Mock <ITopologyProvider>();
            var     queueDeclaration = new QueueDeclaration();
            var     policyCalled     = false;
            Context capturedContext  = null;

            topology
            .SetupSequence(t => t.DeclareQueueAsync(queueDeclaration))
            .Throws(new OperationInterruptedException(null))
            .Returns(Task.CompletedTask);

            var context = new PipeContext
            {
                Properties = new Dictionary <string, object>
                {
                    { PipeKey.QueueDeclaration, queueDeclaration }
                }
            };

            context.UsePolicy(Policy
                              .Handle <OperationInterruptedException>()
                              .RetryAsync((exception, retryCount, pollyContext) =>
            {
                policyCalled    = true;
                capturedContext = pollyContext;
            }), PolicyKeys.QueueDeclare);
            var middleware = new QueueDeclareMiddleware(topology.Object, NullLogger <QueueDeclareMiddleware> .Instance)
            {
                Next = new NoOpMiddleware()
            };

            /* Test */
            await middleware.InvokeAsync(context);

            /* Assert */
            Assert.True(policyCalled, "Should call policy");
            Assert.Equal(context, capturedContext[RetryKey.PipeContext]);
            Assert.Equal(queueDeclaration, capturedContext[RetryKey.QueueDeclaration]);
            Assert.Equal(topology.Object, capturedContext[RetryKey.TopologyProvider]);
        }
Exemplo n.º 18
0
        public static Sink <ByteString, Task> Simple(Action <SimpleQueueOptions> opt)
        {
            var option = new SimpleQueueOptions();

            opt.Invoke(option);

            var connectionSettings = AmqpConnectionDetails.Create(option.HostAndPorts.First().Host, option.HostAndPorts.First().Port)
                                     .WithHostsAndPorts(option.HostAndPorts.First(), option.HostAndPorts.ToArray())
                                     .WithCredentials(AmqpCredentials.Create(option.UserName, option.Password))
                                     .WithAutomaticRecoveryEnabled(true)
                                     .WithNetworkRecoveryInterval(TimeSpan.FromSeconds(1))
                                     .WithVirtualHost(option.VirtualHost ?? "/");

            var queueDeclaration = QueueDeclaration.Create(option.QueueName)
                                   .WithDurable(false)
                                   .WithAutoDelete(false);

            return(AmqpSink.CreateSimple(AmqpSinkSettings.Create(connectionSettings)
                                         .WithRoutingKey(option.QueueName)
                                         .WithDeclarations(queueDeclaration)));
        }
Exemplo n.º 19
0
        public void Publish_via_RPC_which_expects_2_responses_per_message_and_then_consume_through_a_simple_queue_again_in_the_same_process()
        {
            var queueName        = "amqp-conn-it-spec-rpc-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);

            var amqpRpcFlow = AmqpRpcFlow.CreateSimple(
                AmqpSinkSettings.Create(_connectionSettings).WithRoutingKey(queueName).WithDeclarations(queueDeclaration), repliesPerMessage: 2);

            var amqpSource = AmqpSource.AtMostOnceSource(NamedQueueSourceSettings.Create(_connectionSettings, queueName), bufferSize: 1);

            var input = new[] { "one", "two", "three", "four", "five" };

            var t =
                Source.From(input)
                .Select(ByteString.FromString)
                .ViaMaterialized(amqpRpcFlow, Keep.Right)
                .ToMaterialized(this.SinkProbe <ByteString>(), Keep.Both)
                .Run(_mat);

            var rpcQueueF = t.Item1;
            var probe     = t.Item2;

            rpcQueueF.Result.Should().NotBeNullOrWhiteSpace("RPC flow materializes into response queue name");

            var amqpSink = AmqpSink.ReplyTo(AmqpReplyToSinkSettings.Create(_connectionSettings));

            amqpSource
            .SelectMany(b =>
                        new[]
            {
                new OutgoingMessage(b.Bytes.Concat(ByteString.FromString("a")), false, false, b.Properties),
                new OutgoingMessage(b.Bytes.Concat(ByteString.FromString("aa")), false, false, b.Properties)
            })
            .RunWith(amqpSink, _mat);

            probe
            .Request(10)
            .ExpectNextUnorderedN(input.SelectMany(s => new[] { ByteString.FromString(s + "a"), ByteString.FromString(s + "aa") }))
            .ExpectComplete();
        }
Exemplo n.º 20
0
        public void Publish_via_RPC_and_then_consume_through_a_simple_queue_again_in_the_same_process_without_autoAck()
        {
            var queueName        = "amqp-conn-it-spec-rpc-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);

            var input = new[] { "one", "two", "three", "four", "five" };

            var amqpRpcFlow = AmqpRpcFlow.CommittableFlow(
                AmqpSinkSettings.Create(_connectionSettings).WithRoutingKey(queueName).WithDeclarations(queueDeclaration), bufferSize: 10);

            var t =
                Source.From(input)
                .Select(ByteString.FromString)
                .Select(bytes => new OutgoingMessage(bytes, false, false))
                .ViaMaterialized(amqpRpcFlow, Keep.Right)
                .SelectAsync(1, async cm =>
            {
                await cm.Ack();
                return(cm.Message);
            })
                .ToMaterialized(this.SinkProbe <IncomingMessage>(), Keep.Both)
                .Run(_mat);

            var rpcQueueF = t.Item1;
            var probe     = t.Item2;

            rpcQueueF.Wait();

            var amqpSink = AmqpSink.ReplyTo(AmqpReplyToSinkSettings.Create(_connectionSettings));

            var amqpSource = AmqpSource.AtMostOnceSource(NamedQueueSourceSettings.Create(_connectionSettings, queueName), bufferSize: 1);

            amqpSource
            .Select(b => new OutgoingMessage(b.Bytes, false, false, b.Properties))
            .RunWith(amqpSink, _mat);

            probe.ToStrict(TimeSpan.FromSeconds(3)).Select(x => x.Bytes.ToString()).Should().Equal(input);
        }
Exemplo n.º 21
0
        public void Publish_elements_with_flow_then_consume_them_with_source()
        {
            ExchangeDeclaration.Create("logs", "topic");

            var queueName        = "amqp-conn-it-spec-simple-queue-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName).WithDurable(false).WithAutoDelete(true);

            var amqpFlow =
                AmqpFlow.Create <string>(
                    AmqpSinkSettings.Create(_connectionSettings)
                    .WithRoutingKey(queueName)
                    .WithDeclarations(queueDeclaration));


            var input = new[] { "one", "two", "three", "four", "five" };

            var passedThrough =
                Source.From(input)
                .Select(x => (new OutgoingMessage(ByteString.FromString(x), true, true), x))
                .Via(amqpFlow)
                .RunWith(Sink.Seq <string>(), _mat)
                .Result
                .ToArray();

            Assert.Equal(input, passedThrough, StringComparer.InvariantCulture);

            var consumed =
                AmqpSource.AtMostOnceSource(
                    NamedQueueSourceSettings.Create(_connectionSettings, queueName).WithDeclarations(queueDeclaration),
                    bufferSize: 10)
                .Select(m => m.Bytes.ToString(Encoding.UTF8))
                .Take(input.Length)
                .RunWith(Sink.Seq <string>(), _mat)
                .Result
                .ToArray();

            Assert.Equal(input, consumed, StringComparer.InvariantCulture);
        }
Exemplo n.º 22
0
        private void DeclareQueue(QueueDeclaration queue)
        {
            if (IsDeclared(queue))
            {
                return;
            }

            _logger.Info("Declaring queue {queueName}.", queue.Name);

            var channel = GetOrCreateChannel();

            channel.QueueDeclare(
                queue.Name,
                queue.Durable,
                queue.Exclusive,
                queue.AutoDelete,
                queue.Arguments);

            if (queue.AutoDelete)
            {
                _initQueues.Add(queue.Name);
            }
        }
Exemplo n.º 23
0
        public void Declare_connection_that_does_not_require_server_acks()
        {
            //var connectionSettings = AmqpConnectionDetails.Create("localhost", 5672);

            var queueName        = "amqp-conn-it-spec-fire-and-forget-" + Environment.TickCount;
            var queueDeclaration = QueueDeclaration.Create(queueName);

            var amqpSink = AmqpSink.CreateSimple(AmqpSinkSettings.Create(_connectionSettings)
                                                 .WithRoutingKey(queueName)
                                                 .WithDeclarations(queueDeclaration));

            var amqpSource = AmqpSource.CommittableSource(NamedQueueSourceSettings.Create(_connectionSettings, queueName)
                                                          .WithAckRequired(false)
                                                          .WithDeclarations(queueDeclaration), bufferSize: 10);

            var input = new[] { "one", "two", "three", "four", "five" };

            Source.From(input).Select(ByteString.FromString).RunWith(amqpSink, _mat).Wait();

            var result = amqpSource.Take(input.Length).RunWith(Sink.Seq <CommittableIncomingMessage>(), _mat).Result;

            result.Select(x => x.Message.Bytes.ToString(Encoding.UTF8)).Should().Equal(input);
        }
 protected virtual void SaveToContext(IPipeContext context, QueueDeclaration declaration)
 {
     SaveToContextAction?.Invoke(context, declaration);
 }
 protected virtual Task DeclareQueueAsync(QueueDeclaration queue, IPipeContext context, CancellationToken token)
 {
     return(Topology.DeclareQueueAsync(queue));
 }
Exemplo n.º 26
0
 public ScheduledQueueTask(QueueDeclaration queue)
 {
     Configuration = queue;
 }
Exemplo n.º 27
0
 public bool IsDeclared(QueueDeclaration queue)
 {
     return(queue.IsDirectReplyTo() || _initQueues.Contains(queue.Name));
 }
Exemplo n.º 28
0
 protected virtual void AlignConsumerConfig(ConsumeConfiguration consumeConfig, QueueDeclaration declaration)
 {
     if (consumeConfig == null)
     {
         return;
     }
     consumeConfig.QueueName = declaration.Name;
 }
Exemplo n.º 29
0
 public static async Task DeclareQueueAsync(this IBusClient client, QueueDeclaration declaration, CancellationToken ct = default(CancellationToken))
 {
     await client.InvokeAsync(DeclareQueueAction, ctx => ctx.Properties.Add(PipeKey.QueueDeclaration, declaration), ct);
 }
Exemplo n.º 30
0
 protected virtual void AppendSuffix(QueueDeclaration queue, string suffix)
 {
     AppendSuffixAction?.Invoke(queue, suffix);
 }