RocketMQ triggers

Updated at:

After ApsaraMQ for RocketMQ is integrated with Function Compute through EventBridge as an event source, an ApsaraMQ for RocketMQ trigger (RocketMQ trigger for short) can invoke the associated function. The function lets you process messages published to ApsaraMQ for RocketMQ in a custom manner. This topic describes how to create a RocketMQ trigger in the Function Compute console, configure function entry point parameters, and write and test function code.

How it works

After you submit a trigger creation request in the Function Compute console, Function Compute automatically creates an Event stream resource in EventBridge based on the trigger configuration.

After the trigger is created, you can view the trigger information in the Function Compute console and view the automatically created resources in the EventBridge console. When a message is enqueued in the source ApsaraMQ for RocketMQ instance, Function Compute is invoked. During each invocation, one or more message events are pushed to the function as a batch based on your batching configuration. This behavior suits end-to-end streaming data processing scenarios.

Usage notes

  • The ApsaraMQ for RocketMQ instance that serves as the event 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 RocketMQ triggers.

For the limits on the number of resources involved in creating a trigger within a single Alibaba Cloud account in a single region, see Limits.

Prerequisites

Step 1: Create a trigger

  1. Log on to the Function Compute console, find the function that you want to manage, and then on the function details page, click the Trigger tab and click Create Trigger.

  2. In the Create Trigger panel, set Trigger Type to ApsaraMQ for RocketMQ, configure the parameters described in the following table, and then click OK.

Parameter Description Example
Name Enter a custom trigger name. rocketmq-trigger
Version or Alias Default value: LATEST. To create a trigger for another version or alias, first switch to that version or alias in the upper-right corner of the function details page. For an introduction to versions and aliases, see Versions and Aliases. LATEST
ApsaraMQ for RocketMQ Instance Select an existing ApsaraMQ for RocketMQ instance. rmq-zky****
Topic Select a topic of the existing ApsaraMQ for RocketMQ instance. topic1
Tag Enter a message filter tag. The function is invoked only when a message that carries this filter tag is received. tag
Group ID Quickly create a group ID or select an existing one. Do not share a group ID with an existing business, to avoid affecting message sending and receiving for that business. GID_FC_TRIGGER_****
Consumer Offset Select the consumer offset of ApsaraMQ for RocketMQ messages, which is the position from which EventBridge starts pulling messages. Valid values: Latest Offset: consume from the latest offset. Earliest Offset: consume from the earliest offset. Timestamp: consume from a specified timestamp. Latest Offset
Invocation Method Select how the function is invoked. Valid values: Sync Invocation: suits sequential invocation scenarios. A single event or batch invokes the function, and the next event or batch invokes the function only after the current invocation returns a result. The maximum payload of a synchronous invocation request body is 32 MB. For more information, see Synchronous invocation. Async Invocation: consumes events quickly. A single event or batch invokes the function, Function Compute returns a response immediately, and the next event or batch invokes the function. The function runs asynchronously during this process. The maximum payload of an asynchronous invocation request body is 128 KB. For more information, see Asynchronous invocation. Sync Invocation

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

Step 2: (Optional) Configure function entry parameters

The ApsaraMQ for RocketMQ event source is passed to the function as an event input parameter. Use your code to parse the event parameter and process it. You can pass an event to the function manually to simulate a trigger event and check whether the function code is correct.

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

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

The event format is as follows.

[
    {
    "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"
    }
    },
    {
    "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 contained in the data field. For the parameters defined in the CloudEvents specification, see Overview.

Field Type Example Description
topic String TopicName The topic name.
systemProperties Map RocketMQ system-level message properties.
systemProperties.MIN_OFFSET Int 0 The earliest offset in the queue.
systemProperties.TRACE_ON Boolean true Whether a message trace exists.
systemProperties.MAX_OFFSET Int 8 The latest offset in the queue.
systemProperties.MSG_REGION String cn-hangzhou The region where the message was sent.
systemProperties.KEYS String systemProperties.KEYS Keys used to filter the message.
systemProperties.CONSUME_START_TIME Long 1628577790396 Message consumption start time, in milliseconds.
systemProperties.UNIQ_KEY String AC14C305069E1B28CDFA3181CDA2**** The unique key of the message.
systemProperties.TAGS String systemProperties.TAGS Tags used to filter the message.
systemProperties.INSTANCE_ID String MQ_INST_123456789098****_BXhFHryi The ApsaraMQ for RocketMQ instance ID.
userProperties Map The user properties.
body String TEST The message body.

Step 3: Write and test the function code

After you create the trigger, write and test the function code to verify that the code is correct. In practice, when an ApsaraMQ for RocketMQ event is delivered to Function Compute through EventBridge, the trigger automatically invokes the function.

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

    This topic uses Node.js function code as an example.

    'use strict';
    /*
    To enable the initializer feature,
    implement the initializer function as follows:
    exports.initializer = (context, callback) => {
      console.log('initializing');
      callback(null, '');
    };
    */
    exports.handler = (event, context, callback) => {
      console.log("event: %s", event);
      // Parse the event parameter and process the event.
      callback(null, 'return result');
    }
  2. Test the function.

    Method 1: If you configured the event function entry parameter to simulate the event source, click Test Function.

    Method 2: Log on to the ApsaraMQ for RocketMQ console, find the topic of the ApsaraMQ for RocketMQ instance that you want to use, click Message Send/Receive Test, and then send a message as prompted.

  3. After the run is complete, on the Code tab of the function details page, click Real-time Logs to view the result.