After the Message Queue for RabbitMQ event source is integrated with Function Compute by using EventBridge, the Message Queue for RabbitMQ trigger (RabbitMQ trigger) can trigger associated functions. The functions can be used to perform custom operations on the messages that are published to Message Queue for RabbitMQ. This topic describes how to create a RabbitMQ trigger, configure function input parameters, and write and test code in the Function Compute console.

Overview

After you submit a request to create a trigger in the Function Compute console, Function Compute creates EventBridge resources based on the trigger configurations. Two message push models are available: Event Model and Event Stream Model. The following resources are created for each model:
After the trigger is created, you can view the trigger information in the Function Compute console. You can also view the information about the created resources in the EventBridge console. When a message is enqueued in the source Message Queue for RabbitMQ instance, the function in Function Compute is triggered. The parameters vary with different message push models.
  • Event model: Each message is passed into a function as an event parameter. The event follows the specifications for CloudEvents. For information about the relationship between message content and CloudEvents, see the details of parameters.
  • Event stream model: One or more messages are passed into a function in batches based on your batch configurations. This model is suitable for scenarios in which end-to-end streaming data is processed.

Usage notes

  • The Message Queue for RabbitMQ instance that is the trigger source must be in the same region as the function in Function Compute.
  • When the number of created custom event buses and the number of created event rules reach the upper limits, you cannot create a Message Queue for RabbitMQ trigger of the event model.
  • When the number of created event streams reaches the upper limit, you cannot create a Message Queue for RabbitMQ trigger of the event stream model.
The following table describes the limits on the number of resources that can be created by using an Alibaba Cloud account in each region.
Item Upper limit
Number of custom event buses 10
Number of event rules created in each custom event bus 10
Number of event streams 30

Before you begin

Step 1: Create a trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click the function that you want to manage.
  4. On the function details page, click the Triggers tab, select the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
  5. In the Create Trigger panel, specify related parameters. After you specify the parameters, click OK.
    For more information about advanced settings, see Advanced features of EventBridge triggers.
    Category Parameter Description Example
    Basic settings Trigger Type Select Message Queue for RabbitMQ from the drop-down list. Message Queue for RabbitMQ
    Name Enter a trigger name. rabbitmq-trigger
    Version or Alias The default value is LATEST. If you want to create a trigger for another version or alias, switch to the specified version or alias in the upper-right corner of the function details page. For information about versions and aliases of a service, see Manage versions and Manage aliases. LATEST
    RabbitMQ Instance Select a Message Queue for RabbitMQ instance from the drop-down list. amqp-cn-i7m2l6m2****
    vhost Select a vhost of a Message Queue for RabbitMQ instance from the drop-down list. myhost-1
    Queue Select a queue of a Message Queue for RabbitMQ instance from the drop-down list. myqueue-1
    Advanced settings Invocation Method Select a method to invoke the function.
    Valid values:
    • Synchronous Invocation: After an event triggers a function, Function Compute returns the execution result when the execution is complete. This is the default value. For more information about synchronous invocation, see Synchronous invocations.
    • Asynchronous Invocation: After an event triggers a function, Function Compute immediately returns a response and ensures that the function is executed at least once. However, the detailed execution result is not returned. This invocation method is suitable for functions that have higher scheduling latency. For more information about asynchronous invocation, see Overview.
    Synchronous Invocation
    Message Push Model The underlying application model when message data is pushed to Function Compute.
    Valid values:
    • Event Model: Each message is passed into a function as an event parameter. The event follows the specifications for CloudEvents. For information about the relationship between message content and CloudEvents, see Parameters.
    • Event Stream Model: One or more messages are passed into a function in batches based on your batch configuration. This model is suitable for scenarios in which end-to-end streaming data is processed.
    Event Model
    Batch Push

    This parameter is available only if you select Event Stream Model as the message push model.

    Batch push helps you aggregate multiple events. Batch push is triggered when the conditions of either Batch Push Messages or Batch Push Interval are met.

    For example, if you set Batch Push Messages to 100 and Batch Push Interval to 15 seconds, the batch push is triggered immediately when the number of messages reaches 100 in less than 15 seconds.

    Enable
    Batch Push Messages The maximum number of messages that are sent by each function invocation in a batch. Requests are sent when the number of backlog messages reaches the specified value. Valid values: [1, 500]. 1
    Batch Push Interval The interval for function invocation. The system sends the aggregated messages to Function Compute at the specified interval. Valid values: [0, 15] (unit: seconds). A value of 0 specifies that the system sends the messages to Function Compute in real time. 1
    Retry Policy

    This parameter is available only if you select Event Stream Model as the message push model.

    The retry policy to be used when a message fails to be pushed.

    Valid values:
    • Backoff Retry: A message push request can be retried for up to three times at random intervals in the range of 10 to 20 seconds.
    • Exponential Decay Retry: A message push request can be retried for up to 176 times, and the retry lasts for a maximum of one day. The interval between each retry is increased by a factor of 2 up to a maximum of 512 seconds: 1, 2, 4, 8, ... 512 seconds.
    Backoff Retry
    Fault Tolerance Policy

    This parameter is available only if you select Event Stream Model as the message push model.

    The method to handle errors.

    Valid values:
    • Fault Tolerance Allowed: Fault tolerance is allowed. Event processing is not blocked when an error occurs. The messages that fail after they are retried based on the retry policy are delivered to dead-letter queues or discarded based on your configurations.
    • Fault Tolerance Prohibited: Fault tolerance is prohibited. If an error occurs and the message fails after it is retried based on the retry policy, event processing is blocked.
    Fault Tolerance Allowed
    Dead-letter Queue

    This parameter is available only if you select Event Stream Model as the message push model.

    The message queue to which events that are not processed or have exceeded the number of retries are sent. If you disable this feature, messages that have exceeded the number of retries specified by the retry policy are discarded.

    Enable Dead-letter Queue
    Queue Type The type of the dead-letter queue.
    Valid values:
    • MNS
    • Message Queue for Apache RocketMQ
    MNS
    Queue Name The name of the dead-letter queue. test-queue

    After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing trigger, see Manage triggers.

Step 2: Configure the input parameters of the function

The Message Queue for RabbitMQ event source is passed to the function in the form of event, which acts as the input parameter. You can also manually pass event to the function to trigger the function and test whether the code of the function is correct.

  1. On the function details page, click the Code tab and click the xialatubiao icon. From the drop-down list that appears, select Configure Test Parameters.
  2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. After you specify the parameters, click OK.
    Sample code for the event of the event model:
      {
        "id":"bj694332-4cj1-389e-9d8c-b137h30b****",
        "source":"RabbitMQ-Function-rabbitmq-trigger",
        "specversion":"1.0",
        "type":"amqp:Queue:SendMessage",
        "datacontenttype":"application/json;charset=utf-8",
        "subject":"acs:amqp:cn-hangzhou:164901546557****:/instances/amqp-cn-tl32e756****/vhosts/eb-connect/queues/housekeeping",
        "time":"2021-08-12T06:56:40.709Z",
        "aliyunaccountid":"164901546557****",
        "aliyunpublishtime":"2021-10-15T08:58:55.140Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"RabbitMQ-Function-rabbitmq-trigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "envelope":{
                "deliveryTag":98,
                "exchange":"",
                "redeliver":false,
                "routingKey":"housekeeping"
            },
            "body":{
                "Hello":"RabbitMQ"
            },
            "props":{
                "contentEncoding":"UTF-8",
                "messageId":"f7622d51-e198-41de-a072-77c1ead7****"
            }
        }
    }
    Sample code for the event of the event stream model:
    [
          {
        "id":"bj694332-4cj1-389e-9d8c-b137h30b****",
        "source":"RabbitMQ-Function-rabbitmq-trigger",
        "specversion":"1.0",
        "type":"amqp:Queue:SendMessage",
        "datacontenttype":"application/json;charset=utf-8",
        "subject":"acs:amqp:cn-hangzhou:164901546557****:/instances/amqp-cn-tl32e756****/vhosts/eb-connect/queues/housekeeping",
        "time":"2021-08-12T06:56:40.709Z",
        "aliyunaccountid":"164901546557****",
        "aliyunpublishtime":"2021-10-15T08:58:55.140Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"RabbitMQ-Function-rabbitmq-trigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "envelope":{
                "deliveryTag":98,
                "exchange":"",
                "redeliver":false,
                "routingKey":"housekeeping"
            },
            "body":{
                "Hello":"RabbitMQ"
            },
            "props":{
                "contentEncoding":"UTF-8",
                "messageId":"f7622d51-e198-41de-a072-77c1ead7****"
            }
        }
        },
        {
        "id":"bj694332-4cj1-389e-9d8c-b137h30b****",
        "source":"RabbitMQ-Function-rabbitmq-trigger",
        "specversion":"1.0",
        "type":"amqp:Queue:SendMessage",
        "datacontenttype":"application/json;charset=utf-8",
        "subject":"acs:amqp:cn-hangzhou:164901546557****:/instances/amqp-cn-tl32e756****/vhosts/eb-connect/queues/housekeeping",
        "time":"2021-08-12T06:56:40.709Z",
        "aliyunaccountid":"164901546557****",
        "aliyunpublishtime":"2021-10-15T08:58:55.140Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"RabbitMQ-Function-rabbitmq-trigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "envelope":{
                "deliveryTag":98,
                "exchange":"",
                "redeliver":false,
                "routingKey":"housekeeping"
            },
            "body":{
                "Hello":"RabbitMQ"
            },
            "props":{
                "contentEncoding":"UTF-8",
                "messageId":"f7622d51-e198-41de-a072-77c1ead7****"
            }
        }
        }
    ]
    The following table describes the parameters in data. For information about the parameters that are defined in the CloudEvents specification, see Overview.
    Parameter Type Example Description
    body Map The content of the message.
    Hello String EventBridge The user data.
    props Map The properties of the message.
    contentEncoding String utf-8 The format in which the content of the message is encoded.
    messageId String f7622d51-e198-41de-a072-77c1ead7**** The ID of the message. The ID of each message is unique.
    envelope Map The envelope information about the message.
    deliveryTag Int 98 The tag of the message.
    exchange String N/A The name of the exchange that sent the message.
    redeliver Boolean false Indicates whether the message can be resent. Valid values:
    • true: The message can be resent.
    • false: The message cannot be resent.
    routingKey String housekeeping The routing rule of the message.

Step 3: Write and test the function

After you create the trigger, you can write function code and test the function to verify that the code is correct. When Message Queue for RabbitMQ events are delivered to Function Compute by using EventBridge, the trigger triggers function execution.

  1. On the function details page, click the Code tab, edit the function code in the code editor, and then click Deploy.
    This topic uses the Node.js function code as an example.
    'use strict';
    /*
    To enable the initializer feature
    please implement the initializer function as below:
    exports.initializer = (context, callback) => {
      console.log('initializing');
      callback(null, '');
    };
    */
    exports.handler = (event, context, callback) => {
      console.log("event: %s", event);
      // Parse the event parameters and process the event. 
      callback(null, 'return result');
    }
  2. Click the Code tab and click Test Function.
    After the function is executed, you can view the result on the Code tab.

References

To modify or delete an existing trigger, see Manage triggers.