Exemplo n.º 1
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.º 2
0
        public async Task Publish_and_consume_elements_through_a_simple_queue_again_in_the_same_process()
        {
            Connection.DisableServerCertValidation = true;
            Trace.TraceLevel    = TraceLevel.Frame;
            Trace.TraceListener = (l, f, a) =>
                                  Output.WriteLine(DateTime.Now.ToString("[hh:mm:ss.fff]") + " " + string.Format(f, a));

            var connection = new Connection(_address);
            var session    = new Session(connection);

            var queueName        = "simple-v1-queue-test" + Guid.NewGuid();
            var senderLinkName   = "amqp-v1-conn-test-sender";
            var receiverLinkName = "amqp-v1-conn-test-receiver";

            //create sink and source
            var amqpSink   = AmqpSink.Create(new NamedQueueSinkSettings <string>(session, senderLinkName, queueName, _serializer));
            var amqpSource = AmqpSource.Create(new NamedQueueSourceSettings <string>(session, receiverLinkName, queueName, 200, _serializer));

            //run sink
            var input = new[] { "one", "two", "three", "four", "five" };
            await Source.From(input).RunWith(amqpSink, _materializer);

            //run source
            var result = amqpSource
                         .Take(input.Length)
                         .RunWith(Sink.Seq <string>(), _materializer);

            await result;

            Assert.True(result.IsCompleted);
            Assert.Equal(input, result.Result);

            await session.CloseAsync();

            await connection.CloseAsync();
        }