All Products
Search
Document Center

Function Compute:Simple Message Queue (formerly MNS) queue triggers

Last Updated:Sep 19, 2024

After Simple Message Queue (formerly MNS) is integrated with Function Compute as an event source by using EventBridge, you can use Simple Message Queue (formerly MNS) queue triggers to trigger the associated functions. The functions can be used to perform custom operations on messages that are published to Simple Message Queue (formerly MNS). This topic describes how to create an Simple Message Queue (formerly MNS) queue trigger, configure the input parameters of the associated function, write function code, and test the function in the Function Compute console.

Overview

After you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates event streams in EventBridge based on the configurations of the trigger. For more information, see Overview.

After the trigger is created, you can view the information about the trigger 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 Simple Message Queue (formerly MNS) queue, the associated function in Function Compute is triggered. During function executions, message events are pushed to the function for processing separately or in batches based on your batch configurations. Simple Message Queue (formerly MNS) queue triggers are suitable for scenarios in which end-to-end streaming data is processed.

Usage notes

  • The Simple Message Queue (formerly MNS) queue that is used as the trigger source must reside in the same region as the Function Compute function.

  • If the number of created event streams exceeds the upper limit, you can no longer create Simple Message Queue (formerly MNS) queue triggers.

For information about the limits on resources related to triggers in one region for each Alibaba Cloud account, see Limits.

Before you start

Step 1: Create a trigger

After you create an Simple Message Queue (formerly MNS) queue, log on to the Function Compute console, click the desired function to go to the function details page, and then click the Configurations tab. Click Create Trigger on the Triggers page. Create a trigger and click OK. The following figure shows the details.

image

Description:

Invocation Method

  • Sync Invocation: This method is suitable for sequential invocations. When an event or a batch of events trigger the function, Function Compute executes the function and waits for a response before the function processes the next event or batch of events. The maximum payload of a synchronous invocation request is 32 MB. For more information, see Synchronous invocation.

  • Async Invocation: This method allows you to quickly consume events. When an event or a batch of events trigger the function, Function Compute immediately returns a response and continues to process the next event or batch of events. During this process, the function is executed in asynchronous mode. The maximum payload of an asynchronous invocation request is 128 KB. For more information, see Asynchronous invocation.

For information about advanced configurations such as push settings, retry policies, and dead-letter queues, see Advanced features of triggers.

After the trigger is created, it is displayed on the Triggers tab. To modify or delete a trigger, see Trigger management.

Step 2: (Optional) Configure input parameters

An Simple Message Queue (formerly MNS) source event is passed to the function as an input parameter in the form of event. You can use code to parse and process event. You can manually pass event to the function to simulate a trigger event to test whether the function code is correct.

  1. On the Code tab of the function details page, click the image.png icon next Test Function and select Configure Test Parameters from the drop-down list.

  2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, enter the event name and event content, and then click OK.

    Sample code of event:

    [
        {
        "id":"c2g71017-6f65-fhcf-a814-a396fc8d****",
        "source":"MNS-Function-mnstrigger",
        "specversion":"1.0",
        "type":"mns:Queue:SendMessage",
        "datacontenttype":"application/json; charset=utf-8",
        "subject":"acs:mns:cn-hangzhou:164901546557****:queues/zeus",
        "time":"2021-04-08T06:28:17.093Z",
        "aliyunaccountid":"164901546557****",
        "aliyunpublishtime":"2021-10-15T07:06:34.028Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"MNS-Function-mnstrigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "requestId":"606EA3074344430D4C81****",
            "messageId":"C6DB60D1574661357FA227277445****",
            "messageBody":"TEST"
        }
        },
        {
        "id":"d2g71017-6f65-fhcf-a814-a396fc8d****",
        "source":"MNS-Function-mnstrigger",
        "specversion":"1.0",
        "type":"mns:Queue:SendMessage",
        "datacontenttype":"application/json; charset=utf-8",
        "subject":"acs:mns:cn-hangzhou:164901546557****:queues/zeus",
        "time":"2021-04-08T06:28:17.093Z",
        "aliyunaccountid":"164901546557****",
        "aliyunpublishtime":"2021-10-15T07:06:34.028Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"MNS-Function-mnstrigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "requestId":"606EA3074344430D4C81****",
            "messageId":"C6DB60D1574661357FA227277445****",
            "messageBody":"TEST"
        }
        }
    ]

    The following table describes the parameters contained in data. For information about parameters that are defined in the CloudEvents specification, see Overview.

    ParameterTypeExampleDescription
    requestIdString606EA3074344430D4C81****The ID of the request. The ID of each request is unique.
    messageIdStringC6DB60D1574661357FA227277445****The ID of the message. The ID of each message is unique.
    messageBodyStringTESTThe content of the message.

Step 3: Write function code and test the function

After you create the trigger, you can write function code and test the function to verify whether the code is correct. When events that are generated by Simple Message Queue (formerly MNS) are delivered to Function Compute by using EventBridge, the trigger automatically triggers the function.

  1. On the Code tab of the function details page, write code in the code editor and click Deploy Code.

    The following sample code provides an example of function code in Node.js:

    '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 and process event. 
      callback(null, 'return result');
    }
  2. Test the function.

    Method 1: If you configure event to simulate an event source, click Test Function.

    Method 2: Log on to the Simple Message Queue (formerly MNS) console, select the queue that you created, and then click Send Message. The following figure shows an example.

    image

  1. After the execution is complete, view the result in Real-time Logs.

    image

More information

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