/// <inheritdoc />
        void ISubscriptionBuilder <T> .Configure(
            JustSayingBus bus,
            IHandlerResolver handlerResolver,
            IServiceResolver serviceResolver,
            IVerifyAmazonQueues creator,
            IAwsClientFactoryProxy awsClientFactoryProxy,
            ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger <QueueSubscriptionBuilder <T> >();

            var attachedQueueConfig = new QueueAddressConfiguration();

            ConfigureReads?.Invoke(attachedQueueConfig);

            IAmazonSQS sqsClient = awsClientFactoryProxy
                                   .GetAwsClientFactory()
                                   .GetSqsClient(RegionEndpoint.GetBySystemName(_queueAddress.RegionName));

            var queue = new QueueAddressQueue(_queueAddress, sqsClient);

            attachedQueueConfig.SubscriptionGroupName ??= queue.QueueName;
            attachedQueueConfig.Validate();

            bus.AddQueue(attachedQueueConfig.SubscriptionGroupName, queue);

            logger.LogInformation(
                "Created SQS queue subscription for '{MessageType}' on '{QueueName}'",
                typeof(T), queue.QueueName);

            var resolutionContext = new HandlerResolutionContext(queue.QueueName);
            var proposedHandler   = handlerResolver.ResolveHandler <T>(resolutionContext);

            if (proposedHandler == null)
            {
                throw new HandlerNotRegisteredWithContainerException(
                          $"There is no handler for '{typeof(T)}' messages.");
            }

            var middlewareBuilder = new HandlerMiddlewareBuilder(handlerResolver, serviceResolver);
            var handlerMiddleware = middlewareBuilder
                                    .Configure(MiddlewareConfiguration ?? (b => b.UseDefaults <T>(proposedHandler.GetType())))
                                    .Build();

            bus.AddMessageMiddleware <T>(queue.QueueName, handlerMiddleware);

            logger.LogInformation(
                "Added a message handler for message type for '{MessageType}' on queue '{QueueName}'",
                typeof(T), queue.QueueName);
        }
Exemplo n.º 2
0
    protected override void Given()
    {
        _queue = CreateSuccessfulTestQueue(Guid.NewGuid().ToString(), new TestMessage());
        Queues.Add(_queue);
        _messageLock = new FakeMessageLock();

        var serviceResolver = new InMemoryServiceResolver(sc =>
                                                          sc.AddSingleton <IMessageLockAsync>(_messageLock)
                                                          .AddSingleton <IHandlerAsync <SimpleMessage> >(Handler)
                                                          .AddLogging(x => x.AddXUnit(OutputHelper)));

        var middlewareBuilder = new HandlerMiddlewareBuilder(serviceResolver, serviceResolver);

        var middleware = middlewareBuilder.Configure(pipe =>
        {
            pipe.UseExactlyOnce <SimpleMessage>("a-unique-lock-key");
            pipe.UseHandler <SimpleMessage>();
        }).Build();

        Middleware = middleware;
    }