แก้ไข

แชร์ผ่าน


Send related Service Bus messages in a specific order by using a sequential convoy in Azure Logic Apps

Applies to: Azure Logic Apps (Consumption + Standard)

Some scenarios require your workflow to process session-related messages in a specific order from an Azure Service Bus queue. These messages have a property that defines the relationship with each other, such as a session ID. To process these messages by session, set up a sequential convoy pattern in your workflow.

For example, suppose you have a Service Bus queue that receives messages from multiple sessions. You have 10 messages from a session named Session 1 and 5 messages from a session named Session 2. You can create a workflow that alternates between sessions to process messages from the queue. When the trigger first fires, the workflow run handles all the messages from Session 1. When the trigger fires again, the workflow run handles all the messages from Session 2.

Diagram shows the general sequential convoy pattern.

This guide shows how to create a workflow that sets up the sequential convoy pattern to perform the following high-level steps:

  1. When the Service Bus trigger named When a message is received in a queue (peek-lock) fires, run the workflow.

    For more information about peek-lock mode, see Receive modes and Peek lock.

  2. Initialize a session based on the message that the trigger reads from the Service Bus queue.

  3. Read and process all the messages from the same session in the queue during the current workflow run.

For Standard logic apps, you can create a workflow by using the template named Azure Service Bus: Process related messages from a session-enabled queue for the same workflow instance - Sequential convoy pattern.

For Consumption workflows, no workflow template is available in the designer, so you have to manually build the workflow. You can also review the template for Standard workflows or the JSON file for the formerly available template named Correlated in-order delivery using service bus sessions available in GitHub: service-bus-sessions.json.

Prerequisites

Check access to Service Bus namespace

Your logic app resource and workflow need permissions to access your Service Bus namespace. If you're unsure whether your workflow has permissions, check them now.

  1. In the Azure portal, find and select your Service Bus namespace.

  2. On the sidebar menu, under Settings, select Shared access policies. Check that you have Manage permissions for that namespace.

    Screenshot shows the Shared access policies page for a Service Bus namespace.

  3. Get the connection string for your Service Bus namespace by following these steps:

    1. On the Shared access policies page, under Policy, select RootManageSharedAccessKey.

    2. On the SAS Policy: RootManageSharedAccessKey pane, next to the primary connection string, select the copy button.

      Screenshot shows the Shared access policies where you can copy a Service Bus namespace connection string.

      Tip

      To make sure that your connection string is associated with your Service Bus namespace and not a messaging entity, such as a queue, check the connection string for the EntityPath parameter. If you find this parameter, the connection string is for a specific entity, and isn't the correct string to use with your workflow.

    3. Save the connection string for later use when you connect from your workflow to the namespace.

Create a sequential convoy workflow

Based on whether your logic app resource is Standard or Consumption, follow the corresponding steps.

These steps create a workflow from the template named Azure Service Bus: Process related messages from a session-enabled queue for the same workflow instance - Sequential convoy pattern. The template includes the trigger and actions to implement this workflow pattern. You also create a connection to your Service Bus namespace and specify the name for the Service Bus queue that you want to use.

  1. In the Azure portal, open your Standard logic app resource.

  2. On the sidebar menu, under Workflows, select Workflows.

  3. On the Workflows toolbar, select Create > Create from Template.

  4. Under Templates, select the template named Azure Service Bus: Process related messages from a session-enabled queue for the same workflow instance - Sequential convoy pattern, and then select Use this template.

    The following example partially shows the workflow templates gallery:

    Screenshot shows a page where you can select a service bus sessions template.

  5. On the Create a new workflow from template pane, follow these steps:

    1. On the Basics tab, follow these steps:

      1. Enter a name for your workflow.

        The name must start with a letter and can contain only letters, numbers, dashes, and underscores.

      2. Select Stateful or Stateless, based on your scenario.

      3. Select Next.

    2. On the Connections tab, follow these steps:

      1. For Service Bus, in the Connection column, select Connect.

        Screenshot shows pane to start connecting to Azure Service Bus namespace.

      2. Provide the following information:

        Parameter Value Description
        Connection Name <connection-name> The name for the connection to your Service Bus namespace.
        Authentication Type Connection String The authentication type to use.
        Connection String <namespace-connection-string> The connection string that you saved earlier from your Service Bus namespace.

        Screenshot shows Service Bus connection pane in Standard workflow.

      3. When you're done, select Add connection > Next.

    3. On the Parameters tab, follow these steps:

      1. Provide the following information:

        Parameter Description
        Queue name The name for the Service Bus session-enabled queue with the messages to process.
        Maximum batch size The maximum number of messages to get as a batch.
      2. When you're done, select Next.

    4. Review the provided information, and select Create.

    The designer shows a workflow prepopulated with the trigger named When messages are available in a queue (peek-lock) and various actions, for example:

    Screenshot shows workflow with all operations.

    This workflow includes predefined cross-environment parameters to abstract values that change across various environments where your workflow runs, such as development, test, and production. Many operations in the workflow use these parameters instead of hardcoded values.

    The following table describes these cross-environment parameters:

    Name Type Value Description
    delayInMinutes_<workflow-name> Integer 0 The number of minutes to wait before renewing the message lock token.
    messageBatchSize_<workflow-name> Integer 50 The number of messages for the current batch.
    queueName_<workflow-name> String <Service-Bus-queue-name> The name for your Service Bus queue.

    For more information, see Create cross-environment parameters for workflow inputs.

  6. To learn about the predefined operations in the workflow, continue to the next section.

Workflow operations

The following sections describe the predefined operations in the workflow.

When messages are available in a queue (peek-lock)

This Azure Service Bus built-in trigger waits for one or more messages to arrive in a specified Service Bus queue. If a message exists in the queue, the trigger fires and runs a workflow instance. The term peek-lock means that the trigger sends a request to retrieve a message from the queue. If a message exists, the trigger retrieves and locks the message so that no other processing happens on that message until the lock period expires. The trigger also outputs a ServiceBusMessage object.

The following table describes the prepopulated trigger parameters:

Parameter Value Description
Queue name queueName_<workflow-name> The predefined cross-environment parameter that specifies your Service Bus queue. To set this value, follow these steps:

1. Select inside the Queue name box, and then select the lightning icon to open the dynamic content list.
2. From the list, select queueName_<workflow-name>.
Queue type Main This default type refers to your primary Service Bus queue.
Maximum message batch size messageBatchSize_<workflow-name> The predefined cross-environment parameter that specifies the largest number of messages in a batch. To set this value, follow these steps:

1. Select inside the Maximum message batch size box, and then select the lightning icon to open the dynamic content list.
2. From the list, select messageBatchSize_<workflow-name>.

For more information, see When messages are available in a queue (peek-lock).

Initialize Process Complete Flag

This Initialize variable action named Initialize Process Complete Flag creates a variable with the following initial values:

Parameter Value Description
Name processCompleted The name for the variable.
Type Boolean The data type for the variable.
Value false The default value for the variable.

The processCompleted variable's value changes to true during workflow execution later when the following conditions become true:

  • No more messages exist in the session for processing.
  • The session lock no longer needs to be renewed so the current workflow instance can finish running.
Scope action

The outer parent Scope action contains an inner nested Scope action named Business Logic Scope. This nested scope contains a placeholder action named Business Logic. Here, you can add the actions for your scenario's business logic to process the current message.

  1. For clarity, rename the outer parent Scope action to Process message.

    Screenshot shows the parent scope renamed as Process message.

  2. Expand the inner nested action named Business Logic Scope.

    If a problem happens in this scope, use the subsequent actions outside the parent Scope action to handle that problem.

  3. Replace the Business Logic placeholder action with the actual actions for your scenario's business logic.

Under the Business Logic Scope action, the Process Finished action is set up to always run, regardless of the status assigned to the Business Logic Scope action after all the actions in the Business Logic Scope action finish running. For the Process Finished action to always run, the workflow presets the Run after setting to the following values:

  • Is successful
  • Has timed out
  • Is skipped
  • Has failed

Screenshot shows the Run after settings for the Process Finished action.

The Process Finished action changes the processCompleted variable value from false to true, which indicates that message processing finished.

Screenshot shows the updated values in the Process Finished action.

Until

At the Business Logic Scope level, a parallel branch runs an Until loop that determines whether any other messages exist in the same session. Actions run inside the loop until the loop condition is met or the timeout period elapses. In this example, the loop continues running until message processing finishes as expressed by the following condition:

processCompleted = true

Screenshot shows the parallel branch with the Until loop.

Renew lock on a message in a queue

In the Until loop, the Service Bus action named Renew lock on a message in a queue controls the time that the workflow can process a message before the message becomes available again for processing. This behavior prevents premature release due to long-running tasks and makes sure that only one client handles the message at a time before setting the lock duration to the initial value. For more information, see Renew locks.

The following table describes the action's prepopulated parameters:

Parameter Value Description
Queue name queueName_<workflow-name> The predefined cross-environment parameter that specifies your Service Bus queue. To set this value, follow these steps:

1. Select inside the Queue name box, and then select the lightning icon to open the dynamic content list.
2. From the list, select queueName_<workflow-name>.
Lock token Lock Token The lock token for the message to abandon. To set this value, follow these steps:

1. Select inside the Lock token box, and then select the lightning icon to open the dynamic content list.
2. From the list, under When messages are available in a queue, select Lock Token.
Wait for Process to Complete

In the Until loop, the Delay action named Wait for Process to Complete adds an extra delay for message processing to complete. Make sure this delay value is less than the lock timeout duration for the queue. The minimum lock duration is 30 seconds.

The following table describes the action's prepopulated parameters:

Parameter Value Description
Count delayInMinutes_<workflow-name> The predefined cross-environment parameter that specifies the number of minutes to wait before renewing the message lock token. To set this value, follow these steps:

1. Select inside the Queue name box, and then select the lightning icon to open the dynamic content list.
2. From the list, select delayInMinutes_<workflow-name>.
Unit Minute The time unit for the delay.
Actions for post-message processing

Under the Process message scope, two parallel branches exist to handle the cases for unsuccessful and successful message processing.

  • Branch 1

    This branch includes a Compose action named Compensation Logic as a placeholder for the actions you want to run for handling errors and exceptions.

    1. Replace this placeholder with the actions that you want instead.

      For example, you might set up a Scope action that contains your error handling actions.

    2. Make sure that the error-handling action directly under the Process message scope runs only after the Process message scope completes with one of the following statuses:

      • Has timed out
      • Is skipped
      • Has failed

      For example:

      Screenshot shows error handling scope and run after settings.

    Under the error-handling actions, the Service Bus action named Abandon the message in a queue unlocks a message and requeues the message for another attempt.

    The following table describes the action's prepopulated values:

    Parameter Value Description
    Queue name queueName_<workflow-name> The predefined cross-environment parameter that specifies your Service Bus queue. To set this value, follow these steps:

    1. Select inside the Queue name box, and then select the lightning icon to open the dynamic content list.
    2. From the list, select queueName_<workflow-name>.
    Lock token Lock Token The lock token for the message to abandon. To set this value, follow these steps:

    1. Select inside the Lock token box, and then select the lightning icon to open the dynamic content list.
    2. From the list, under When messages are available in a queue, select Lock Token.

    For example:

    Screenshot shows Abandon message action for Standard workflow.

  • Branch 2

    This branch includes a Service Bus action named Complete the message in a queue that marks the message as done and deletes the message from the queue. This behavior prevents redelivery and moves the message to "processed" status. This action runs only if the Process message scope successfully completes.

    The following table describes the action's prepopulated values:

    Parameter Value Description
    Queue name queueName_<workflow-name> The predefined cross-environment parameter that specifies your Service Bus queue. To set this value, follow these steps:

    1. Select inside the Queue name box, and then select the lightning icon to open the dynamic content list.
    2. From the list, select queueName_<workflow-name>.
    Lock token Lock Token The lock token for the message to abandon. To set this value, follow these steps:

    1. Select inside the Lock token box, and then select the lightning icon to open the dynamic content list.
    2. From the list, under When messages are available in a queue, select Lock Token.

    For example:

    Screenshot shows Complete message action for Standard workflow.

Run the workflow

  1. Save your workflow. On the designer toolbar, select Save.

  2. To test your workflow, send messages to your Service Bus queue.

  3. Confirm that the workflow behaves as expected.