/// <summary>
        ///     Adds an inbound endpoint and instantiates a consumer.
        /// </summary>
        /// <param name="endpointsConfigurationBuilder">
        ///     The <see cref="IEndpointsConfigurationBuilder" />.
        /// </param>
        /// <param name="endpoint">
        ///     The endpoint (topic).
        /// </param>
        /// <param name="consumersCount">
        ///     The number of consumers to be instantiated. The default is 1.
        /// </param>
        /// <returns>
        ///     The <see cref="IEndpointsConfigurationBuilder" /> so that additional calls can be chained.
        /// </returns>
        public static IEndpointsConfigurationBuilder AddInbound(
            this IEndpointsConfigurationBuilder endpointsConfigurationBuilder,
            IConsumerEndpoint endpoint,
            int consumersCount = 1)
        {
            Check.NotNull(endpointsConfigurationBuilder, nameof(endpointsConfigurationBuilder));
            Check.NotNull(endpoint, nameof(endpoint));

            if (consumersCount <= 0)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(consumersCount),
                          consumersCount,
                          "The consumers count must be greater or equal to 1.");
            }

            var serviceProvider  = endpointsConfigurationBuilder.ServiceProvider;
            var brokerCollection = serviceProvider.GetRequiredService <IBrokerCollection>();
            var logger           =
                serviceProvider.GetRequiredService <ISilverbackLogger <IEndpointsConfigurationBuilder> >();

            if (!endpoint.IsValid(logger))
            {
                return(endpointsConfigurationBuilder);
            }

            for (int i = 0; i < consumersCount; i++)
            {
                brokerCollection.AddConsumer(endpoint);
            }

            return(endpointsConfigurationBuilder);
        }
        /// <summary>
        ///     Adds and inbound endpoint.
        /// </summary>
        /// <param name="endpointsConfigurationBuilder">
        ///     The <see cref="IEndpointsConfigurationBuilder" />.
        /// </param>
        /// <param name="endpoint">
        ///     The endpoint (topic).
        /// </param>
        /// <param name="consumersCount">
        ///     The number of consumers to be instantiated. The default is 1.
        /// </param>
        /// <returns>
        ///     The <see cref="IEndpointsConfigurationBuilder" /> so that additional calls can be chained.
        /// </returns>
        public static IEndpointsConfigurationBuilder AddInbound(
            this IEndpointsConfigurationBuilder endpointsConfigurationBuilder,
            IConsumerEndpoint endpoint,
            int consumersCount = 1)
        {
            Check.NotNull(endpointsConfigurationBuilder, nameof(endpointsConfigurationBuilder));
            Check.NotNull(endpoint, nameof(endpoint));

            if (consumersCount <= 0)
            {
                throw new ArgumentException(
                          "The consumers count must be greater or equal to 1.",
                          nameof(consumersCount));
            }

            var serviceProvider  = endpointsConfigurationBuilder.ServiceProvider;
            var brokerCollection = serviceProvider.GetRequiredService <IBrokerCollection>();

            for (int i = 0; i < consumersCount; i++)
            {
                brokerCollection.AddConsumer(endpoint);
            }

            return(endpointsConfigurationBuilder);
        }
        /// <inheritdoc cref="IOffsetStore.StoreAsync" />
        public Task StoreAsync(IComparableOffset offset, IConsumerEndpoint endpoint)
        {
            Check.NotNull(offset, nameof(offset));
            Check.NotNull(endpoint, nameof(endpoint));

            return(AddOrReplaceAsync(GetKey(offset.Key, endpoint), offset));
        }
Exemple #4
0
        /// <inheritdoc cref="IBroker.AddConsumer" />
        public virtual IConsumer AddConsumer(IConsumerEndpoint endpoint)
        {
            Check.NotNull(endpoint, nameof(endpoint));

            if (_consumers == null)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (IsConnected)
            {
                throw new InvalidOperationException(CannotCreateConsumerIfConnectedExceptionMessage);
            }

            _logger.LogInformation(
                IntegrationEventIds.CreatingNewConsumer,
                "Creating new consumer for endpoint {endpointName}.",
                endpoint.Name);

            var consumer = InstantiateConsumer(
                (TConsumerEndpoint)endpoint,
                _serviceProvider.GetRequiredService <IBrokerBehaviorsProvider <IConsumerBehavior> >(),
                _serviceProvider);

            _consumers.Add(consumer);

            return(consumer);
        }
        /// <inheritdoc cref="IOffsetStore.GetLatestValueAsync" />
        public async Task <IComparableOffset?> GetLatestValueAsync(string offsetKey, IConsumerEndpoint endpoint)
        {
            Check.NotNull(offsetKey, nameof(offsetKey));
            Check.NotNull(endpoint, nameof(endpoint));

            var storedOffsetEntity = await DbSet.FindAsync(GetKey(offsetKey, endpoint)).ConfigureAwait(false)
                                     ?? await DbSet.FindAsync(offsetKey).ConfigureAwait(false);

            return(DeserializeOffset(storedOffsetEntity));
        }
Exemple #6
0
 /// <summary>
 ///     Creates an <see cref="IRawInboundEnvelope" />.
 /// </summary>
 /// <param name="rawMessageStream">
 ///     The raw message body.
 /// </param>
 /// <param name="headers">
 ///     The message headers.
 /// </param>
 /// <param name="endpoint">
 ///     The source endpoint.
 /// </param>
 /// <param name="identifier">
 ///     The <see cref="IBrokerMessageIdentifier" />.
 /// </param>
 /// <returns>
 ///     An <see cref="IInboundEnvelope{TMessage}" /> containing the specified message.
 /// </returns>
 public static IRawInboundEnvelope Create(
     Stream rawMessageStream,
     MessageHeaderCollection?headers,
     IConsumerEndpoint endpoint,
     IBrokerMessageIdentifier identifier) =>
 new RawInboundEnvelope(
     rawMessageStream,
     headers?.AsReadOnlyCollection(),
     endpoint,
     Check.NotNull(endpoint, nameof(endpoint)).Name,
     identifier);
Exemple #7
0
 public RawInboundEnvelope(
     Stream?rawMessage,
     IReadOnlyCollection <MessageHeader>?headers,
     IConsumerEndpoint endpoint,
     string actualEndpointName,
     IBrokerMessageIdentifier brokerMessageIdentifier)
     : base(rawMessage, headers, endpoint)
 {
     ActualEndpointName      = actualEndpointName;
     BrokerMessageIdentifier = brokerMessageIdentifier;
 }
 public RawInboundEnvelope(
     Stream?rawMessage,
     IEnumerable <MessageHeader>?headers,
     IConsumerEndpoint endpoint,
     string actualEndpointName,
     IBrokerMessageIdentifier brokerMessageIdentifier,
     IDictionary <string, string>?additionalLogData = null)
     : base(rawMessage, headers, endpoint, additionalLogData)
 {
     ActualEndpointName      = actualEndpointName;
     BrokerMessageIdentifier = brokerMessageIdentifier;
 }
Exemple #9
0
 public RawInboundEnvelope(
     Stream?rawMessage,
     IEnumerable <MessageHeader>?headers,
     IConsumerEndpoint endpoint,
     string actualEndpointName,
     IOffset offset,
     IDictionary <string, string>?additionalLogData = null)
     : base(rawMessage, headers, endpoint, additionalLogData)
 {
     ActualEndpointName = actualEndpointName;
     Offset             = offset;
 }
        /// <inheritdoc cref="IBrokerCollection.AddConsumer" />
        public IConsumer AddConsumer(IConsumerEndpoint endpoint)
        {
            Check.NotNull(endpoint, nameof(endpoint));

            var endpointType = endpoint.GetType();

            return(FindBroker(
                       broker => broker.ConsumerEndpointType,
                       endpointType,
                       _consumerEndpointTypeMap)
                   .AddConsumer(endpoint));
        }
Exemple #11
0
 public RawInboundEnvelope(
     byte[]?rawMessage,
     IReadOnlyCollection <MessageHeader>?headers,
     IConsumerEndpoint endpoint,
     string actualEndpointName,
     IBrokerMessageIdentifier brokerMessageIdentifier)
     : this(
         rawMessage != null ? new MemoryStream(rawMessage) : null,
         headers,
         endpoint,
         actualEndpointName,
         brokerMessageIdentifier)
 {
 }
 public RawInboundEnvelope(
     byte[]?rawMessage,
     IEnumerable <MessageHeader>?headers,
     IConsumerEndpoint endpoint,
     string actualEndpointName,
     IBrokerMessageIdentifier brokerMessageIdentifier,
     IDictionary <string, string>?additionalLogData = null)
     : this(
         rawMessage != null ? new MemoryStream(rawMessage) : null,
         headers,
         endpoint,
         actualEndpointName,
         brokerMessageIdentifier,
         additionalLogData)
 {
 }
Exemple #13
0
 public InboundEnvelope(
     object message,
     Stream?rawMessage,
     IReadOnlyCollection <MessageHeader>?headers,
     IBrokerMessageIdentifier brokerMessageIdentifier,
     IConsumerEndpoint endpoint,
     string actualEndpointName)
     : base(
         rawMessage,
         headers,
         endpoint,
         actualEndpointName,
         brokerMessageIdentifier)
 {
     Message = message;
 }
Exemple #14
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Consumer" /> class.
        /// </summary>
        /// <param name="broker">
        ///     The <see cref="IBroker" /> that is instantiating the consumer.
        /// </param>
        /// <param name="endpoint">
        ///     The endpoint to be consumed.
        /// </param>
        /// <param name="behaviorsProvider">
        ///     The <see cref="IBrokerBehaviorsProvider{TBehavior}" />.
        /// </param>
        /// <param name="serviceProvider">
        ///     The <see cref="IServiceProvider" /> to be used to resolve the needed services.
        /// </param>
        /// <param name="logger">
        ///     The <see cref="ISilverbackIntegrationLogger" />.
        /// </param>
        protected Consumer(
            IBroker broker,
            IConsumerEndpoint endpoint,
            IBrokerBehaviorsProvider <IConsumerBehavior> behaviorsProvider,
            IServiceProvider serviceProvider,
            ISilverbackIntegrationLogger <Consumer> logger)
        {
            Broker   = Check.NotNull(broker, nameof(broker));
            Endpoint = Check.NotNull(endpoint, nameof(endpoint));

            _behaviors      = Check.NotNull(behaviorsProvider, nameof(behaviorsProvider)).GetBehaviorsList();
            ServiceProvider = Check.NotNull(serviceProvider, nameof(serviceProvider));
            _logger         = Check.NotNull(logger, nameof(logger));

            Endpoint.Validate();
        }
Exemple #15
0
        public RawInboundEnvelope(
            Stream?rawMessage,
            IReadOnlyCollection <MessageHeader>?headers,
            IConsumerEndpoint endpoint,
            string actualEndpointName,
            IBrokerMessageIdentifier brokerMessageIdentifier)
            : base(rawMessage, headers, endpoint)
        {
            Check.NotNull(endpoint, nameof(endpoint));

            ActualEndpointName      = actualEndpointName;
            BrokerMessageIdentifier = brokerMessageIdentifier;

            if (endpoint.FriendlyName != null)
            {
                _actualEndpointDisplayName = $"{endpoint.FriendlyName} ({actualEndpointName})";
            }
        }
Exemple #16
0
 public InboundEnvelope(
     object message,
     Stream?rawMessage,
     IEnumerable <MessageHeader>?headers,
     IBrokerMessageIdentifier brokerMessageIdentifier,
     IConsumerEndpoint endpoint,
     string actualEndpointName,
     IDictionary <string, string>?additionalLogData = null)
     : base(
         rawMessage,
         headers,
         endpoint,
         actualEndpointName,
         brokerMessageIdentifier,
         additionalLogData)
 {
     Message = message;
 }
Exemple #17
0
        /// <inheritdoc cref="IOffsetStore.StoreAsync" />
        public async Task StoreAsync(IBrokerMessageOffset offset, IConsumerEndpoint endpoint)
        {
            Check.NotNull(offset, nameof(offset));
            Check.NotNull(endpoint, nameof(endpoint));

            var storedOffsetEntity = await DbSet.FindAsync(GetKey(offset.Key, endpoint)).ConfigureAwait(false) ??
                                     DbSet.Add(
                new StoredOffset
            {
                Key     = GetKey(offset.Key, endpoint),
                ClrType = offset.GetType().AssemblyQualifiedName
            });

            storedOffsetEntity.Value = offset.Value;

#pragma warning disable CS0618 // Obsolete
            storedOffsetEntity.Offset = null;
#pragma warning restore CS0618 // Obsolete
        }
Exemple #18
0
 private static string GetKey(string offsetKey, IConsumerEndpoint endpoint) =>
 $"{endpoint.GetUniqueConsumerGroupName()}|{offsetKey}";
 /// <inheritdoc cref="IOffsetStore.GetLatestValueAsync" />
 public Task <IComparableOffset?> GetLatestValueAsync(string offsetKey, IConsumerEndpoint endpoint) =>
 Task.FromResult(
     (IComparableOffset?)Items.Union(UncommittedItems)
     .Where(pair => pair.Key == GetKey(offsetKey, endpoint))
     .Select(pair => pair.Value)
     .Max());
Exemple #20
0
 public static void LogCreatingNewConsumer(
     this ISilverbackLogger logger,
     IConsumerEndpoint endpoint) =>
 CreatingNewConsumer(logger.InnerLogger, endpoint.DisplayName, null);
Exemple #21
0
 public ConsumerEndpointTest()
 {
     _endpoint = new ConsumerEndpoint <MockEntity>(EntryEndpoint, "endpoint");
 }
 /// <summary>
 /// Creates a new REST action command.
 /// </summary>
 /// <param name="endpoint">The endpoint this command operates on.</param>
 public ConsumerCommand(IConsumerEndpoint<TEntity> endpoint)
     : base(endpoint)
 {}
Exemple #23
0
 private static bool IsToBeTested(
     IConsumerEndpoint consumerEndpoint,
     Func <IConsumerEndpoint, bool>?endpointNames) =>
 endpointNames == null || endpointNames.Invoke(consumerEndpoint);