Пример #1
0
        void CreateModel(IAsyncPipeContextAgent <ClientContext> asyncContext, CancellationToken cancellationToken)
        {
            IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext =>
            {
                try
                {
                    var clientContext = connectionContext.CreateClientContext(cancellationToken);

                    await asyncContext.Created(clientContext).ConfigureAwait(false);

                    await asyncContext.Completed.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    await asyncContext.CreateCanceled().ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    await asyncContext.CreateFaulted(exception).ConfigureAwait(false);
                }
            });

            var connectionTask = _connectionContextSupervisor.Send(connectionPipe, cancellationToken);

            Task NotifyCreateCanceled(Task task) => asyncContext.CreateCanceled();

            connectionTask.ContinueWith(NotifyCreateCanceled, TaskContinuationOptions.OnlyOnCanceled);

            Task NotifyCreateFaulted(Task task) => asyncContext.CreateFaulted(task.Exception);

            connectionTask.ContinueWith(NotifyCreateFaulted, TaskContinuationOptions.OnlyOnFaulted);
        }
Пример #2
0
        void CreateSession(IAsyncPipeContextAgent <SessionContext> asyncContext, CancellationToken cancellationToken)
        {
            async Task <SessionContext> CreateSessionContext(ConnectionContext connectionContext, CancellationToken createCancellationToken)
            {
                var session = await connectionContext.CreateSession(createCancellationToken).ConfigureAwait(false);

                void HandleConnectionException(Exception exception)
                {
                    // ReSharper disable once MethodSupportsCancellation
                    asyncContext.Stop($"Connection Exception: {exception}");
                }

                connectionContext.Connection.ExceptionListener += HandleConnectionException;

            #pragma warning disable 4014
                // ReSharper disable once MethodSupportsCancellation
                asyncContext.Completed.ContinueWith(_ => connectionContext.Connection.ExceptionListener -= HandleConnectionException,
                                                    TaskContinuationOptions.ExecuteSynchronously);
            #pragma warning restore 4014

                return(new ActiveMqSessionContext(connectionContext, session, createCancellationToken));
            }

            _connectionContextSupervisor.CreateAgent(asyncContext, CreateSessionContext, cancellationToken);
        }
Пример #3
0
        IPipeContextAgent <TContext> IPipeContextFactory <TContext> .CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <TContext> asyncContext = supervisor.AddAsyncContext <TContext>();

            Task <TContext> context = CreateJoinContext(asyncContext, supervisor.Stopped);

            context.ContinueWith(task =>
            {
                try
                {
                    if (task.IsCanceled)
                    {
                        asyncContext.CreateCanceled();
                    }
                    else if (task.IsFaulted)
                    {
                        asyncContext.CreateFaulted(task.Exception);
                    }
                }
                catch
                {
                }
            });

            return(asyncContext);
        }
Пример #4
0
        async Task <ConnectionContext> CreateConnection(IAsyncPipeContextAgent <ConnectionContext> asyncContext, IAgent supervisor)
        {
            IConnection connection = null;

            try
            {
                if (supervisor.Stopping.IsCancellationRequested)
                {
                    throw new OperationCanceledException($"The connection is stopping and cannot be used: {_description}");
                }

                connection = _settings.CreateConnection();

                var connectionContext = new AmazonSqsConnectionContext(connection, _settings, _topology, _description, supervisor.Stopped);
                connectionContext.GetOrAddPayload(() => _settings);

                await asyncContext.Created(connectionContext).ConfigureAwait(false);

                return(connectionContext);
            }
            catch (OperationCanceledException)
            {
                await asyncContext.CreateCanceled().ConfigureAwait(false);

                throw;
            }
        }
Пример #5
0
 public CreateAgentPipe(IAsyncPipeContextAgent <TAgent> asyncContext, Func <T, CancellationToken, Task <TAgent> > agentFactory,
                        CancellationToken cancellationToken)
 {
     _asyncContext      = asyncContext;
     _agentFactory      = agentFactory;
     _cancellationToken = cancellationToken;
 }
Пример #6
0
        async Task <ModelContext> CreateModel(IAsyncPipeContextAgent <ModelContext> asyncContext, CancellationToken cancellationToken)
        {
            IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext =>
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Creating model: {0}", connectionContext.Description);
                }

                try
                {
                    var model = await connectionContext.CreateModel().ConfigureAwait(false);

                    var modelContext = new RabbitMqModelContext(connectionContext, model, _host, cancellationToken);

                    await asyncContext.Created(modelContext).ConfigureAwait(false);

                    await asyncContext.Completed.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    await asyncContext.CreateCanceled().ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    await asyncContext.CreateFaulted(exception).ConfigureAwait(false);
                }
            });

            _connectionCache.Send(connectionPipe, cancellationToken).ConfigureAwait(false);

            return(await asyncContext.Context.ConfigureAwait(false));
        }
        async Task <ConnectionContext> CreateConnection(IAsyncPipeContextAgent <ConnectionContext> asyncContext, IAgent supervisor)
        {
            IConnection connection = null;

            try
            {
                if (supervisor.Stopping.IsCancellationRequested)
                {
                    throw new OperationCanceledException($"The connection is stopping and cannot be used: {_configuration.HostAddress}");
                }

                connection = _configuration.Settings.CreateConnection();

                var connectionContext = new AmazonSqsConnectionContext(connection, _configuration, _hostTopology, supervisor.Stopped);

                await asyncContext.Created(connectionContext).ConfigureAwait(false);

                return(connectionContext);
            }
            catch (OperationCanceledException)
            {
                await asyncContext.CreateCanceled().ConfigureAwait(false);

                throw;
            }
        }
        void CreateSession(IAsyncPipeContextAgent <SessionContext> asyncContext, CancellationToken cancellationToken)
        {
            IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext =>
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Creating session: {0}", connectionContext.Description);
                }

                try
                {
                    var session = await connectionContext.CreateSession().ConfigureAwait(false);

                    var sessionContext = new ActiveMqSessionContext(connectionContext, session, _host, cancellationToken);

                    await asyncContext.Created(sessionContext).ConfigureAwait(false);

                    await asyncContext.Completed.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    await asyncContext.CreateCanceled().ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    await asyncContext.CreateFaulted(exception).ConfigureAwait(false);
                }
            });

            _connectionCache.Send(connectionPipe, cancellationToken).ConfigureAwait(false);
        }
Пример #9
0
        IPipeContextAgent <TContext> IPipeContextFactory <TContext> .CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <TContext> asyncContext = supervisor.AddAsyncContext <TContext>();

            Task <TContext> context = CreateJoinContext(asyncContext, supervisor.Stopped);

            return(asyncContext);
        }
Пример #10
0
        public IPipeContextAgent<ClientContext> CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent<ClientContext> asyncContext = supervisor.AddAsyncContext<ClientContext>();

            CreateClientContext(asyncContext, supervisor.Stopped);

            return asyncContext;
        }
Пример #11
0
        IPipeContextAgent <ConnectionContext> IPipeContextFactory <ConnectionContext> .CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <ConnectionContext> asyncContext = supervisor.AddAsyncContext <ConnectionContext>();

            Task.Run(() => CreateConnection(asyncContext, supervisor), supervisor.Stopping);

            return(asyncContext);
        }
Пример #12
0
        IPipeContextAgent <ProcessorContext> IPipeContextFactory <ProcessorContext> .CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <ProcessorContext> asyncContext = supervisor.AddAsyncContext <ProcessorContext>();

            CreateProcessor(asyncContext, supervisor.Stopped);

            return(asyncContext);
        }
Пример #13
0
        IPipeContextAgent <ClientContext> IPipeContextFactory <ClientContext> .CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <ClientContext> asyncContext = supervisor.AddAsyncContext <ClientContext>();

            CreateModel(asyncContext, supervisor.Stopped);

            return(asyncContext);
        }
Пример #14
0
        public IPipeContextAgent <ProducerContext <TKey, TValue> > CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <ProducerContext <TKey, TValue> > asyncContext = supervisor.AddAsyncContext <ProducerContext <TKey, TValue> >();

            CreateProducer(asyncContext, supervisor.Stopped);

            return(asyncContext);
        }
Пример #15
0
        public IPipeContextAgent <SendEndpointContext> CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <SendEndpointContext> asyncContext = supervisor.AddAsyncContext <SendEndpointContext>();

            CreateSendEndpointContext(asyncContext, supervisor.Stopped);

            return(asyncContext);
        }
Пример #16
0
        Task <ModelContext> CreateModel(IAsyncPipeContextAgent <ModelContext> asyncContext, CancellationToken cancellationToken)
        {
            async Task <ModelContext> CreateModelContext(ModelContext context, CancellationToken createCancellationToken)
            {
                return(new SharedModelContext(context, cancellationToken));
            }

            return(_supervisor.CreateAgent(asyncContext, CreateModelContext, cancellationToken));
        }
        IPipeContextAgent <ConnectionContext> IPipeContextFactory <ConnectionContext> .CreateContext(ISupervisor supervisor)
        {
            IAsyncPipeContextAgent <ConnectionContext> asyncContext = supervisor.AddAsyncContext <ConnectionContext>();

            var context = Task.Factory.StartNew(() => CreateConnection(asyncContext, supervisor), supervisor.Stopping, TaskCreationOptions.None,
                                                TaskScheduler.Default)
                          .Unwrap();

            return(asyncContext);
        }
Пример #18
0
        async Task <ConnectionContext> CreateConnection(IAsyncPipeContextAgent <ConnectionContext> asyncContext, IAgent supervisor)
        {
            return(await _connectionRetryPolicy.Retry(async() =>
            {
                if (supervisor.Stopping.IsCancellationRequested)
                {
                    throw new OperationCanceledException($"The connection is stopping and cannot be used: {_configuration.Description}");
                }

                IConnection connection = null;
                try
                {
                    TransportLogMessages.ConnectHost(_configuration.Description);

                    connection = _configuration.Settings.CreateConnection();

                    void HandleConnectionException(Exception exception)
                    {
                        asyncContext.Stop($"Connection Exception: {exception}");
                    }

                    connection.ExceptionListener += HandleConnectionException;

                #pragma warning disable 4014
                    asyncContext.Completed.ContinueWith(_ => connection.ExceptionListener -= HandleConnectionException);
                #pragma warning restore 4014

                    connection.Start();

                    LogContext.Debug?.Log("Connected: {Host} (client-id: {ClientId}, version: {Version})", _configuration.Description, connection.ClientId,
                                          connection.MetaData.NMSVersion);

                    var connectionContext = new ActiveMqConnectionContext(connection, _configuration, _hostTopology, supervisor.Stopped);

                    await asyncContext.Created(connectionContext).ConfigureAwait(false);

                    return connectionContext;
                }
                catch (OperationCanceledException)
                {
                    await asyncContext.CreateCanceled().ConfigureAwait(false);
                    throw;
                }
                catch (NMSConnectionException ex)
                {
                    LogContext.Error?.Log(ex, "ActiveMQ connection failed");

                    await asyncContext.CreateFaulted(ex).ConfigureAwait(false);

                    connection?.Dispose();

                    throw new ActiveMqConnectException("Connect failed: " + _configuration.Description, ex);
                }
            }));
        }
Пример #19
0
        void CreateSession(IAsyncPipeContextAgent <SessionContext> asyncContext, CancellationToken cancellationToken)
        {
            IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext =>
            {
                try
                {
                    var session = await connectionContext.CreateSession(cancellationToken).ConfigureAwait(false);

                    LogContext.Debug?.Log("Created session: {Host}", connectionContext.Description);

                    var sessionContext = new ActiveMqSessionContext(connectionContext, session, cancellationToken);

                    void HandleException(Exception exception)
                    {
                    #pragma warning disable 4014
                        sessionContext.DisposeAsync(CancellationToken.None);
                    #pragma warning restore 4014
                    }

                    connectionContext.Connection.ExceptionListener += HandleException;

                #pragma warning disable 4014
                    // ReSharper disable once MethodSupportsCancellation
                    asyncContext.Completed.ContinueWith(task =>
                #pragma warning restore 4014
                    {
                        connectionContext.Connection.ExceptionListener -= HandleException;
                    });

                    await asyncContext.Created(sessionContext).ConfigureAwait(false);

                    await asyncContext.Completed.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    await asyncContext.CreateCanceled().ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    LogContext.Error?.Log(exception, "Create session failed: {Host}", connectionContext.Description);

                    await asyncContext.CreateFaulted(exception).ConfigureAwait(false);
                }
            });

            var connectionTask = _connectionContextSupervisor.Send(connectionPipe, cancellationToken);

            Task NotifyCreateCanceled(Task task) => asyncContext.CreateCanceled();

            connectionTask.ContinueWith(NotifyCreateCanceled, TaskContinuationOptions.OnlyOnCanceled);

            Task NotifyCreateFaulted(Task task) => asyncContext.CreateFaulted(task.Exception);

            connectionTask.ContinueWith(NotifyCreateFaulted, TaskContinuationOptions.OnlyOnFaulted);
        }
Пример #20
0
        void CreateClientContext(IAsyncPipeContextAgent<ClientContext> asyncContext, CancellationToken cancellationToken)
        {
            Task<ClientContext> Create(ConnectionContext connectionContext, CancellationToken createCancellationToken)
            {
                var inputAddress = _settings.GetInputAddress(connectionContext.Endpoint, _settings.Path);

                return Task.FromResult(CreateClientContext(connectionContext, inputAddress, asyncContext));
            }

            _supervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
Пример #21
0
        void CreateSession(IAsyncPipeContextAgent <SessionContext> asyncContext, CancellationToken cancellationToken)
        {
            IPipe <ConnectionContext> connectionPipe = Pipe.ExecuteAsync <ConnectionContext>(async connectionContext =>
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Creating session: {0}", connectionContext.Description);
                }

                try
                {
                    var session = await connectionContext.CreateSession().ConfigureAwait(false);

                    var sessionContext = new ActiveMqSessionContext(connectionContext, session, cancellationToken);

                    void HandleException(Exception exception)
                    {
                        var disposeAsync = sessionContext.DisposeAsync(CancellationToken.None);
                    }

                    connectionContext.Connection.ExceptionListener += HandleException;

                #pragma warning disable 4014
                    asyncContext.Completed.ContinueWith(task =>
                #pragma warning restore 4014
                    {
                        connectionContext.Connection.ExceptionListener -= HandleException;
                    });

                    await asyncContext.Created(sessionContext).ConfigureAwait(false);

                    await asyncContext.Completed.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    await asyncContext.CreateCanceled().ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    await asyncContext.CreateFaulted(exception).ConfigureAwait(false);
                }
            });

            var connectionTask = _connectionContextSupervisor.Send(connectionPipe, cancellationToken);

            Task NotifyCreateCanceled(Task task) => asyncContext.CreateCanceled();

            connectionTask.ContinueWith(NotifyCreateCanceled, TaskContinuationOptions.OnlyOnCanceled);

            Task NotifyCreateFaulted(Task task) => asyncContext.CreateFaulted(task.Exception);

            connectionTask.ContinueWith(NotifyCreateFaulted, TaskContinuationOptions.OnlyOnFaulted);
        }
Пример #22
0
        void CreateProducer(IAsyncPipeContextAgent <ProducerContext <TKey, TValue> > asyncContext, CancellationToken cancellationToken)
        {
            Task <ProducerContext <TKey, TValue> > Create(ClientContext clientContext, CancellationToken createCancellationToken)
            {
                ProducerBuilder <TKey, TValue> producerBuilder = _producerBuilderFactory();
                ProducerContext <TKey, TValue> context         = new KafkaProducerContext <TKey, TValue>(producerBuilder, _headersSerializer, cancellationToken);

                return(Task.FromResult(context));
            }

            _clientContextSupervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
        void CreateProcessor(IAsyncPipeContextAgent <ProducerContext> asyncContext, CancellationToken cancellationToken)
        {
            Task <ProducerContext> Create(ConnectionContext connectionContext, CancellationToken createCancellationToken)
            {
                var             client  = connectionContext.CreateEventHubClient(_eventHubName);
                ProducerContext context = new EventHubProducerContext(client, _messageSerializer, cancellationToken);

                return(Task.FromResult(context));
            }

            _contextSupervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
Пример #24
0
        async Task <ConnectionContext> CreateConnection(IAsyncPipeContextAgent <ConnectionContext> asyncContext, IAgent supervisor)
        {
            IConnection connection = null;

            try
            {
                if (supervisor.Stopping.IsCancellationRequested)
                {
                    throw new OperationCanceledException($"The connection is stopping and cannot be used: {_description}");
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connecting: {0}", _description);
                }

                connection = _settings.CreateConnection();

                connection.Start();

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connected: {0} (client-id: {1}, version: {2})", _description, connection.ClientId, connection.MetaData.NMSVersion);
                }

                var connectionContext = new ActiveMqConnectionContext(connection, _settings, _topology, _description, supervisor.Stopped);
                connectionContext.GetOrAddPayload(() => _settings);

                await asyncContext.Created(connectionContext).ConfigureAwait(false);

                return(connectionContext);
            }
            catch (OperationCanceledException)
            {
                await asyncContext.CreateCanceled().ConfigureAwait(false);

                throw;
            }
            catch (NMSConnectionException ex)
            {
                if (_log.IsDebugEnabled)
                {
                    _log.Debug("ActiveMQ Connect failed:", ex);
                }

                await asyncContext.CreateFaulted(ex).ConfigureAwait(false);

                connection?.Dispose();

                throw new ActiveMqConnectException("Connect failed: " + _description, ex);
            }
        }
Пример #25
0
        Task <ModelContext> CreateModel(IAsyncPipeContextAgent <ModelContext> asyncContext, CancellationToken cancellationToken)
        {
            async Task <ModelContext> CreateModelContext(ConnectionContext connectionContext, CancellationToken createCancellationToken)
            {
                var modelContext = await connectionContext.CreateModelContext(createCancellationToken).ConfigureAwait(false);

                LogContext.Debug?.Log("Created model: {ChannelNumber} {Host}", modelContext.Model.ChannelNumber, connectionContext.Description);

                return(modelContext);
            }

            return(_supervisor.CreateAgent(asyncContext, CreateModelContext, cancellationToken));
        }
        void CreateConsumer(IAsyncPipeContextAgent <ConsumerContext <TKey, TValue> > asyncContext, CancellationToken cancellationToken)
        {
            Task <ConsumerContext <TKey, TValue> > Create(ClientContext clientContext, CancellationToken createCancellationToken)
            {
                ConsumerBuilder <TKey, TValue> consumerBuilder = _consumerBuilderFactory();
                ConsumerContext <TKey, TValue> context         = new KafkaConsumerContext <TKey, TValue>(_hostConfiguration, _receiveSettings,
                                                                                                         _headersDeserializer, consumerBuilder, cancellationToken);

                return(Task.FromResult(context));
            }

            _clientContextSupervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
Пример #27
0
        void CreateProcessor(IAsyncPipeContextAgent <ProcessorContext> asyncContext, CancellationToken cancellationToken)
        {
            Task <ProcessorContext> Create(ConnectionContext connectionContext, CancellationToken createCancellationToken)
            {
                var blobContainerClient = _blobContainerClientFactory(connectionContext.StorageSettings);
                var client = _clientFactory(connectionContext.HostSettings, blobContainerClient);
                ProcessorContext context = new EventHubProcessorContext(_hostConfiguration, _receiveSettings, blobContainerClient,
                                                                        client, _partitionInitializingHandler, _partitionClosingHandler, createCancellationToken);

                return(Task.FromResult(context));
            }

            _contextSupervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
Пример #28
0
        void CreateSendEndpointContext(IAsyncPipeContextAgent <SendEndpointContext> asyncContext, CancellationToken cancellationToken)
        {
            async Task <SendEndpointContext> Create(ConnectionContext context, CancellationToken createCancellationToken)
            {
                var sendEndpointContext = CreateSendEndpointContext(context);

                if (_pipe.IsNotEmpty())
                {
                    await _pipe.Send(sendEndpointContext).ConfigureAwait(false);
                }

                return(sendEndpointContext);
            }

            _supervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
Пример #29
0
        void CreateSendEndpointContext(IAsyncPipeContextAgent <SendEndpointContext> asyncContext, CancellationToken cancellationToken)
        {
            async Task <SendEndpointContext> Create(ConnectionContext context, CancellationToken createCancellationToken)
            {
                var messageSender = context.CreateMessageSender(_settings.EntityPath);

                var sendEndpointContext = new MessageSendEndpointContext(context, messageSender);

                if (_pipe.IsNotEmpty())
                {
                    await _pipe.Send(sendEndpointContext).ConfigureAwait(false);
                }

                return(sendEndpointContext);
            }

            _supervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
        async Task <ConnectionContext> CreateConnection(IAsyncPipeContextAgent <ConnectionContext> asyncContext, IAgent supervisor)
        {
            IConnection connection = null;

            try
            {
                if (supervisor.Stopping.IsCancellationRequested)
                {
                    throw new OperationCanceledException($"The connection is stopping and cannot be used: {_configuration.Description}");
                }

                LogContext.Debug?.Log("Connecting: {Host}", _configuration.Description);

                connection = _configuration.Settings.CreateConnection();

                connection.Start();

                LogContext.Debug?.Log("Connected: {Host} (client-id: {ClientId}, version: {Version})", _configuration.Description, connection.ClientId,
                                      connection.MetaData.NMSVersion);

                var connectionContext = new ActiveMqConnectionContext(connection, _configuration, _hostTopology, supervisor.Stopped);

                await asyncContext.Created(connectionContext).ConfigureAwait(false);

                return(connectionContext);
            }
            catch (OperationCanceledException)
            {
                await asyncContext.CreateCanceled().ConfigureAwait(false);

                throw;
            }
            catch (NMSConnectionException ex)
            {
                LogContext.Error?.Log(ex, "ActiveMQ connection failed");

                await asyncContext.CreateFaulted(ex).ConfigureAwait(false);

                connection?.Dispose();

                throw new ActiveMqConnectException("Connect failed: " + _configuration.Description, ex);
            }
        }