Beispiel #1
0
        public RMQMessageConsumerRetryDLQTests()
        {
            Guid   correlationId = Guid.NewGuid();
            string contentType   = "text\\plain";
            var    channelName   = $"Requeue-Limit-Tests-{Guid.NewGuid().ToString()}";

            _topicName = $"Requeue-Limit-Tests-{Guid.NewGuid().ToString()}";
            var routingKey = new RoutingKey(_topicName);

            //what do we send
            var myCommand = new MyDeferredCommand {
                Value = "Hello Requeue"
            };

            _message = new Message(
                new MessageHeader(myCommand.Id, _topicName, MessageType.MT_COMMAND, correlationId, "", contentType),
                new MessageBody(JsonSerializer.Serialize((object)myCommand, JsonSerialisationOptions.Options))
                );

            var deadLetterQueueName  = $"{_message.Header.Topic}.DLQ";
            var deadLetterRoutingKey = $"{_message.Header.Topic}.DLQ";

            var subscription = new RmqSubscription <MyCommand>(
                name: new SubscriptionName(channelName),
                channelName: new ChannelName(channelName),
                routingKey: routingKey,
                //after 2 retries, fail and move to the DLQ
                requeueCount: 2,
                //delay before re-queuing
                requeueDelayInMilliseconds: 50,
                deadLetterChannelName: new ChannelName(deadLetterQueueName),
                deadLetterRoutingKey: deadLetterRoutingKey,
                makeChannels: OnMissingChannel.Create
                );

            var rmqConnection = new RmqMessagingGatewayConnection
            {
                AmpqUri            = new AmqpUriSpecification(new Uri("amqp://*****:*****@localhost:5672/%2f")),
                Exchange           = new Exchange("paramore.brighter.exchange"),
                DeadLetterExchange = new Exchange("paramore.brighter.exchange.dlq")
            };

            //how do we send to the queue
            _sender = new RmqMessageProducer(rmqConnection, new RmqPublication());

            //set up our receiver
            _channelFactory = new ChannelFactory(new RmqMessageConsumerFactory(rmqConnection));
            _channel        = _channelFactory.CreateChannel(subscription);

            //how do we handle a command
            IHandleRequests <MyDeferredCommand> handler = new MyDeferredCommandHandler();

            //hook up routing for the command processor
            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <MyDeferredCommand, MyDeferredCommandHandler>();

            //once we read, how do we dispatch to a handler. N.B. we don't use this for reading here
            _commandProcessor = new CommandProcessor(
                subscriberRegistry: subscriberRegistry,
                handlerFactory: new QuickHandlerFactory(() => handler),
                requestContextFactory: new InMemoryRequestContextFactory(),
                policyRegistry: new PolicyRegistry()
                );

            //pump messages from a channel to a handler - in essence we are building our own dispatcher in this test
            IAmAMessageMapper <MyDeferredCommand> mapper = new MyDeferredCommandMessageMapper(_topicName);

            _messagePump = new MessagePumpBlocking <MyDeferredCommand>(_commandProcessor, mapper)
            {
                Channel = _channel, TimeoutInMilliseconds = 5000, RequeueCount = 3
            };

            _deadLetterConsumer = new RmqMessageConsumer(
                connection: rmqConnection,
                queueName: deadLetterQueueName,
                routingKey: deadLetterRoutingKey,
                isDurable: false,
                makeChannels: OnMissingChannel.Assume
                );
        }
        public SnsReDrivePolicySDlqTests()
        {
            Guid   correlationId = Guid.NewGuid();
            string replyTo       = "http:\\queueUrl";
            string contentType   = "text\\plain";
            var    channelName   = $"Redrive-Tests-{Guid.NewGuid().ToString()}".Truncate(45);

            _dlqChannelName = $"Redrive-DLQ-Tests-{Guid.NewGuid().ToString()}".Truncate(45);
            _topicName      = $"Redrive-Tests-{Guid.NewGuid().ToString()}".Truncate(45);
            var routingKey = new RoutingKey(_topicName);

            //how are we consuming
            var subscription = new SqsSubscription <MyCommand>(
                name: new SubscriptionName(channelName),
                channelName: new ChannelName(channelName),
                routingKey: routingKey,
                //don't block the redrive policy from owning retry management
                requeueCount: -1,
                //delay before requeuing
                requeueDelayInMs: 50,
                //we want our SNS subscription to manage requeue limits using the DLQ for 'too many requeues'
                redrivePolicy: new RedrivePolicy
                (
                    deadLetterQueueName: new ChannelName(_dlqChannelName),
                    maxReceiveCount: 2
                ));

            //what do we send
            var myCommand = new MyDeferredCommand {
                Value = "Hello Redrive"
            };

            _message = new Message(
                new MessageHeader(myCommand.Id, _topicName, MessageType.MT_COMMAND, correlationId, replyTo, contentType),
                new MessageBody(JsonSerializer.Serialize((object)myCommand, JsonSerialisationOptions.Options))
                );

            //Must have credentials stored in the SDK Credentials store or shared credentials file
            (AWSCredentials credentials, RegionEndpoint region) = CredentialsChain.GetAwsCredentials();
            _awsConnection = new AWSMessagingGatewayConnection(credentials, region);

            //how do we send to the queue
            _sender = new SqsMessageProducer(_awsConnection, new SnsPublication {
                MakeChannels = OnMissingChannel.Create
            });

            //We need to do this manually in a test - will create the channel from subscriber parameters
            _channelFactory = new ChannelFactory(_awsConnection);
            _channel        = _channelFactory.CreateChannel(subscription);

            //how do we handle a command
            IHandleRequests <MyDeferredCommand> handler = new MyDeferredCommandHandler();

            //hook up routing for the command processor
            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <MyDeferredCommand, MyDeferredCommandHandler>();

            //once we read, how do we dispatch to a handler. N.B. we don't use this for reading here
            _commandProcessor = new CommandProcessor(
                subscriberRegistry: subscriberRegistry,
                handlerFactory: new QuickHandlerFactory(() => handler),
                requestContextFactory: new InMemoryRequestContextFactory(),
                policyRegistry: new PolicyRegistry()
                );

            //pump messages from a channel to a handler - in essence we are building our own dispatcher in this test
            IAmAMessageMapper <MyDeferredCommand> mapper = new MyDeferredCommandMessageMapper(_topicName);

            _messagePump = new MessagePumpBlocking <MyDeferredCommand>(_commandProcessor, mapper)
            {
                Channel = _channel, TimeoutInMilliseconds = 5000, RequeueCount = 3
            };
        }