Ejemplo n.º 1
0
        public void TestUnAckMessageRedeliveryWithReceive()
        {
            var topic   = $"persistent://public/default/async-unack-redelivery-{Guid.NewGuid()}";
            var builder = new ConsumerConfigBuilder <byte[]>();

            builder.Topic(topic);
            builder.SubscriptionName("sub-TestUnAckMessageRedeliveryWithReceive");
            builder.AckTimeout(TimeSpan.FromMilliseconds(8000));
            builder.ForceTopicCreation(true);
            builder.AcknowledgmentGroupTime(0);
            builder.SubscriptionType(Protocol.Proto.CommandSubscribe.SubType.Shared);
            var consumer = _client.NewConsumer(builder);

            var pBuilder = new ProducerConfigBuilder <byte[]>();

            pBuilder.Topic(topic);
            var producer = _client.NewProducer(pBuilder);

            const int messageCount = 10;

            for (var i = 0; i < messageCount; i++)
            {
                var receipt = producer.Send(Encoding.UTF8.GetBytes("my-message-" + i));
                _output.WriteLine(JsonSerializer.Serialize(receipt, new JsonSerializerOptions {
                    WriteIndented = true
                }));
            }

            var messageReceived = 0;

            Thread.Sleep(TimeSpan.FromSeconds(5));
            for (var i = 0; i < messageCount; ++i)
            {
                var m = consumer.Receive();
                var receivedMessage = Encoding.UTF8.GetString(m.Data);
                _output.WriteLine($"Received message: [{receivedMessage}]");
                Assert.NotNull(receivedMessage);
                messageReceived++;
            }

            Assert.Equal(10, messageReceived);
            Thread.Sleep(TimeSpan.FromSeconds(10));
            for (var i = 0; i < messageCount; i++)
            {
                var m = consumer.Receive();
                var receivedMessage = Encoding.UTF8.GetString(m.Data);
                _output.WriteLine($"Received message: [{receivedMessage}]");
                Assert.NotNull(receivedMessage);
                messageReceived++;
            }
            Assert.Equal(20, messageReceived);
            producer.Close();
            consumer.Close();
        }
Ejemplo n.º 2
0
        private Consumer <byte[]> CreateConsumer(string topic, string consumerSub, KeySharedPolicy keySharedPolicy = null)
        {
            var builder = new ConsumerConfigBuilder <byte[]>();

            builder.Topic(topic);
            builder.SubscriptionName(consumerSub);
            builder.AckTimeout(TimeSpan.FromSeconds(10));
            builder.ForceTopicCreation(true);
            if (keySharedPolicy != null)
            {
                builder.KeySharedPolicy(keySharedPolicy);
            }
            builder.SubscriptionType(CommandSubscribe.SubType.KeyShared);
            return(_client.NewConsumer(builder));
        }