示例#1
0
        public virtual async Task Run(string forwardingBatchId, Predicate <MessageContext> filter, CancellationToken cancellationToken, int?expectedMessageCount)
        {
            IReceivingRawEndpoint         processor    = null;
            CancellationTokenRegistration?registration = null;

            try
            {
                shouldProcess      = filter;
                targetMessageCount = expectedMessageCount;
                actualMessageCount = 0;

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Starting receiver");
                }

                var config = createEndpointConfiguration();
                syncEvent            = new TaskCompletionSource <bool>();
                stopCompletionSource = new TaskCompletionSource <bool>();
                registration         = cancellationToken.Register(() => { Task.Run(() => syncEvent.TrySetResult(true), CancellationToken.None).Ignore(); });

                processor = await RawEndpoint.Start(config).ConfigureAwait(false);

                Log.Info($"Forwarder for batch {forwardingBatchId} started receiving messages from {processor.TransportAddress}.");

                if (!expectedMessageCount.HasValue)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("Running in timeout mode. Starting timer.");
                    }

                    timer.Change(TimeSpan.FromSeconds(45), Timeout.InfiniteTimeSpan);
                }
            }
            finally
            {
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat($"Waiting for forwarder for batch {forwardingBatchId} to finish.");
                }

                await syncEvent.Task.ConfigureAwait(false);

                registration?.Dispose();
                if (processor != null)
                {
                    await processor.Stop().ConfigureAwait(false);
                }

                Log.Info($"Forwarder for batch {forwardingBatchId} finished forwarding all messages.");

                await Task.Run(() => stopCompletionSource.TrySetResult(true), CancellationToken.None).ConfigureAwait(false);
            }

            if (endedPrematurely || cancellationToken.IsCancellationRequested)
            {
                throw new Exception("We are in the process of shutting down. Safe to ignore.");
            }
        }
示例#2
0
        public virtual async Task Run(Predicate <MessageContext> filter, CancellationToken cancellationToken, int?expectedMessageCount = null)
        {
            IReceivingRawEndpoint         processor    = null;
            CancellationTokenRegistration?registration = null;

            try
            {
                Log.DebugFormat("Started. Expectected message count {0}", expectedMessageCount);

                if (expectedMessageCount.HasValue && expectedMessageCount.Value == 0)
                {
                    return;
                }

                shouldProcess      = filter;
                targetMessageCount = expectedMessageCount;
                actualMessageCount = 0;
                Log.DebugFormat("Starting receiver");

                var config = createEndpointConfiguration();
                syncEvent            = new TaskCompletionSource <bool>();
                stopCompletionSource = new TaskCompletionSource <bool>();
                registration         = cancellationToken.Register(() => { Task.Run(() => syncEvent.TrySetResult(true), CancellationToken.None).Ignore(); });

                processor = await RawEndpoint.Start(config).ConfigureAwait(false);

                if (!expectedMessageCount.HasValue)
                {
                    Log.Debug("Running in timeout mode. Starting timer");
                    timer.Change(TimeSpan.FromSeconds(45), Timeout.InfiniteTimeSpan);
                }

                Log.InfoFormat("{0} started", GetType().Name);
            }
            finally
            {
                Log.DebugFormat("Waiting for {0} finish", GetType().Name);
                await syncEvent.Task.ConfigureAwait(false);

                registration?.Dispose();
                if (processor != null)
                {
                    await processor.Stop().ConfigureAwait(false);
                }

                await Task.Run(() => stopCompletionSource.TrySetResult(true), CancellationToken.None).ConfigureAwait(false);

                Log.DebugFormat("{0} finished", GetType().Name);
            }

            if (endedPrematurelly || cancellationToken.IsCancellationRequested)
            {
                throw new Exception("We are in the process of shutting down. Safe to ignore.");
            }
        }
        /// <summary>
        /// Starts the endpoint.
        /// </summary>
        public async Task Start()
        {
            var outConfig = RawEndpointConfiguration.Create(storageQueueName, OnOutgoingMessage, poisonMessageQueue);

            outConfig.LimitMessageProcessingConcurrencyTo(1);
            transportCustomization(outConfig.UseTransport <T>());
            outConfig.CustomErrorHandlingPolicy(new RetryForeverPolicy());
            outConfig.AutoCreateQueue();

            outEndpoint = await RawEndpoint.Start(outConfig).ConfigureAwait(false);
        }
示例#4
0
        public override async Task Start(CancellationToken token)
        {
            var config = RawEndpointConfiguration.Create("poison", OnMessage, "poison");

            config.AutoCreateQueue();
            config.CustomErrorHandlingPolicy(new IgnoreErrorsPolicy());
            var transport = config.UseTransport <TestTransport>();

            transportConfiguration(transport);

            endpoint = await RawEndpoint.Start(config);
        }
 internal void SetEndpoint(IReceivingRawEndpoint endpointInstance)
 {
     lock (endpointCriticalLock)
     {
         endpoint = endpointInstance;
         foreach (var latentCritical in criticalErrors)
         {
             RaiseForEndpoint(latentCritical.Message, latentCritical.Exception);
         }
         criticalErrors.Clear();
     }
 }
示例#6
0
    public async Task Start()
    {
        leftStartable = await RawEndpoint.Create(leftConfig).ConfigureAwait(false);

        rightStartable = await RawEndpoint.Create(rightConfig).ConfigureAwait(false);

        leftSubscribeRouter  = CreateSubscribeRouter(leftStartable.Settings.Get <TransportInfrastructure>());
        rightSubscribeRouter = CreateSubscribeRouter(rightStartable.Settings.Get <TransportInfrastructure>());

        leftEndpoint = await leftStartable.Start().ConfigureAwait(false);

        rightEndpoint = await rightStartable.Start().ConfigureAwait(false);
    }
示例#7
0
文件: BusStuff.cs 项目: kortov1337/CT
 public static async Task InitServerEndpoint(string endpointName)
 {
     if (serverEndpoint == null)
     {
         var senderConfig = RawEndpointConfiguration.Create(
             endpointName: endpointName,
             onMessage: OnMessage,
             poisonMessageQueue: "error");
         senderConfig.UseTransport <MsmqTransport>();
         serverEndpoint = await RawEndpoint.Start(senderConfig)
                          .ConfigureAwait(false);
     }
 }
示例#8
0
文件: BusStuff.cs 项目: kortov1337/CT
 public async static Task InitSubTwoEndpoint(string endpointName, Func <MessageContext, IDispatchMessages, Task> func)
 {
     if (subscriber2Endpoint == null)
     {
         var senderConfig = RawEndpointConfiguration.Create(
             endpointName: endpointName,
             onMessage: func,
             poisonMessageQueue: "error");
         senderConfig.UseTransport <MsmqTransport>();
         senderConfig.AutoCreateQueue();
         subscriber2Endpoint = await RawEndpoint.Start(senderConfig)
                               .ConfigureAwait(false);
     }
 }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var endpointConfiguration = RawEndpointConfiguration.Create(attribute.QueueName, OnMessage, poisonMessageQueue);

            if (Directory.Exists(nsbLogPath))
            {
                LogManager.Use <DefaultFactory>().Directory(nsbLogPath);
            }

            var connectionString = AmbientConnectionStringProvider.Instance.GetConnectionString(attribute.Connection);

            endpointConfiguration.UseTransport <AzureStorageQueueTransport>().ConnectionString(connectionString);

            endpointConfiguration.DefaultErrorHandlingPolicy(poisonMessageQueue, immediateRetryCount);

            endpoint = await RawEndpoint.Start(endpointConfiguration).ConfigureAwait(false);
        }
示例#10
0
文件: BusStuff.cs 项目: kortov1337/CT
    public static async Task SendToSubscriber(MessageContext context, IReceivingRawEndpoint endpoint)
    {
        var headers = context.Headers;
        var request = new OutgoingMessage(
            messageId: Guid.NewGuid().ToString(),
            headers: headers,
            body: context.Body);

        var operation = new TransportOperation(
            request,
            new UnicastAddressTag(endpoint.EndpointName));

        await endpoint.Dispatch(
            outgoingMessages : new TransportOperations(operation),
            transaction : new TransportTransaction(),
            context : new ContextBag())
        .ConfigureAwait(false);
    }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var nameShortener            = new RuleNameShortener();
            var endpointConfigurationRaw = RawEndpointConfiguration.Create(_attribute.Endpoint, OnMessage, _poisonMessageQueue);

            if (_nServiceBusOptions.EndpointConfiguration != null)
            {
                endpointConfigurationRaw = _nServiceBusOptions.EndpointConfiguration.Invoke(endpointConfigurationRaw);
            }
            else
            {
                var tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();

                endpointConfigurationRaw.UseTransport <AzureServiceBusTransport>()
                .RuleNameShortener(nameShortener.Shorten)
                .CustomTokenProvider(tokenProvider)
                .ConnectionString(_attribute.Connection)
                .Transactions(TransportTransactionMode.ReceiveOnly);
            }

            if (!string.IsNullOrEmpty(EnvironmentVariables.NServiceBusLicense))
            {
                endpointConfigurationRaw.UseLicense(EnvironmentVariables.NServiceBusLicense);
            }
            endpointConfigurationRaw.DefaultErrorHandlingPolicy(_poisonMessageQueue, ImmediateRetryCount);
            endpointConfigurationRaw.AutoCreateQueue();

            _endpoint = await RawEndpoint.Start(endpointConfigurationRaw).ConfigureAwait(false);

            if (_nServiceBusOptions.OnStarted != null)
            {
                _nServiceBusOptions.OnStarted.Invoke(_endpoint);
            }

            await _endpoint.SubscriptionManager.Subscribe(_parameter.ParameterType, new ContextBag());
        }
示例#12
0
 private async Task StopEndpoint(IReceivingRawEndpoint endpoint)
 {
     await endpoint.Stop().ConfigureAwait(false);
 }
示例#13
0
 /// <summary>
 /// Starts replaying of subscription messages.
 /// </summary>
 public async Task Start()
 {
     stoppable = await endpoint.Start().ConfigureAwait(false);
 }