All Products
Search
Document Center

Function Compute:Self-managed Apache RocketMQ triggers

Last Updated:Jun 14, 2024

After Apache RocketMQ is integrated with Function Compute by using Function Compute, Apache RocketMQ triggers can be used to invoke functions. This way, you can invoke functions to process the messages that are published to Apache RocketMQ based on your business requirements. This topic describes how to create an Apache RocketMQ trigger, configure the input parameters of a function, and write and test function code in the Function Compute console.

Background information

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.

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 queued in Apache RocketMQ, the associated function in Function Compute is invoked. One or more message events are pushed to the function in batches for processing based on your batch configurations.

Prerequisites

  • EventBridge

  • Function Compute

    • A function is created. For more information, see the Create a function section of the "Manage functions" topic.

  • Apache RocketMQ

  • Note

    You can quickly deploy an Apache RocketMQ cluster and send and receive messages by referring to the following documentation:

    • An Apache RocketMQ cluster is created.

    • A topic is created.

    • A consumer group is created.

Usage notes

  • The Apache RocketMQ cluster that serves as the trigger source can be accessed over the Internet or an Alibaba Cloud virtual private cloud (VPC).

  • If you want to access the Apache RocketMQ cluster over a VPC, the VPC must reside in the same region as the function to be invoked in Function Compute.

  • If the number of created event streams reaches the upper limit, no more Apache RocketMQ triggers can be created. For more information about the limits on the number of event streams, see Limits.

Step 1: Create an Apache RocketMQ trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers. Then, click Create Trigger.

  4. In the Create Trigger panel, configure the parameters and click OK.

    Parameter

    Description

    Example

    Trigger Type

    The type of the trigger. Select Self-built Apache RocketMQ.

    Self-built Apache RocketMQ

    Name

    The name of the trigger.

    apache-rocketmq-trigger

    Version or Alias

    The version or alias of the trigger. Default value: LATEST. If you want to create a trigger for another version or alias, select a version or alias in the upper-right corner of the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.

    LATEST

    Endpoint

    The IP address of the NameServer in the Apache RocketMQ cluster.

    192.168.X.X:9876

    Topic

    The name of the existing topic in the created Apache RocketMQ cluster.

    testTopic

    Group ID

    The ID of the existing consumer group in the created Apache RocketMQ cluster.

    testGroup

    FilterType

    The method that you want to use to filter messages. Valid values:

    • Tag: filters messages by tag.

    • SQL: filters messages by SQL statement. You can match the attribute identifiers and attribute values of messages.

    Tag

    Filter

    After you configure the FilterType parameter, you must configure the tags or SQL statements that you want to use to filter messages.

    TagA

    Authentication Mode

    The authentication mode. You can set this parameter to ACL.

    ACL

    Username

    If you set the Authentication Mode parameter to ACL, you must specify an Apache RocketMQ username for authentication.

    admin

    Password

    If you set the Authentication Mode parameter to ACL, you must specify an Apache RocketMQ password for authentication.

    ******

    Consumer Offset

    The consumer offset of messages. A consumer offset specifies the point from which Apache RocketMQ starts to pull messages from EventBridge. 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

    Networking

    The type of the network over which you want to route messages. Valid values:

    • Internet: accesses the Apache RocketMQ cluster over the Internet.

    • VPC: accesses the Apache RocketMQ cluster over a VPC. You must configure the VPC, vSwitch, and Security Group parameters.

    Internet

    Invocation Method

    The mode in which the function is invoked.

    Valid values:

    • Sync Invocation (default): After an event invokes the function, Function Compute waits for the function invocation to complete before it returns a response. For more information, see Synchronous invocations.

    • Async Invocation: After an event invokes the function, Function Compute immediately returns a response and ensures that the function is successfully executed at least once. However, the detailed results are not returned. This mode is suitable for functions that have higher scheduling latency. For more information, see Overview.

    Sync Invocation

    Trigger State

    Specifies whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected. The trigger is immediately enabled after it is created.

    Enable Trigger

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

    The trigger is displayed on the Triggers tab after it is created. For more information about how to modify or delete a created trigger, see Manage triggers.

Step 2: Configure the input parameters of the function

An Apache RocketMQ event is used to invoke a function in Function Compute. The parameters of the event are used as the input parameters of the function. You can manually pass the parameters of the event to invoke the function as a test.

  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.

    The following sample code provides an example on the format of the event content:

    [
      {
        "msgId": "7F0000010BDD2A84AEE70DA49B57****",
        "topic": "testTopic",
        "systemProperties": {
          "UNIQ_KEY": "7F0000010BDD2A84AEE70DA49B57****",
          "CLUSTER": "DefaultCluster",
          "MIN_OFFSET": "0",
          "TAGS": "TagA",
          "MAX_OFFSET": "128"
        },
        "userProperties": {},
        "body": "Hello RocketMQ"
      }
    ]

    The following table describes the parameters of the event.

    Parameter

    Type

    Example

    Description

    msgId

    String

    7F0000010BDD2A84AEE70DA49B57****

    The ID of the Apache RocketMQ message.

    topic

    String

    testTopic

    The name of the topic in the Apache RocketMQ cluster.

    systemProperties

    Map

    The system properties.

    UNIQ_KEY

    String

    7F0000010BDD2A84AEE70DA49B57****

    The unique key of the message.

    CLUSTER

    String

    DefaultCluster

    The name of the Apache RocketMQ cluster.

    MIN_OFFSET

    Int

    0

    The minimum offset.

    MAX_OFFSET

    Int

    128

    The maximum offset.

    TAGS

    String

    TagA

    The tags that are used to filter messages.

    userProperties

    Map

    N/A

    The user properties.

    body

    String

    Hello RocketMQ

    The content of the message.

Step 3: Write and test function code

After you create the trigger, you can write function code and test the function code to verify whether the code is valid. When Apache RocketMQ receives a message, the trigger automatically invokes the function.

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

    In this example, the function code is written in Node.js. The following sample code provides 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 Test Function.

More information

In addition to the Function Compute console, you can configure triggers by using one of the following methods:

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