After the Message Queue for Apache RocketMQ event source is integrated with Function Compute by using EventBridge, the Message Queue for Apache RocketMQ trigger (RocketMQ trigger) can trigger associated functions. The functions can be used to perform custom operations on the messages that are published to Message Queue for Apache RocketMQ. This topic describes how to create a RocketMQ 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 that are named in the following formats based on the trigger configurations:
  • Event bus name: RocketMQ-<Function name>-<Trigger name>.
  • Event source name: RocketMQ-<Function name>-<Trigger name>.
  • Event rule name: <Service name>-<Function name>-<Trigger name>.
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 an event of the type specified by the event source is delivered to the event bus, the function that is associated with the trigger is triggered to execute once.

Limits

  • In EventBridge, the number of custom event buses in the same region cannot exceed 10, and the number of event rules that are created on each custom bus cannot exceed 10. If these limits are exceeded, you cannot create EventBridge triggers.
  • The created Message Queue for Apache RocketMQ instances and the Function Compute functions must be in the same region.
  • You cannot create RocketMQ triggers by using Serverless Devs.

Before you begin

Step 1: Create a trigger

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, find the desired service and click Functions in the Actions column.
  5. On the Functions page, click the function that you want to manage.
  6. On the function details page, click the Triggers tab, select a version or alias from the Version or Alias drop-down list, and then click Create Trigger.
  7. In the Create Trigger panel, specify related parameters. After you specify the parameters, click OK.
    Parameter Description Example
    Trigger Type Select Message Queue for Apache RocketMQ from the drop-down list. Message Queue for Apache RocketMQ
    Name Enter a custom trigger name rocketmq-trigger
    Version or Alias The default value is LATEST. If you want to create a trigger for another version or alias, switch to the trigger of the specified version or alias in the upper-right corner of the function details page. For more information about versions and aliases of a service, see Manage versions and Manage aliases. LATEST
    Apache RocketMQ Instance Select a Message Queue for Apache RocketMQ instance from the drop-down list. MQ_INST_164901546557****_BX7****
    Topic Select a topic of the Message Queue for Apache RocketMQ instance from the drop-down list. topic1
    Tag Specify a tag for message filtering.

    The execution of a function is triggered only when a message that contains the specified filtering tag is received.

    tag
    Group ID Select the group ID of the created Message Queue for Apache RocketMQ instance. GID_group1
    Consumer Offset Select a consumer offset of the message. A consumer offset specifies the point at which Message Queue for Apache RocketMQ starts to pull messages from the event bus. Valid values:
    • Latest Offset: consumes messages from the latest offset.
    • Earliest Offset: consumes messages from the earliest offset.
    • Timestamp: consumes messages from the specified timestamp.
    Latest Offset
    Invocation Method Select the function invocation method. Valid values:
    • Synchronous Invocation: After an event triggers the execution of a function, Function Compute returns the execution result when the execution is complete. This is the default value. For more information, see Synchronous invocations.
    • Asynchronous Invocation: After an event triggers the execution of a function, Function Compute immediately returns a response and ensures that the function is successfully executed at least once. However, the detailed execution result is not returned. This invocation method is suitable for functions that have relatively higher scheduling latency. For more information, see Asynchronous calls.
    Synchronous Invocation

    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 Apache RocketMQ 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 of event:

    {
        "id":"94ebc15f-f0db-4bbe-acce-56fb72fb****",
        "source":"RocketMQ-Function-rocketmq-trigger",
        "specversion":"1.0",
        "type":"mq:Topic:SendMessage",
        "datacontenttype":"application/json; charset=utf-8",
        "subject":"acs:mq:cn-hangzhou:164901546557****:MQ_INST_164901546557****_BXhFHryi%TopicName",
        "time":"2021-04-08T06:01:20.766Z",
        "aliyunaccountid":"164901546557****",
        "aliyunpublishtime":"2021-10-15T02:05:16.791Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"RocketMQ-Function-rocketmq-trigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "topic":"TopicName",
            "systemProperties":{
                "MIN_OFFSET":"0",
                "TRACE_ON":"true",
                "MAX_OFFSET":"8",
                "MSG_REGION":"cn-hangzhou",
                "KEYS":"systemProperties.KEYS",
                "CONSUME_START_TIME":1628577790396,
                "TAGS":"systemProperties.TAGS",
                "INSTANCE_ID":"MQ_INST_164901546557****_BXhFHryi"
            },
            "userProperties":{
    
            },
            "body":"TEST"
        }
    }     
    The following table describes the parameters in data. For more information about the parameter definition in the CloudEvents specification, see Overview.
    Parameter Type Example Description
    topic String TopicName The name of the topic.
    systemProperties Map The system properties.
    MIN_OFFSET Int 0 The earliest offset.
    TRACE_ON Boolean true Indicates whether a message trace exists. Valid values:
    • true: A message trace exists.
    • false: No message trace exists.
    MAX_OFFSET Int 8 The latest offset.
    MSG_REGION String cn-hangzhou The region from which the message was sent.
    KEYS String systemProperties.KEYS The keys for filtering.
    CONSUME_START_TIME Long 1628577790396 The start time of consumption. Unit: milliseconds.
    UNIQ_KEY String AC14C305069E1B28CDFA3181CDA2**** The unique key of the message.
    TAGS String systemProperties.TAGS The tags for filtering.
    INSTANCE_ID String MQ_INST_123456789098****_BXhFHryi The ID of the instance.
    userProperties Map N/A The user properties.
    body String TEST The content of the message.

Step 3: Write and test the function

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

  1. On the function details page, click the Code tab, enter function code in the code editor, and then click Save.
    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);
      handle_event(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.