internal async Task ProcessSessionMessageAsync(ProcessSessionMessageEventArgs args)
        {
            EnsureIsRunning();

            _concurrencyUpdateManager?.MessageProcessed();

            using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _cancellationTokenSource.Token))
            {
                var actions = new ServiceBusSessionMessageActions(args);
                if (!await _sessionMessageProcessor.Value.BeginProcessingMessageAsync(actions, args.Message, linkedCts.Token).ConfigureAwait(false))
                {
                    return;
                }

                ServiceBusTriggerInput input = ServiceBusTriggerInput.CreateSingle(args.Message, actions, _client.Value);

                TriggeredFunctionData data   = input.GetTriggerFunctionData();
                FunctionResult        result = await _triggerExecutor.TryExecuteAsync(data, linkedCts.Token).ConfigureAwait(false);

                if (actions.ShouldReleaseSession)
                {
                    args.ReleaseSession();
                }

                await _sessionMessageProcessor.Value.CompleteProcessingMessageAsync(actions, args.Message, result, linkedCts.Token).ConfigureAwait(false);
            }
        }
Esempio n. 2
0
 public override async Task <bool> BeginProcessingMessageAsync(
     ServiceBusSessionMessageActions actions,
     ServiceBusReceivedMessage message, CancellationToken cancellationToken)
 {
     _logger?.LogInformation("Custom processor Begin called!" + message.Body.ToString());
     return(await base.BeginProcessingMessageAsync(actions, message, cancellationToken));
 }
Esempio n. 3
0
            public static async Task RunAsync(
                [ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
                ServiceBusReceivedMessage[] messages,
                ServiceBusSessionMessageActions sessionActions,
                ServiceBusClient client)
            {
                using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    await sessionActions.CompleteMessageAsync(messages.First());

                    var sender = client.CreateSender(_secondQueueScope.QueueName);
                    await sender.SendMessageAsync(new ServiceBusMessage()
                    {
                        SessionId = "sessionId"
                    });

                    ts.Complete();
                }
                // This can be uncommented once https://github.com/Azure/azure-sdk-for-net/issues/24989 is fixed
                // ServiceBusReceiver receiver1 = await client.AcceptNextSessionAsync(_firstQueueScope.QueueName);
                // Assert.IsNull(receiver1);
                // need to use a separate client here to do the assertions
                var noTxClient = new ServiceBusClient(ServiceBusTestEnvironment.Instance.ServiceBusConnectionString);
                ServiceBusReceiver receiver2 = await noTxClient.AcceptNextSessionAsync(_secondQueueScope.QueueName);

                Assert.IsNotNull(receiver2);
                _waitHandle1.Set();
            }
            public static void RunAsync(
                [ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
                ServiceBusReceivedMessage[] messages,
                ServiceBusSessionMessageActions sessionActions)
            {
                var message = messages.Single();

                switch (count)
                {
                case 0:
                    Assert.AreEqual("session1", message.SessionId);
                    sessionActions.ReleaseSession();
                    break;

                case 1:
                case 2:
                    Assert.AreEqual("session2", message.SessionId);
                    break;

                case 3:
                    Assert.AreEqual("session1", message.SessionId);
                    _waitHandle1.Set();
                    break;
                }

                count++;
            }
            public static async Task QueueWithSessionsBatch(
                [ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
                ServiceBusReceivedMessage[] array,
                string[] sessionIdArray,
                ServiceBusSessionMessageActions sessionActions,
                CancellationToken cancellationToken,
                ILogger logger)
            {
                Assert.True(array.Length > 0);
                logger.LogInformation(
                    $"DrainModeTestJobBatch.QueueWithSessionsBatch: received {array.Length} messages with session id {array[0].SessionId}");
                Assert.AreEqual(_drainModeSessionId, array[0].SessionId);
                _drainValidationPreDelay.Set();
                await DrainModeHelper.WaitForCancellation(cancellationToken);

                Assert.True(cancellationToken.IsCancellationRequested);
                int index = 0;

                foreach (ServiceBusReceivedMessage msg in array)
                {
                    Assert.AreEqual(msg.SessionId, sessionIdArray[index++]);
                    // validate that manual lock renewal works
                    var initialLockedUntil = sessionActions.SessionLockedUntil;
                    await sessionActions.RenewSessionLockAsync();

                    Assert.Greater(sessionActions.SessionLockedUntil, initialLockedUntil);

                    await sessionActions.CompleteMessageAsync(msg);
                }

                _drainValidationPostDelay.Set();
            }
Esempio n. 6
0
 public static void SBQueue2SBQueue(
     [ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
     TestPoco[] array,
     ServiceBusSessionMessageActions messageSession)
 {
     string[] messages = array.Select(x => "{'Name': '" + x.Name + "', 'Value': 'Value'}").ToArray();
     ServiceBusMultipleTestJobsBase.ProcessMessages(messages);
 }
Esempio n. 7
0
 public static void SBQueue2SBQueue(
     [ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
     ServiceBusReceivedMessage[] array,
     ServiceBusSessionMessageActions messageSession)
 {
     string[] messages = array.Select(x => x.Body.ToString()).ToArray();
     ServiceBusMultipleTestJobsBase.ProcessMessages(messages);
 }
Esempio n. 8
0
 public override async Task CompleteProcessingMessageAsync(
     ServiceBusSessionMessageActions actions,
     ServiceBusReceivedMessage message,
     Executors.FunctionResult result,
     CancellationToken cancellationToken)
 {
     _logger?.LogInformation("Custom processor End called!" + message.Body.ToString());
     await base.CompleteProcessingMessageAsync(actions, message, result, cancellationToken);
 }
Esempio n. 9
0
            public static void SBSub1Trigger(
                [ServiceBusTrigger(TopicNameKey, FirstSubscriptionNameKey, IsSessionsEnabled = true)]
                ServiceBusReceivedMessage message, int deliveryCount,
                ServiceBusSessionMessageActions messageSession,
                ILogger log,
                string lockToken)
            {
                Assert.AreEqual(1, deliveryCount);
                Assert.AreEqual(message.LockToken, lockToken);

                ServiceBusSessionsTestHelper.ProcessMessage(message, log, _waitHandle1, _waitHandle2);
            }
Esempio n. 10
0
 public static async Task SBQueue2SBQueue(
     [ServiceBusTrigger(FirstQueueNameKey, IsSessionsEnabled = true)]
     string[] messages,
     ServiceBusSessionMessageActions messageSession, CancellationToken cancellationToken)
 {
     try
     {
         ServiceBusMultipleTestJobsBase.ProcessMessages(messages);
         await Task.Delay(0, cancellationToken);
     }
     catch (OperationCanceledException)
     {
     }
 }
Esempio n. 11
0
            public static async Task TopicWithSessions(
                [ServiceBusTrigger(TopicNameKey, FirstSubscriptionNameKey, IsSessionsEnabled = true)]
                ServiceBusReceivedMessage msg,
                ServiceBusSessionMessageActions messageSession,
                CancellationToken cancellationToken,
                ILogger logger)
            {
                logger.LogInformation(
                    $"DrainModeValidationFunctions.TopicWithSessions: message data {msg.Body} with session id {msg.SessionId}");
                Assert.AreEqual(_drainModeSessionId, msg.SessionId);
                _drainValidationPreDelay.Set();
                await DrainModeHelper.WaitForCancellation(cancellationToken);

                Assert.True(cancellationToken.IsCancellationRequested);
                await messageSession.CompleteMessageAsync(msg);

                _drainValidationPostDelay.Set();
            }
Esempio n. 12
0
        internal async Task ProcessSessionMessageAsync(ProcessSessionMessageEventArgs args)
        {
            using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(args.CancellationToken, _cancellationTokenSource.Token))
            {
                var actions = new ServiceBusSessionMessageActions(args);
                if (!await _sessionMessageProcessor.BeginProcessingMessageAsync(actions, args.Message, linkedCts.Token).ConfigureAwait(false))
                {
                    return;
                }

                ServiceBusTriggerInput input = ServiceBusTriggerInput.CreateSingle(args.Message);
                input.MessageActions = actions;

                TriggeredFunctionData data   = input.GetTriggerFunctionData();
                FunctionResult        result = await _triggerExecutor.TryExecuteAsync(data, linkedCts.Token).ConfigureAwait(false);

                await _sessionMessageProcessor.CompleteProcessingMessageAsync(actions, args.Message, result, linkedCts.Token).ConfigureAwait(false);
            }
        }
        public static async Task Run(
            [ServiceBusTrigger("<queue_name>", Connection = "<connection_name>", IsSessionsEnabled = true)]
            ServiceBusReceivedMessage[] messages,
            ServiceBusSessionMessageActions sessionActions)
        {
            foreach (ServiceBusReceivedMessage message in messages)
            {
                if (message.MessageId == "1")
                {
                    await sessionActions.DeadLetterMessageAsync(message);
                }
                else
                {
                    await sessionActions.CompleteMessageAsync(message);
                }
            }

            // We can also perform session-specific operations using the actions, such as setting state that is specific to this session.
            await sessionActions.SetSessionStateAsync(new BinaryData("<session state>"));
        }
Esempio n. 14
0
            public static async Task TopicWithSessionsBatch(
                [ServiceBusTrigger(TopicNameKey, FirstSubscriptionNameKey, IsSessionsEnabled = true)]
                ServiceBusReceivedMessage[] array,
                ServiceBusSessionMessageActions messageSession,
                CancellationToken cancellationToken,
                ILogger logger)
            {
                Assert.True(array.Length > 0);
                logger.LogInformation(
                    $"DrainModeTestJobBatch.TopicWithSessionsBatch: received {array.Length} messages with session id {array[0].SessionId}");
                Assert.AreEqual(_drainModeSessionId, array[0].SessionId);
                _drainValidationPreDelay.Set();
                await DrainModeHelper.WaitForCancellation(cancellationToken);

                Assert.True(cancellationToken.IsCancellationRequested);
                foreach (ServiceBusReceivedMessage msg in array)
                {
                    await messageSession.CompleteMessageAsync(msg);
                }

                _drainValidationPostDelay.Set();
            }